C端小程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
12 KiB

  1. // pages/spellGroup/mySpellGroup/index.js
  2. var config = require("../../../config/config.js");
  3. const Http = require("../../../utils/HttpBasics");
  4. const imgurl = require("../../../utils/imgurl");
  5. const utils = require("../../../utils/util.js")
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. teljpgUrl: imgurl.teljpg.url,
  12. couponChannelId:'',
  13. couponId:'',
  14. data:[],
  15. spellData:null,
  16. canSpell:true,
  17. canBuyIf:true,
  18. clock: "00",
  19. day: "00",
  20. hour: "00",
  21. min: "00",
  22. sec: "00",
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function (options) {
  28. this.setData({
  29. couponChannelId: options.couponChannelId,
  30. couponId: options.couponId
  31. })
  32. this.getDetail(options.couponChannelId);
  33. this.getOneSpell(options.couponId)
  34. this.getUserInfo();
  35. },
  36. /**
  37. * 拨打电话
  38. */
  39. phone: function (e) {
  40. let that = this;
  41. wx.makePhoneCall({
  42. phoneNumber: e.target.dataset.merchantlinkphone
  43. });
  44. },
  45. /**
  46. * 直接购买
  47. */
  48. gotoBuy(){
  49. console.log(22222)
  50. this.setData({
  51. canBuyIf: false
  52. })
  53. this.orderFunc()
  54. },
  55. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  56. dateformat(micro_second) {
  57. // 总秒数
  58. var second = Math.floor(micro_second / 1000);
  59. // 天数
  60. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  61. // 小时
  62. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  63. // 分钟
  64. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  65. // 秒
  66. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  67. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  68. return {
  69. a1: day,
  70. b1: hr,
  71. c1: min,
  72. d1: sec
  73. }
  74. },
  75. countdown(end_time) {
  76. let that = this;
  77. var EndTime = end_time;
  78. var NowTime = new Date().getTime();
  79. var total_micro_second = EndTime - NowTime || [];
  80. // 渲染倒计时时钟
  81. let obj = that.dateformat(total_micro_second);
  82. if (total_micro_second > 0) {
  83. that.setData({
  84. clock: obj,
  85. day: obj.a1,
  86. hour: obj.b1,
  87. min: obj.c1,
  88. sec: obj.d1,
  89. })
  90. } else {
  91. that.setData({
  92. // spellData:null,
  93. clock: "00",
  94. day: "00",
  95. hour: "00",
  96. min: "00",
  97. sec: "00",
  98. })
  99. // that.getOneSpell(that.data.couponId)
  100. }
  101. setTimeout(function () {
  102. total_micro_second -= 1000;
  103. that.countdown(end_time);
  104. }, 1000)
  105. },
  106. /**
  107. * 发起拼团
  108. */
  109. gotoSpell(){
  110. console.log(111111111)
  111. this.setData({
  112. canSpell:false
  113. })
  114. this.orderFunc(0)
  115. },
  116. //参与别人的拼团
  117. gotoPartner(){
  118. wx.navigateTo({
  119. url: `/pages/joinFrDpell/index?orderId=${this.data.spellData.orderId}&couponId=${this.data.spellData.couponId}&orderGroupId=${this.data.spellData.orderGroupId}&couponChannelId=${this.data.couponChannelId}&avatarUrl=${_this.data.avatarUrl}&nickName=${_this.data.nickName}`
  120. });
  121. },
  122. /**
  123. * 获取一个拼团信息
  124. */
  125. getOneSpell(couponId){
  126. let that = this;
  127. Http.get({
  128. url: config.api.queryRemainOne,
  129. data: {
  130. couponId: couponId
  131. }
  132. }).then(res => {
  133. if(res.data){
  134. that.countdown(res.data.expiredDate);
  135. that.setData({
  136. spellData: res.data
  137. });
  138. }
  139. });
  140. },
  141. /**
  142. * 获取券详情信息
  143. */
  144. getDetail(couponChannelId){
  145. let that = this;
  146. Http.get({
  147. url: config.api.couponDetail,
  148. data: {
  149. couponChannelId: couponChannelId
  150. }
  151. }).then(res => {
  152. let data = res.data;
  153. data.price = (data.price / 100).toFixed(2)
  154. data.salePrice = (data.salePrice / 100).toFixed(2);
  155. data.validStartDate = utils.formatTime(data.validStartDate,'yyyy-MM-dd')
  156. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  157. that.setData({
  158. data
  159. });
  160. });
  161. },
  162. /**
  163. * 去拼团
  164. */
  165. goToOrderGroup(orderId, orderGroupId, _this){
  166. console.log(22222222)
  167. let that = this;
  168. // 支付成功
  169. Http.post({
  170. url: config.api.toOrderGroup,
  171. data: {
  172. id: orderGroupId,
  173. orderId,
  174. couponId: _this.data.data.couponId
  175. }
  176. })
  177. .then(res => {
  178. wx.navigateTo({
  179. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}&couponChannelId=${_this.data.couponChannelId}`
  180. });
  181. })
  182. .catch(err => {
  183. console.log(err);
  184. })
  185. // return;
  186. },
  187. /**
  188. * 支付订单更新
  189. */
  190. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  191. console.log(orderGroupId,7777)
  192. let that = this;
  193. // 支付成功
  194. Http.post({
  195. url: config.api.payOrderUpdate,
  196. data: {
  197. payOrderId: payOrderId,
  198. orderId: orderId,
  199. status: status,
  200. reason: reason
  201. }
  202. })
  203. .then(res => {
  204. wx.hideLoading()
  205. if (orderGroupId==undefined){
  206. wx.navigateTo({
  207. url: '/pages/order/detail/index?orderId='+res.data.id,
  208. })
  209. }else{
  210. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  211. }
  212. })
  213. .catch(err => {
  214. console.log(err);
  215. if (err.code!=12002){
  216. setTimeout(function () {
  217. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  218. }, 2000)
  219. }
  220. })
  221. },
  222. /**
  223. * 发起支付
  224. */
  225. orderFunc(orderGroupId) {
  226. let data = {
  227. couponChannelId: this.data.data.id,
  228. couponId: this.data.data.couponId
  229. }
  230. /**
  231. * 拼团订单
  232. */
  233. if (orderGroupId==0){
  234. data.orderGroupId = orderGroupId
  235. }
  236. var that = this;
  237. Http.post({
  238. url: config.api.checkPhoneStatus,
  239. data: {}
  240. })
  241. .then(res => {
  242. /**
  243. * orderSave 下单
  244. */
  245. return Http.post({
  246. url: config.api.orderSave,
  247. data: data
  248. });
  249. })
  250. .catch(err => {
  251. console.log(err);
  252. that.setData({
  253. showbutton: false,
  254. showbutton1: false
  255. })
  256. if (err.code == 2011) {
  257. wx.showToast({
  258. title: "商户信息没找到",
  259. image: './../../../assets/images/fail.png',
  260. duration: 2000,
  261. mask: false
  262. });
  263. } else if (err.code == 2013) {
  264. wx.showToast({
  265. title: "商户信息禁用",
  266. image: './../../../assets/images/fail.png',
  267. duration: 2000,
  268. mask: false
  269. });
  270. } else if (err.code == 3000) {
  271. wx.showToast({
  272. title: "库存不足",
  273. image: './../../../assets/images/fail.png',
  274. duration: 2000,
  275. mask: false
  276. });
  277. } else if (err.code == 3001) {
  278. wx.showToast({
  279. title: "超过限购条件",
  280. image: './../../../assets/images/fail.png',
  281. duration: 2000,
  282. mask: false
  283. });
  284. } else if (err.code == 3002) {
  285. wx.showToast({
  286. title: "订单失败",
  287. image: './../../../assets/images/fail.png',
  288. duration: 2000,
  289. mask: false
  290. });
  291. } else if (err.code == 3003) {
  292. wx.showToast({
  293. title: "订单不存在",
  294. image: './../../../assets/images/fail.png',
  295. duration: 2000,
  296. mask: false
  297. });
  298. } else if (err.code == 3004) {
  299. wx.showToast({
  300. title: "订单不存在",
  301. image: './../../../assets/images/fail.png',
  302. duration: 2000,
  303. mask: false
  304. });
  305. } else if (err.code == 4003) {
  306. wx.showToast({
  307. title: "卡券已作废",
  308. image: './../../../assets/images/fail.png',
  309. duration: 2000,
  310. mask: false
  311. });
  312. } else if (err.code == 11005) {
  313. /**
  314. * 将值传到用户手机号授权的页面
  315. *
  316. */
  317. wx.redirectTo({
  318. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  319. that.data.couponChannelId + '&couponId=' + that.data.couponId
  320. });
  321. } else if (err.code == 11006) {
  322. // 用户手机已加密
  323. wx.redirectTo({
  324. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  325. that.data.couponChannelId + '&couponId=' + that.data.couponId
  326. });
  327. } else {
  328. wx.showToast({
  329. title: err.message,
  330. icon: 'none',
  331. duration: 2000,
  332. mask: false
  333. });
  334. }
  335. })
  336. .then(res => {
  337. console.log(res)
  338. if (typeof (res) != "undefined") {
  339. let orderId = "" + res.data.id;
  340. that.setData({
  341. orderId: orderId
  342. });
  343. // 支付金额不为0
  344. /**
  345. * 支付订单创建
  346. */
  347. Http.post({
  348. url: config.api.payOrderCreate,
  349. data: {
  350. orderId: orderId
  351. }
  352. })
  353. .then(res => {
  354. var payOrderId = "" + res.data.payOrderId;
  355. wx.hideLoading();
  356. wx.requestPayment({
  357. timeStamp: res.data.timeStamp,
  358. nonceStr: res.data.nonceStr,
  359. package: res.data.package,
  360. signType: (res.data.signType) ? res.data.signType : "MD5",
  361. paySign: res.data.paySign,
  362. success: res => {
  363. wx.showLoading({
  364. title: '订单正在处理中...',
  365. })
  366. setTimeout(function () {
  367. wx.hideLoading()
  368. }, 5000)
  369. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  370. if (res.errMsg == "requestPayment:ok") {
  371. setTimeout(function () {
  372. wx.hideLoading();
  373. }, 2000);
  374. /**
  375. * 用户支付成功以后跳转到券包列表
  376. */
  377. if (that.data.cardType == 100) {
  378. wx.setStorage({
  379. key: 'couponNum2',
  380. data: "couponNum2"
  381. })
  382. } else if (that.data.data.type != 5) {
  383. wx.setStorage({
  384. key: 'couponNum',
  385. data: "couponNum"
  386. })
  387. }
  388. }
  389. },
  390. fail: res => {
  391. /**
  392. * 支付失败,需要更新订单的状态
  393. */
  394. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  395. that.setData({
  396. showbutton: false
  397. })
  398. return;
  399. },
  400. complete: res => { }
  401. });
  402. /// End payment --------
  403. })
  404. .catch(err => {
  405. wx.showToast({
  406. title: err.message,
  407. icon: 'none',
  408. duration: 2000,
  409. mask: false
  410. });
  411. })
  412. }
  413. })
  414. },
  415. /**
  416. * 生命周期函数--监听页面初次渲染完成
  417. */
  418. onReady: function () {
  419. },
  420. /**
  421. * 生命周期函数--监听页面显示
  422. */
  423. onShow: function () {
  424. this.setData({
  425. canSpell:true,
  426. canBuyIf:true
  427. })
  428. },
  429. /**
  430. * 生命周期函数--监听页面隐藏
  431. */
  432. onHide: function () {
  433. },
  434. /**
  435. * 生命周期函数--监听页面卸载
  436. */
  437. onUnload: function () {
  438. },
  439. /**
  440. * 页面相关事件处理函数--监听用户下拉动作
  441. */
  442. onPullDownRefresh: function () {
  443. },
  444. /**
  445. * 页面上拉触底事件的处理函数
  446. */
  447. onReachBottom: function () {
  448. }
  449. })