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.

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