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

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