C端小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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