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.

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