C端小程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

491 satır
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. home: imgurl.home.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. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad: function (options) {
  32. this.setData({
  33. paramData:options,
  34. couponChannelId: options.couponChannelId,
  35. couponId: options.couponId,
  36. orderGroupId: options.orderGroupId,
  37. orderId:options.orderId,
  38. avatarUrl: options.avatarUrl,
  39. nickName: options.nickName,
  40. })
  41. this.checkUser(options);
  42. },
  43. gotoIndex(){
  44. wx.reLaunch({
  45. url: '/pages/index/index',
  46. })
  47. },
  48. /**
  49. * 判断用户是否已经在团中
  50. */
  51. checkUser(options){
  52. let that = this;
  53. Http.get({
  54. url: config.api.queryAttendStatus,
  55. data: {
  56. id: options.orderGroupId,
  57. }
  58. }).then(res => {
  59. wx.stopPullDownRefresh();
  60. if (res.data.attend == true || res.data.status!=10) {
  61. wx.redirectTo({
  62. url: `/pages/spellDetail/index?orderId=${options.orderId}&couponId=${options.couponId}&orderGroupId=${options.orderGroupId}&couponChannelId=${options.couponChannelId}`,
  63. })
  64. }else{
  65. that.getDetail(that.data.couponChannelId);
  66. that.getOneSpell(that.data.couponId)
  67. }
  68. });
  69. },
  70. /**
  71. * 拨打电话
  72. */
  73. phone: function (e) {
  74. let that = this;
  75. wx.makePhoneCall({
  76. phoneNumber: e.target.dataset.merchantlinkphone
  77. });
  78. },
  79. /**
  80. * 直接购买
  81. */
  82. gotoBuy() {
  83. console.log(22222)
  84. this.setData({
  85. canBuyIf: false
  86. })
  87. this.orderFunc()
  88. },
  89. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  90. dateformat(micro_second) {
  91. // 总秒数
  92. var second = Math.floor(micro_second / 1000);
  93. // 天数
  94. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  95. // 小时
  96. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  97. // 分钟
  98. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  99. // 秒
  100. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  101. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  102. return {
  103. a1: day,
  104. b1: hr,
  105. c1: min,
  106. d1: sec
  107. }
  108. },
  109. countdown(end_time) {
  110. let that = this;
  111. var EndTime = end_time;
  112. var NowTime = new Date().getTime();
  113. var total_micro_second = EndTime - NowTime || [];
  114. // 渲染倒计时时钟
  115. let obj = that.dateformat(total_micro_second);
  116. if (total_micro_second > 0) {
  117. that.setData({
  118. clock: obj,
  119. day: obj.a1,
  120. hour: obj.b1,
  121. min: obj.c1,
  122. sec: obj.d1,
  123. })
  124. } else {
  125. that.setData({
  126. clock: "00",
  127. day: "0",
  128. hour: "00",
  129. min: "00",
  130. sec: "00",
  131. })
  132. }
  133. setTimeout(function () {
  134. total_micro_second -= 1000;
  135. that.countdown(end_time);
  136. }, 1000)
  137. },
  138. /**
  139. * 发起拼团
  140. */
  141. gotoSpell() {
  142. console.log(111111111)
  143. this.setData({
  144. canSpell: false
  145. })
  146. this.orderFunc(this.data.orderGroupId)
  147. },
  148. /**
  149. * 获取一个拼团信息
  150. */
  151. getOneSpell(couponId) {
  152. let that = this;
  153. Http.get({
  154. url: config.api.queryOrderGroupStatus,
  155. data: {
  156. couponId: couponId,
  157. id: that.data.orderGroupId,
  158. orderId: that.data.orderId
  159. }
  160. }).then(res => {
  161. if (res.data) {
  162. that.countdown(res.data.expiredDate);
  163. that.setData({
  164. spellData: res.data
  165. });
  166. }
  167. });
  168. },
  169. /**
  170. * 获取券详情信息
  171. */
  172. getDetail(couponChannelId) {
  173. let that = this;
  174. Http.get({
  175. url: config.api.couponDetail,
  176. data: {
  177. couponChannelId: couponChannelId
  178. }
  179. }).then(res => {
  180. let data = res.data;
  181. data.price = (data.price / 100).toFixed(2)
  182. data.salePrice = (data.salePrice / 100).toFixed(2)
  183. data.validStartDate = utils.formatTime(data.validStartDate, 'yyyy-MM-dd')
  184. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  185. that.setData({
  186. data
  187. });
  188. });
  189. },
  190. /**
  191. * 去拼团
  192. */
  193. goToOrderGroup(orderId, orderGroupId, _this) {
  194. console.log(22222222)
  195. let that = this;
  196. // 支付成功
  197. Http.post({
  198. url: config.api.toOrderGroup,
  199. data: {
  200. id: orderGroupId,
  201. orderId,
  202. couponId: _this.data.data.couponId
  203. }
  204. })
  205. .then(res => {
  206. if(res.code=200){
  207. wx.reLaunch({
  208. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}`
  209. });
  210. } else if (res.code == 28001){
  211. _this.setData({
  212. showErr:true
  213. })
  214. }
  215. })
  216. .catch(err => {
  217. console.log(err);
  218. })
  219. // return;
  220. },
  221. /**
  222. * 支付订单更新
  223. */
  224. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  225. let that = this;
  226. // 支付成功
  227. Http.post({
  228. url: config.api.payOrderUpdate,
  229. data: {
  230. payOrderId: payOrderId,
  231. orderId: orderId,
  232. status: status,
  233. reason: reason
  234. }
  235. })
  236. .then(res => {
  237. wx.hideLoading()
  238. if (orderGroupId==undefined) {
  239. wx.navigateTo({
  240. url: '/pages/order/detail/index?orderId=' + res.data.id,
  241. })
  242. } else {
  243. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  244. }
  245. })
  246. .catch(err => {
  247. console.log(err);
  248. if (err.code != 12002) {
  249. setTimeout(function () {
  250. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  251. }, 2000)
  252. }
  253. })
  254. },
  255. /**
  256. * 发起支付
  257. */
  258. orderFunc(orderGroupId) {
  259. let data = {
  260. couponChannelId: this.data.data.id,
  261. couponId: this.data.data.couponId
  262. }
  263. /**
  264. * 拼团订单
  265. */
  266. data.orderGroupId = orderGroupId
  267. var that = this;
  268. Http.post({
  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. console.log(that.data, 444)
  351. wx.redirectTo({
  352. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  353. that.data.couponChannelId + '&orderGroupId=' + that.data.orderGroupId+ '&couponId=' + that.data.couponId + '&orderId=' + that.data.orderId + '&avatarUrl=' + that.data.avatarUrl + '&nickName=' + that.data.nickName
  354. });
  355. } else if (err.code == 11006) {
  356. // 用户手机已加密
  357. wx.redirectTo({
  358. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  359. that.data.couponChannelId + '&orderGroupId=' + that.data.orderGroupId + '&couponId=' + that.data.couponId + '&orderId=' + that.data.orderId + '&avatarUrl=' + that.data.avatarUrl + '&nickName=' + that.data.nickName
  360. });
  361. } else {
  362. wx.showToast({
  363. title: err.message,
  364. icon: 'none',
  365. duration: 2000,
  366. mask: false
  367. });
  368. }
  369. })
  370. .then(res => {
  371. console.log(res)
  372. if (typeof (res) != "undefined") {
  373. let orderId = "" + res.data.id;
  374. that.setData({
  375. orderId: orderId,
  376. canSpell: true,
  377. canBuyIf: true
  378. });
  379. // 支付金额不为0
  380. /**
  381. * 支付订单创建
  382. */
  383. Http.post({
  384. url: config.api.payOrderCreate,
  385. data: {
  386. orderId: orderId
  387. }
  388. })
  389. .then(res => {
  390. var payOrderId = "" + res.data.payOrderId;
  391. wx.hideLoading();
  392. wx.requestPayment({
  393. timeStamp: res.data.timeStamp,
  394. nonceStr: res.data.nonceStr,
  395. package: res.data.package,
  396. signType: (res.data.signType) ? res.data.signType : "MD5",
  397. paySign: res.data.paySign,
  398. success: res => {
  399. wx.showLoading({
  400. title: '订单正在处理中...',
  401. })
  402. setTimeout(function () {
  403. wx.hideLoading()
  404. }, 5000)
  405. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  406. if (res.errMsg == "requestPayment:ok") {
  407. setTimeout(function () {
  408. wx.hideLoading();
  409. }, 2000);
  410. }
  411. },
  412. fail: res => {
  413. /**
  414. * 支付失败,需要更新订单的状态
  415. */
  416. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  417. that.setData({
  418. showbutton: false,
  419. canSpell: true,
  420. canBuyIf: true
  421. })
  422. return;
  423. },
  424. complete: res => { }
  425. });
  426. /// End payment --------
  427. })
  428. .catch(err => {
  429. that.setData({
  430. canSpell: true,
  431. canBuyIf: true
  432. })
  433. wx.showToast({
  434. title: err.message,
  435. icon: 'none',
  436. duration: 2000,
  437. mask: false
  438. });
  439. })
  440. }
  441. })
  442. },
  443. /**
  444. * 生命周期函数--监听页面初次渲染完成
  445. */
  446. onReady: function () {
  447. },
  448. /**
  449. * 生命周期函数--监听页面显示
  450. */
  451. onShow: function () {
  452. this.setData({
  453. canSpell: true,
  454. canBuyIf: true
  455. })
  456. },
  457. /**
  458. * 生命周期函数--监听页面隐藏
  459. */
  460. onHide: function () {
  461. },
  462. /**
  463. * 生命周期函数--监听页面卸载
  464. */
  465. onUnload: function () {
  466. },
  467. /**
  468. * 页面相关事件处理函数--监听用户下拉动作
  469. */
  470. onPullDownRefresh: function () {
  471. this.checkUser(this.data.paramData)
  472. },
  473. /**
  474. * 页面上拉触底事件的处理函数
  475. */
  476. onReachBottom: function () {
  477. }
  478. })