C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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