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ů.

483 řá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. 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. this.setData({
  31. paramData:options,
  32. couponChannelId: options.couponChannelId,
  33. couponId: options.couponId,
  34. orderGroupId: options.orderGroupId,
  35. orderId:options.orderId,
  36. avatarUrl: options.avatarUrl,
  37. nickName: options.nickName,
  38. })
  39. this.checkUser(options);
  40. },
  41. gotoIndex(){
  42. wx.reLaunch({
  43. url: '/pages/index/index',
  44. })
  45. },
  46. /**
  47. * 判断用户是否已经在团中
  48. */
  49. checkUser(options){
  50. let that = this;
  51. Http.get({
  52. url: config.api.queryAttendStatus,
  53. data: {
  54. id: options.orderGroupId,
  55. }
  56. }).then(res => {
  57. wx.stopPullDownRefresh();
  58. if (res.data.attend == true || res.data.status!=10) {
  59. wx.redirectTo({
  60. url: `/pages/spellDetail/index?orderId=${options.orderId}&couponId=${options.couponId}&orderGroupId=${options.orderGroupId}&couponChannelId=${options.couponChannelId}`,
  61. })
  62. }else{
  63. that.getDetail(that.data.couponChannelId);
  64. that.getOneSpell(that.data.couponId)
  65. }
  66. });
  67. },
  68. /**
  69. * 拨打电话
  70. */
  71. phone: function (e) {
  72. let that = this;
  73. wx.makePhoneCall({
  74. phoneNumber: e.target.dataset.merchantlinkphone
  75. });
  76. },
  77. /**
  78. * 直接购买
  79. */
  80. gotoBuy() {
  81. console.log(22222)
  82. this.setData({
  83. canBuyIf: false
  84. })
  85. this.orderFunc()
  86. },
  87. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  88. dateformat(micro_second) {
  89. // 总秒数
  90. var second = Math.floor(micro_second / 1000);
  91. // 天数
  92. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  93. // 小时
  94. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  95. // 分钟
  96. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  97. // 秒
  98. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  99. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  100. return {
  101. a1: day,
  102. b1: hr,
  103. c1: min,
  104. d1: sec
  105. }
  106. },
  107. countdown(end_time) {
  108. let that = this;
  109. var EndTime = end_time;
  110. var NowTime = new Date().getTime();
  111. var total_micro_second = EndTime - NowTime || [];
  112. // 渲染倒计时时钟
  113. let obj = that.dateformat(total_micro_second);
  114. if (total_micro_second > 0) {
  115. that.setData({
  116. clock: obj,
  117. day: obj.a1,
  118. hour: obj.b1,
  119. min: obj.c1,
  120. sec: obj.d1,
  121. })
  122. } else {
  123. that.setData({
  124. clock: "00",
  125. day: "0",
  126. hour: "00",
  127. min: "00",
  128. sec: "00",
  129. })
  130. }
  131. setTimeout(function () {
  132. total_micro_second -= 1000;
  133. that.countdown(end_time);
  134. }, 1000)
  135. },
  136. /**
  137. * 发起拼团
  138. */
  139. gotoSpell() {
  140. console.log(111111111)
  141. this.setData({
  142. canSpell: false
  143. })
  144. this.orderFunc(this.data.orderGroupId)
  145. },
  146. /**
  147. * 获取一个拼团信息
  148. */
  149. getOneSpell(couponId) {
  150. let that = this;
  151. Http.get({
  152. url: config.api.queryOrderGroupStatus,
  153. data: {
  154. couponId: couponId,
  155. id: that.data.orderGroupId,
  156. orderId: that.data.orderId
  157. }
  158. }).then(res => {
  159. if (res.data) {
  160. that.countdown(res.data.expiredDate);
  161. that.setData({
  162. spellData: res.data
  163. });
  164. }
  165. });
  166. },
  167. /**
  168. * 获取券详情信息
  169. */
  170. getDetail(couponChannelId) {
  171. let that = this;
  172. Http.get({
  173. url: config.api.couponDetail,
  174. data: {
  175. couponChannelId: couponChannelId
  176. }
  177. }).then(res => {
  178. let data = res.data;
  179. data.price = (data.price / 100).toFixed(2)
  180. data.salePrice = (data.salePrice / 100).toFixed(2)
  181. data.validStartDate = utils.formatTime(data.validStartDate, 'yyyy-MM-dd')
  182. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  183. that.setData({
  184. data
  185. });
  186. });
  187. },
  188. /**
  189. * 去拼团
  190. */
  191. goToOrderGroup(orderId, orderGroupId, _this) {
  192. console.log(22222222)
  193. let that = this;
  194. // 支付成功
  195. Http.post({
  196. url: config.api.toOrderGroup,
  197. data: {
  198. id: orderGroupId,
  199. orderId,
  200. couponId: _this.data.data.couponId
  201. }
  202. })
  203. .then(res => {
  204. wx.reLaunch({
  205. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}`
  206. });
  207. })
  208. .catch(err => {
  209. console.log(err);
  210. })
  211. // return;
  212. },
  213. /**
  214. * 支付订单更新
  215. */
  216. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  217. let that = this;
  218. // 支付成功
  219. Http.post({
  220. url: config.api.payOrderUpdate,
  221. data: {
  222. payOrderId: payOrderId,
  223. orderId: orderId,
  224. status: status,
  225. reason: reason
  226. }
  227. })
  228. .then(res => {
  229. wx.hideLoading()
  230. if (orderGroupId==undefined) {
  231. wx.navigateTo({
  232. url: '/pages/order/detail/index?orderId=' + res.data.id,
  233. })
  234. } else {
  235. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  236. }
  237. })
  238. .catch(err => {
  239. console.log(err);
  240. if (err.code != 12002) {
  241. setTimeout(function () {
  242. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  243. }, 2000)
  244. }
  245. })
  246. },
  247. /**
  248. * 发起支付
  249. */
  250. orderFunc(orderGroupId) {
  251. let data = {
  252. couponChannelId: this.data.data.id,
  253. couponId: this.data.data.couponId
  254. }
  255. /**
  256. * 拼团订单
  257. */
  258. data.orderGroupId = orderGroupId
  259. var that = this;
  260. Http.post({
  261. url: config.api.checkPhoneStatus,
  262. data: {}
  263. })
  264. .then(res => {
  265. /**
  266. * orderSave 下单
  267. */
  268. return Http.post({
  269. url: config.api.orderSave,
  270. data: data
  271. });
  272. })
  273. .catch(err => {
  274. console.log(err);
  275. that.setData({
  276. showbutton: false,
  277. showbutton1: false,
  278. canSpell: true,
  279. canBuyIf: true
  280. })
  281. if (err.code == 2011) {
  282. wx.showToast({
  283. title: "商户信息没找到",
  284. image: './../../assets/images/fail.png',
  285. duration: 2000,
  286. mask: false
  287. });
  288. } else if (err.code == 2013) {
  289. wx.showToast({
  290. title: "商户信息禁用",
  291. image: './../../assets/images/fail.png',
  292. duration: 2000,
  293. mask: false
  294. });
  295. } else if (err.code == 3000) {
  296. wx.showToast({
  297. title: "库存不足",
  298. image: './../../assets/images/fail.png',
  299. duration: 2000,
  300. mask: false
  301. });
  302. } else if (err.code == 3001) {
  303. wx.showToast({
  304. title: "超过限购条件",
  305. image: './../../assets/images/fail.png',
  306. duration: 2000,
  307. mask: false
  308. });
  309. } else if (err.code == 3002) {
  310. wx.showToast({
  311. title: "订单失败",
  312. image: './../../assets/images/fail.png',
  313. duration: 2000,
  314. mask: false
  315. });
  316. } else if (err.code == 3003) {
  317. wx.showToast({
  318. title: "订单不存在",
  319. image: './../../assets/images/fail.png',
  320. duration: 2000,
  321. mask: false
  322. });
  323. } else if (err.code == 3004) {
  324. wx.showToast({
  325. title: "订单不存在",
  326. image: './../../assets/images/fail.png',
  327. duration: 2000,
  328. mask: false
  329. });
  330. } else if (err.code == 4003) {
  331. wx.showToast({
  332. title: "卡券已作废",
  333. image: './../../assets/images/fail.png',
  334. duration: 2000,
  335. mask: false
  336. });
  337. } else if (err.code == 11005) {
  338. /**
  339. * 将值传到用户手机号授权的页面
  340. *
  341. */
  342. console.log(that.data, 444)
  343. wx.redirectTo({
  344. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  345. that.data.couponChannelId + '&orderGroupId=' + that.data.orderGroupId+ '&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 + '&orderGroupId=' + that.data.orderGroupId + '&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. })