C端小程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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