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

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