C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

531 行
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.showModal({
  362. title: '提示',
  363. content: '您有未支付订单,请先进行支付',
  364. confirmText: "去支付",
  365. success: function (res) {
  366. console.log(res.confirm)
  367. if (res.confirm) {
  368. wx.navigateTo({
  369. url: '/pages/order/index/index?id=all',
  370. })
  371. }
  372. }
  373. })
  374. } else if (err.code == 11005) {
  375. /**
  376. * 将值传到用户手机号授权的页面
  377. *
  378. */
  379. console.log(that.data, 444)
  380. wx.redirectTo({
  381. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  382. that.data.couponChannelId + '&orderGroupId=' + that.data.orderGroupId+ '&couponId=' + that.data.couponId + '&orderId=' + that.data.orderId + '&avatarUrl=' + that.data.avatarUrl + '&nickName=' + that.data.nickName
  383. });
  384. } else if (err.code == 11006) {
  385. // 用户手机已加密
  386. wx.redirectTo({
  387. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  388. that.data.couponChannelId + '&orderGroupId=' + that.data.orderGroupId + '&couponId=' + that.data.couponId + '&orderId=' + that.data.orderId + '&avatarUrl=' + that.data.avatarUrl + '&nickName=' + that.data.nickName
  389. });
  390. } else {
  391. wx.showToast({
  392. title: err.message,
  393. icon: 'none',
  394. duration: 2000,
  395. mask: false
  396. });
  397. }
  398. })
  399. .then(res => {
  400. console.log(res)
  401. if (typeof (res) != "undefined") {
  402. let orderId = "" + res.data.id;
  403. that.setData({
  404. orderId: orderId,
  405. canSpell: false,
  406. canBuyIf: true
  407. });
  408. // 支付金额不为0
  409. /**
  410. * 支付订单创建
  411. */
  412. Http.post({
  413. url: config.api.payOrderCreate,
  414. data: {
  415. orderId: orderId
  416. }
  417. })
  418. .then(res => {
  419. var payOrderId = "" + res.data.payOrderId;
  420. wx.hideLoading();
  421. wx.requestPayment({
  422. timeStamp: res.data.timeStamp,
  423. nonceStr: res.data.nonceStr,
  424. package: res.data.package,
  425. signType: (res.data.signType) ? res.data.signType : "MD5",
  426. paySign: res.data.paySign,
  427. success: res => {
  428. wx.showLoading({
  429. title: '订单正在处理中...',
  430. })
  431. that.setData({
  432. canSpell: false
  433. })
  434. setTimeout(function () {
  435. wx.hideLoading()
  436. }, 5000)
  437. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  438. if (res.errMsg == "requestPayment:ok") {
  439. setTimeout(function () {
  440. wx.hideLoading();
  441. }, 2000);
  442. }
  443. },
  444. fail: res => {
  445. /**
  446. * 支付失败,需要更新订单的状态
  447. */
  448. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  449. that.setData({
  450. showbutton: false,
  451. canSpell: true,
  452. canBuyIf: true
  453. })
  454. return;
  455. },
  456. complete: res => { }
  457. });
  458. /// End payment --------
  459. })
  460. .catch(err => {
  461. that.setData({
  462. canSpell: true,
  463. canBuyIf: true
  464. })
  465. wx.showToast({
  466. title: err.message,
  467. icon: 'none',
  468. duration: 2000,
  469. mask: false
  470. });
  471. })
  472. }
  473. })
  474. },
  475. /**
  476. * 生命周期函数--监听页面初次渲染完成
  477. */
  478. onReady: function () {
  479. },
  480. /**
  481. * 生命周期函数--监听页面显示
  482. */
  483. onShow: function () {
  484. this.setData({
  485. canSpell: true,
  486. canBuyIf: true,
  487. showTime: true
  488. })
  489. if (this.data.spellData != null) {
  490. this.countdown()
  491. }
  492. },
  493. /**
  494. * 生命周期函数--监听页面隐藏
  495. */
  496. onHide: function () {
  497. this.setData({
  498. showTime: false
  499. })
  500. },
  501. /**
  502. * 生命周期函数--监听页面卸载
  503. */
  504. onUnload: function () {
  505. this.setData({
  506. showTime: false
  507. })
  508. },
  509. /**
  510. * 页面相关事件处理函数--监听用户下拉动作
  511. */
  512. onPullDownRefresh: function () {
  513. this.checkUser(this.data.paramData)
  514. },
  515. /**
  516. * 页面上拉触底事件的处理函数
  517. */
  518. onReachBottom: function () {
  519. }
  520. })