C端小程序
Não pode escolher mais do que 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.

497 linhas
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. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. teljpgUrl: imgurl.teljpg.url,
  11. home: imgurl.home.url,
  12. couponChannelId: '',
  13. couponId: '',
  14. data: [],
  15. spellData: null,
  16. canSpell: true,
  17. canBuyIf: true,
  18. clock: "00",
  19. day: "00",
  20. hour: "00",
  21. min: "00",
  22. sec: "00",
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function (options) {
  28. console.log(options,22)
  29. this.setData({
  30. couponChannelId: options.couponChannelId,
  31. couponId: options.couponId,
  32. orderGroupId: options.orderGroupId,
  33. orderId:options.orderId,
  34. avatarUrl: options.avatarUrl,
  35. nickName: options.nickName,
  36. })
  37. this.checkUser(options);
  38. },
  39. gotoIndex(){
  40. wx.reLaunch({
  41. url: '/pages/index/index',
  42. })
  43. },
  44. /**
  45. * 判断用户是否已经在团中
  46. */
  47. checkUser(options){
  48. let that = this;
  49. Http.get({
  50. url: config.api.queryAttendStatus,
  51. data: {
  52. id: options.orderGroupId,
  53. }
  54. }).then(res => {
  55. if (res.data.attend == true || res.data.status!=10) {
  56. wx.redirectTo({
  57. url: `/pages/spellDetail/index?orderId=${options.orderId}&couponId=${options.couponId}&orderGroupId=${options.orderGroupId}&couponChannelId=${options.couponChannelId}`,
  58. })
  59. }else{
  60. that.getDetail(that.data.couponChannelId);
  61. that.getOneSpell(that.data.couponId)
  62. }
  63. });
  64. },
  65. /**
  66. * 拨打电话
  67. */
  68. phone: function (e) {
  69. let that = this;
  70. wx.makePhoneCall({
  71. phoneNumber: e.target.dataset.merchantlinkphone
  72. });
  73. },
  74. /**
  75. * 直接购买
  76. */
  77. gotoBuy() {
  78. console.log(22222)
  79. this.setData({
  80. canBuyIf: false
  81. })
  82. this.orderFunc()
  83. },
  84. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  85. dateformat(micro_second) {
  86. // 总秒数
  87. var second = Math.floor(micro_second / 1000);
  88. // 天数
  89. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  90. // 小时
  91. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  92. // 分钟
  93. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  94. // 秒
  95. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  96. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  97. return {
  98. a1: day,
  99. b1: hr,
  100. c1: min,
  101. d1: sec
  102. }
  103. },
  104. countdown(end_time) {
  105. let that = this;
  106. var EndTime = end_time;
  107. var NowTime = new Date().getTime();
  108. var total_micro_second = EndTime - NowTime || [];
  109. // 渲染倒计时时钟
  110. let obj = that.dateformat(total_micro_second);
  111. if (total_micro_second > 0) {
  112. that.setData({
  113. clock: obj,
  114. day: obj.a1,
  115. hour: obj.b1,
  116. min: obj.c1,
  117. sec: obj.d1,
  118. })
  119. } else {
  120. that.setData({
  121. clock: "00",
  122. day: "00",
  123. hour: "00",
  124. min: "00",
  125. sec: "00",
  126. })
  127. }
  128. setTimeout(function () {
  129. total_micro_second -= 1000;
  130. that.countdown(end_time);
  131. }, 1000)
  132. },
  133. /**
  134. * 发起拼团
  135. */
  136. gotoSpell() {
  137. console.log(111111111)
  138. this.setData({
  139. canSpell: false
  140. })
  141. this.orderFunc(this.data.orderGroupId)
  142. },
  143. //参与别人的拼团
  144. gotoPartner() {
  145. wx.navigateTo({
  146. url: `/pages/spellDetail/index?orderId=${this.data.spellData.orderId}&couponId=${this.data.spellData.couponId}&orderGroupId=${this.data.spellData.orderGroupId}`
  147. });
  148. },
  149. /**
  150. * 获取一个拼团信息
  151. */
  152. getOneSpell(couponId) {
  153. let that = this;
  154. Http.get({
  155. url: config.api.queryOrderGroupStatus,
  156. data: {
  157. couponId: couponId,
  158. id: that.data.orderGroupId,
  159. orderId: that.data.orderId
  160. }
  161. }).then(res => {
  162. if (res.data) {
  163. that.countdown(res.data.expiredDate);
  164. that.setData({
  165. spellData: res.data
  166. });
  167. }
  168. });
  169. },
  170. /**
  171. * 获取券详情信息
  172. */
  173. getDetail(couponChannelId) {
  174. let that = this;
  175. Http.get({
  176. url: config.api.couponDetail,
  177. data: {
  178. couponChannelId: couponChannelId
  179. }
  180. }).then(res => {
  181. let data = res.data;
  182. data.price = (data.price / 100).toFixed(2)
  183. data.salePrice = (data.salePrice / 100).toFixed(2)
  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.navigateTo({
  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
  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
  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. */
  405. if (that.data.cardType == 100) {
  406. wx.setStorage({
  407. key: 'couponNum2',
  408. data: "couponNum2"
  409. })
  410. } else if (that.data.data.type != 5) {
  411. wx.setStorage({
  412. key: 'couponNum',
  413. data: "couponNum"
  414. })
  415. }
  416. }
  417. },
  418. fail: res => {
  419. /**
  420. * 支付失败,需要更新订单的状态
  421. */
  422. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  423. that.setData({
  424. showbutton: false,
  425. canSpell: true,
  426. canBuyIf: true
  427. })
  428. return;
  429. },
  430. complete: res => { }
  431. });
  432. /// End payment --------
  433. })
  434. .catch(err => {
  435. that.setData({
  436. canSpell: true,
  437. canBuyIf: true
  438. })
  439. wx.showToast({
  440. title: err.message,
  441. icon: 'none',
  442. duration: 2000,
  443. mask: false
  444. });
  445. })
  446. }
  447. })
  448. },
  449. /**
  450. * 生命周期函数--监听页面初次渲染完成
  451. */
  452. onReady: function () {
  453. },
  454. /**
  455. * 生命周期函数--监听页面显示
  456. */
  457. onShow: function () {
  458. this.setData({
  459. canSpell: true,
  460. canBuyIf: true
  461. })
  462. },
  463. /**
  464. * 生命周期函数--监听页面隐藏
  465. */
  466. onHide: function () {
  467. },
  468. /**
  469. * 生命周期函数--监听页面卸载
  470. */
  471. onUnload: function () {
  472. },
  473. /**
  474. * 页面相关事件处理函数--监听用户下拉动作
  475. */
  476. onPullDownRefresh: function () {
  477. },
  478. /**
  479. * 页面上拉触底事件的处理函数
  480. */
  481. onReachBottom: function () {
  482. }
  483. })