C端小程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

487 řádky
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. 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. })
  280. if (err.code == 2011) {
  281. wx.showToast({
  282. title: "商户信息没找到",
  283. image: './../../assets/images/fail.png',
  284. duration: 2000,
  285. mask: false
  286. });
  287. } else if (err.code == 2013) {
  288. wx.showToast({
  289. title: "商户信息禁用",
  290. image: './../../assets/images/fail.png',
  291. duration: 2000,
  292. mask: false
  293. });
  294. } else if (err.code == 3000) {
  295. wx.showToast({
  296. title: "库存不足",
  297. image: './../../assets/images/fail.png',
  298. duration: 2000,
  299. mask: false
  300. });
  301. } else if (err.code == 3001) {
  302. wx.showToast({
  303. title: "超过限购条件",
  304. image: './../../assets/images/fail.png',
  305. duration: 2000,
  306. mask: false
  307. });
  308. } else if (err.code == 3002) {
  309. wx.showToast({
  310. title: "订单失败",
  311. image: './../../assets/images/fail.png',
  312. duration: 2000,
  313. mask: false
  314. });
  315. } else if (err.code == 3003) {
  316. wx.showToast({
  317. title: "订单不存在",
  318. image: './../../assets/images/fail.png',
  319. duration: 2000,
  320. mask: false
  321. });
  322. } else if (err.code == 3004) {
  323. wx.showToast({
  324. title: "订单不存在",
  325. image: './../../assets/images/fail.png',
  326. duration: 2000,
  327. mask: false
  328. });
  329. } else if (err.code == 4003) {
  330. wx.showToast({
  331. title: "卡券已作废",
  332. image: './../../assets/images/fail.png',
  333. duration: 2000,
  334. mask: false
  335. });
  336. } else if (err.code == 11005) {
  337. /**
  338. * 将值传到用户手机号授权的页面
  339. *
  340. */
  341. wx.redirectTo({
  342. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  343. that.data.couponChannelId + '&couponId=' + that.data.couponId
  344. });
  345. } else if (err.code == 11006) {
  346. // 用户手机已加密
  347. wx.redirectTo({
  348. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  349. that.data.couponChannelId + '&couponId=' + that.data.couponId
  350. });
  351. } else {
  352. wx.showToast({
  353. title: err.message,
  354. icon: 'none',
  355. duration: 2000,
  356. mask: false
  357. });
  358. }
  359. })
  360. .then(res => {
  361. console.log(res)
  362. if (typeof (res) != "undefined") {
  363. let orderId = "" + res.data.id;
  364. that.setData({
  365. orderId: orderId
  366. });
  367. // 支付金额不为0
  368. /**
  369. * 支付订单创建
  370. */
  371. Http.post({
  372. url: config.api.payOrderCreate,
  373. data: {
  374. orderId: orderId
  375. }
  376. })
  377. .then(res => {
  378. var payOrderId = "" + res.data.payOrderId;
  379. wx.hideLoading();
  380. wx.requestPayment({
  381. timeStamp: res.data.timeStamp,
  382. nonceStr: res.data.nonceStr,
  383. package: res.data.package,
  384. signType: (res.data.signType) ? res.data.signType : "MD5",
  385. paySign: res.data.paySign,
  386. success: res => {
  387. wx.showLoading({
  388. title: '订单正在处理中...',
  389. })
  390. setTimeout(function () {
  391. wx.hideLoading()
  392. }, 5000)
  393. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  394. if (res.errMsg == "requestPayment:ok") {
  395. setTimeout(function () {
  396. wx.hideLoading();
  397. }, 2000);
  398. /**
  399. * 用户支付成功以后跳转到券包列表
  400. */
  401. if (that.data.cardType == 100) {
  402. wx.setStorage({
  403. key: 'couponNum2',
  404. data: "couponNum2"
  405. })
  406. } else if (that.data.data.type != 5) {
  407. wx.setStorage({
  408. key: 'couponNum',
  409. data: "couponNum"
  410. })
  411. }
  412. }
  413. },
  414. fail: res => {
  415. /**
  416. * 支付失败,需要更新订单的状态
  417. */
  418. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  419. that.setData({
  420. showbutton: false
  421. })
  422. return;
  423. },
  424. complete: res => { }
  425. });
  426. /// End payment --------
  427. })
  428. .catch(err => {
  429. wx.showToast({
  430. title: err.message,
  431. icon: 'none',
  432. duration: 2000,
  433. mask: false
  434. });
  435. })
  436. }
  437. })
  438. },
  439. /**
  440. * 生命周期函数--监听页面初次渲染完成
  441. */
  442. onReady: function () {
  443. },
  444. /**
  445. * 生命周期函数--监听页面显示
  446. */
  447. onShow: function () {
  448. this.setData({
  449. canSpell: true,
  450. canBuyIf: true
  451. })
  452. },
  453. /**
  454. * 生命周期函数--监听页面隐藏
  455. */
  456. onHide: function () {
  457. },
  458. /**
  459. * 生命周期函数--监听页面卸载
  460. */
  461. onUnload: function () {
  462. },
  463. /**
  464. * 页面相关事件处理函数--监听用户下拉动作
  465. */
  466. onPullDownRefresh: function () {
  467. },
  468. /**
  469. * 页面上拉触底事件的处理函数
  470. */
  471. onReachBottom: function () {
  472. }
  473. })