C端小程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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