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.

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