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.

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