C端小程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

506 linhas
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. 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. that.setData({
  296. showbutton: false,
  297. showbutton1: false,
  298. canSpell: true,
  299. canBuyIf: true
  300. })
  301. if (err.code == 2011) {
  302. wx.showToast({
  303. title: "商户信息没找到",
  304. image: './../../assets/images/fail.png',
  305. duration: 2000,
  306. mask: false
  307. });
  308. } else if (err.code == 2013) {
  309. wx.showToast({
  310. title: "商户信息禁用",
  311. image: './../../assets/images/fail.png',
  312. duration: 2000,
  313. mask: false
  314. });
  315. } else if (err.code == 3000) {
  316. wx.showToast({
  317. title: "库存不足",
  318. image: './../../assets/images/fail.png',
  319. duration: 2000,
  320. mask: false
  321. });
  322. } else if (err.code == 3001) {
  323. wx.showToast({
  324. title: "您已超过限购",
  325. image: './../../assets/images/fail.png',
  326. duration: 2000,
  327. mask: false
  328. });
  329. } else if (err.code == 3002) {
  330. wx.showToast({
  331. title: "订单失败",
  332. image: './../../assets/images/fail.png',
  333. duration: 2000,
  334. mask: false
  335. });
  336. } else if (err.code == 3003) {
  337. wx.showToast({
  338. title: "订单不存在",
  339. image: './../../assets/images/fail.png',
  340. duration: 2000,
  341. mask: false
  342. });
  343. } else if (err.code == 3004) {
  344. wx.showToast({
  345. title: "订单不存在",
  346. image: './../../assets/images/fail.png',
  347. duration: 2000,
  348. mask: false
  349. });
  350. } else if (err.code == 4003) {
  351. wx.showToast({
  352. title: "卡券已作废",
  353. image: './../../assets/images/fail.png',
  354. duration: 2000,
  355. mask: false
  356. });
  357. } else if (err.code == 3012) {
  358. wx.showToast({
  359. title: err.message,
  360. icon: "none"
  361. })
  362. } else if (err.code == 11005) {
  363. /**
  364. * 将值传到用户手机号授权的页面
  365. *
  366. */
  367. console.log(that.data, 444)
  368. wx.redirectTo({
  369. url: "/pages/getphoneInfo/index?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 if (err.code == 11006) {
  373. // 用户手机已加密
  374. wx.redirectTo({
  375. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  376. that.data.couponChannelId + '&orderGroupId=' + that.data.orderGroupId + '&couponId=' + that.data.couponId + '&orderId=' + that.data.orderId + '&avatarUrl=' + that.data.avatarUrl + '&nickName=' + that.data.nickName
  377. });
  378. } else {
  379. wx.showToast({
  380. title: err.message,
  381. icon: 'none',
  382. duration: 2000,
  383. mask: false
  384. });
  385. }
  386. })
  387. .then(res => {
  388. console.log(res)
  389. if (typeof (res) != "undefined") {
  390. let orderId = "" + res.data.id;
  391. that.setData({
  392. orderId: orderId,
  393. canSpell: false,
  394. canBuyIf: true
  395. });
  396. // 支付金额不为0
  397. /**
  398. * 支付订单创建
  399. */
  400. Http.post({
  401. url: config.api.payOrderCreate,
  402. data: {
  403. orderId: orderId
  404. }
  405. })
  406. .then(res => {
  407. var payOrderId = "" + res.data.payOrderId;
  408. wx.hideLoading();
  409. wx.requestPayment({
  410. timeStamp: res.data.timeStamp,
  411. nonceStr: res.data.nonceStr,
  412. package: res.data.package,
  413. signType: (res.data.signType) ? res.data.signType : "MD5",
  414. paySign: res.data.paySign,
  415. success: res => {
  416. wx.showLoading({
  417. title: '订单正在处理中...',
  418. })
  419. that.setData({
  420. canSpell: false
  421. })
  422. setTimeout(function () {
  423. wx.hideLoading()
  424. }, 5000)
  425. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  426. if (res.errMsg == "requestPayment:ok") {
  427. setTimeout(function () {
  428. wx.hideLoading();
  429. }, 2000);
  430. }
  431. },
  432. fail: res => {
  433. /**
  434. * 支付失败,需要更新订单的状态
  435. */
  436. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  437. that.setData({
  438. showbutton: false,
  439. canSpell: true,
  440. canBuyIf: true
  441. })
  442. return;
  443. },
  444. complete: res => { }
  445. });
  446. /// End payment --------
  447. })
  448. .catch(err => {
  449. that.setData({
  450. canSpell: true,
  451. canBuyIf: true
  452. })
  453. wx.showToast({
  454. title: err.message,
  455. icon: 'none',
  456. duration: 2000,
  457. mask: false
  458. });
  459. })
  460. }
  461. })
  462. },
  463. /**
  464. * 生命周期函数--监听页面显示
  465. */
  466. onShow: function () {
  467. this.setData({
  468. canSpell: true,
  469. canBuyIf: true,
  470. showTime: true
  471. })
  472. if (this.data.spellData != null) {
  473. this.countdown()
  474. }
  475. },
  476. /**
  477. * 生命周期函数--监听页面隐藏
  478. */
  479. onHide: function () {
  480. this.setData({
  481. showTime: false
  482. })
  483. },
  484. /**
  485. * 生命周期函数--监听页面卸载
  486. */
  487. onUnload: function () {
  488. this.setData({
  489. showTime: false
  490. })
  491. },
  492. /**
  493. * 页面相关事件处理函数--监听用户下拉动作
  494. */
  495. onPullDownRefresh: function () {
  496. this.checkUser(this.data.paramData)
  497. }
  498. })