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