C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

530 lines
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. let formId = e.detail.formId;
  87. let that=this;
  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. this.setData({
  158. canSpell: false
  159. })
  160. this.orderFunc(this.data.orderGroupId)
  161. },
  162. /**
  163. * 获取一个拼团信息
  164. */
  165. getOneSpell(couponId) {
  166. let that = this;
  167. Http.get({
  168. url: config.api.queryOrderGroupStatus,
  169. data: {
  170. couponId: couponId,
  171. id: that.data.orderGroupId,
  172. orderId: that.data.orderId
  173. }
  174. }).then(res => {
  175. if (res.data) {
  176. that.countdown(res.data.expiredDate);
  177. that.setData({
  178. spellData: res.data
  179. });
  180. }
  181. });
  182. },
  183. /**
  184. * 获取券详情信息
  185. */
  186. getDetail(couponChannelId) {
  187. let that = this;
  188. Http.get({
  189. url: config.api.couponDetail,
  190. data: {
  191. couponChannelId: couponChannelId
  192. }
  193. }).then(res => {
  194. let data = res.data;
  195. data.price = (data.price / 100).toFixed(2)
  196. data.salePrice = (data.salePrice / 100).toFixed(2)
  197. data.validStartDate = utils.formatTime(data.validStartDate, 'yyyy-MM-dd')
  198. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  199. that.setData({
  200. data
  201. });
  202. });
  203. },
  204. /**
  205. * 去拼团
  206. */
  207. goToOrderGroup(orderId, orderGroupId, _this) {
  208. let that = this;
  209. // 支付成功
  210. Http.post({
  211. url: config.api.toOrderGroup,
  212. data: {
  213. id: orderGroupId,
  214. orderId,
  215. couponId: _this.data.data.couponId
  216. }
  217. })
  218. .then(res => {
  219. wx.reLaunch({
  220. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}&couponChannelId=${_this.data.couponChannelId}`
  221. });
  222. })
  223. .catch(err => {
  224. console.log(err);
  225. })
  226. // return;
  227. },
  228. /**
  229. * 支付订单更新
  230. */
  231. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  232. // 支付成功
  233. Http.post({
  234. url: config.api.payOrderUpdate,
  235. data: {
  236. payOrderId: payOrderId,
  237. orderId: orderId,
  238. status: status,
  239. reason: reason
  240. }
  241. })
  242. .then(res => {
  243. wx.hideLoading()
  244. if (res.data.orderStatus==14){
  245. _this.setData({
  246. showErr:true
  247. })
  248. return;
  249. }
  250. if (orderGroupId==undefined) {
  251. wx.navigateTo({
  252. url: '/pages/order/detail/index?orderId=' + res.data.id,
  253. })
  254. } else {
  255. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  256. }
  257. })
  258. .catch(err => {
  259. console.log(err);
  260. if (err.code != 12002) {
  261. setTimeout(function () {
  262. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  263. }, 2000)
  264. }
  265. })
  266. },
  267. /**
  268. * 发起支付
  269. */
  270. orderFunc(orderGroupId) {
  271. let data = {
  272. couponChannelId: this.data.data.id,
  273. couponId: this.data.data.couponId,
  274. orderId:this.data.formId
  275. }
  276. /**
  277. * 拼团订单
  278. */
  279. data.orderGroupId = orderGroupId
  280. var that = this;
  281. Http.get({
  282. url: config.api.checkPhoneStatus,
  283. data: {}
  284. })
  285. .then(res => {
  286. /**
  287. * orderSave 下单
  288. */
  289. return Http.post({
  290. url: config.api.orderSave,
  291. data: data
  292. });
  293. })
  294. .catch(err => {
  295. console.log(err);
  296. that.setData({
  297. showbutton: false,
  298. showbutton1: false,
  299. canSpell: true,
  300. canBuyIf: true
  301. })
  302. if (err.code == 2011) {
  303. wx.showToast({
  304. title: "商户信息没找到",
  305. image: './../../assets/images/fail.png',
  306. duration: 2000,
  307. mask: false
  308. });
  309. } else if (err.code == 2013) {
  310. wx.showToast({
  311. title: "商户信息禁用",
  312. image: './../../assets/images/fail.png',
  313. duration: 2000,
  314. mask: false
  315. });
  316. } else if (err.code == 3000) {
  317. wx.showToast({
  318. title: "库存不足",
  319. image: './../../assets/images/fail.png',
  320. duration: 2000,
  321. mask: false
  322. });
  323. } else if (err.code == 3001) {
  324. wx.showToast({
  325. title: "您已超过限购",
  326. image: './../../assets/images/fail.png',
  327. duration: 2000,
  328. mask: false
  329. });
  330. } else if (err.code == 3002) {
  331. wx.showToast({
  332. title: "订单失败",
  333. image: './../../assets/images/fail.png',
  334. duration: 2000,
  335. mask: false
  336. });
  337. } else if (err.code == 3003) {
  338. wx.showToast({
  339. title: "订单不存在",
  340. image: './../../assets/images/fail.png',
  341. duration: 2000,
  342. mask: false
  343. });
  344. } else if (err.code == 3004) {
  345. wx.showToast({
  346. title: "订单不存在",
  347. image: './../../assets/images/fail.png',
  348. duration: 2000,
  349. mask: false
  350. });
  351. } else if (err.code == 4003) {
  352. wx.showToast({
  353. title: "卡券已作废",
  354. image: './../../assets/images/fail.png',
  355. duration: 2000,
  356. mask: false
  357. });
  358. } else if (err.code == 3012) {
  359. wx.showModal({
  360. title: '提示',
  361. content: '您有未支付订单,请先进行支付',
  362. confirmText: "去支付",
  363. success: function (res) {
  364. console.log(res.confirm)
  365. if (res.confirm) {
  366. wx.navigateTo({
  367. url: '/pages/order/index/index?id=all',
  368. })
  369. }
  370. }
  371. })
  372. } else if (err.code == 11005) {
  373. /**
  374. * 将值传到用户手机号授权的页面
  375. *
  376. */
  377. console.log(that.data, 444)
  378. wx.redirectTo({
  379. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  380. that.data.couponChannelId + '&orderGroupId=' + that.data.orderGroupId+ '&couponId=' + that.data.couponId + '&orderId=' + that.data.orderId + '&avatarUrl=' + that.data.avatarUrl + '&nickName=' + that.data.nickName
  381. });
  382. } else if (err.code == 11006) {
  383. // 用户手机已加密
  384. wx.redirectTo({
  385. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  386. that.data.couponChannelId + '&orderGroupId=' + that.data.orderGroupId + '&couponId=' + that.data.couponId + '&orderId=' + that.data.orderId + '&avatarUrl=' + that.data.avatarUrl + '&nickName=' + that.data.nickName
  387. });
  388. } else {
  389. wx.showToast({
  390. title: err.message,
  391. icon: 'none',
  392. duration: 2000,
  393. mask: false
  394. });
  395. }
  396. })
  397. .then(res => {
  398. console.log(res)
  399. if (typeof (res) != "undefined") {
  400. let orderId = "" + res.data.id;
  401. that.setData({
  402. orderId: orderId,
  403. canSpell: false,
  404. canBuyIf: true
  405. });
  406. // 支付金额不为0
  407. /**
  408. * 支付订单创建
  409. */
  410. Http.post({
  411. url: config.api.payOrderCreate,
  412. data: {
  413. orderId: orderId
  414. }
  415. })
  416. .then(res => {
  417. var payOrderId = "" + res.data.payOrderId;
  418. wx.hideLoading();
  419. wx.requestPayment({
  420. timeStamp: res.data.timeStamp,
  421. nonceStr: res.data.nonceStr,
  422. package: res.data.package,
  423. signType: (res.data.signType) ? res.data.signType : "MD5",
  424. paySign: res.data.paySign,
  425. success: res => {
  426. wx.showLoading({
  427. title: '订单正在处理中...',
  428. })
  429. that.setData({
  430. canSpell: false
  431. })
  432. setTimeout(function () {
  433. wx.hideLoading()
  434. }, 5000)
  435. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  436. if (res.errMsg == "requestPayment:ok") {
  437. setTimeout(function () {
  438. wx.hideLoading();
  439. }, 2000);
  440. }
  441. },
  442. fail: res => {
  443. /**
  444. * 支付失败,需要更新订单的状态
  445. */
  446. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  447. that.setData({
  448. showbutton: false,
  449. canSpell: true,
  450. canBuyIf: true
  451. })
  452. return;
  453. },
  454. complete: res => { }
  455. });
  456. /// End payment --------
  457. })
  458. .catch(err => {
  459. that.setData({
  460. canSpell: true,
  461. canBuyIf: true
  462. })
  463. wx.showToast({
  464. title: err.message,
  465. icon: 'none',
  466. duration: 2000,
  467. mask: false
  468. });
  469. })
  470. }
  471. })
  472. },
  473. /**
  474. * 生命周期函数--监听页面初次渲染完成
  475. */
  476. onReady: function () {
  477. },
  478. /**
  479. * 生命周期函数--监听页面显示
  480. */
  481. onShow: function () {
  482. this.setData({
  483. canSpell: true,
  484. canBuyIf: true,
  485. showTime: true
  486. })
  487. if (this.data.spellData != null) {
  488. this.countdown()
  489. }
  490. },
  491. /**
  492. * 生命周期函数--监听页面隐藏
  493. */
  494. onHide: function () {
  495. this.setData({
  496. showTime: false
  497. })
  498. },
  499. /**
  500. * 生命周期函数--监听页面卸载
  501. */
  502. onUnload: function () {
  503. this.setData({
  504. showTime: false
  505. })
  506. },
  507. /**
  508. * 页面相关事件处理函数--监听用户下拉动作
  509. */
  510. onPullDownRefresh: function () {
  511. this.checkUser(this.data.paramData)
  512. },
  513. /**
  514. * 页面上拉触底事件的处理函数
  515. */
  516. onReachBottom: function () {
  517. }
  518. })