C端小程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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