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.

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