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.

508 lines
13 KiB

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