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