C端小程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

545 wiersze
14 KiB

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