C端小程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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