C端小程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

491 Zeilen
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. 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. showTime:true
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. this.setData({
  30. couponChannelId: options.couponChannelId,
  31. couponId: options.couponId
  32. })
  33. this.getDetail(options.couponChannelId);
  34. this.getOneSpell(options.couponId)
  35. this.getUserInfo();
  36. },
  37. getUserInfo: function () {
  38. let that = this;
  39. // 获取用户信息
  40. Http.get({
  41. url: config.api.getScore,
  42. data: {}
  43. })
  44. .then(res => {
  45. console.log(res)
  46. that.setData({
  47. nickName: res.data.nickName,
  48. avatarUrl: res.data.avatarUrl
  49. })
  50. })
  51. },
  52. /**
  53. * 拨打电话
  54. */
  55. phone: function (e) {
  56. let that = this;
  57. wx.makePhoneCall({
  58. phoneNumber: e.target.dataset.merchantlinkphone
  59. });
  60. },
  61. /**
  62. * 直接购买
  63. */
  64. gotoBuy(){
  65. console.log(22222)
  66. this.setData({
  67. canBuyIf: false
  68. })
  69. this.orderFunc()
  70. },
  71. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  72. dateformat(micro_second) {
  73. // 总秒数
  74. var second = Math.floor(micro_second / 1000);
  75. // 天数
  76. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  77. // 小时
  78. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  79. // 分钟
  80. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  81. // 秒
  82. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  83. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  84. return {
  85. a1: day,
  86. b1: hr,
  87. c1: min,
  88. d1: sec
  89. }
  90. },
  91. countdown(time) {
  92. let that = this;
  93. var EndTime = time;
  94. if (that.data.spellData!=null){
  95. EndTime = that.data.spellData.expiredDate;
  96. }
  97. var NowTime = new Date().getTime();
  98. var total_micro_second = EndTime - NowTime || [];
  99. // 渲染倒计时时钟
  100. let obj = that.dateformat(total_micro_second);
  101. console.log(obj)
  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. // spellData:null,
  113. clock: "00",
  114. day: "00",
  115. hour: "00",
  116. min: "00",
  117. sec: "00",
  118. })
  119. // that.getOneSpell(that.data.couponId)
  120. }
  121. if(that.data.showTime){
  122. setTimeout(function () {
  123. total_micro_second -= 1000;
  124. that.countdown();
  125. }, 1000)
  126. }
  127. },
  128. /**
  129. * 发起拼团
  130. */
  131. gotoSpell(){
  132. console.log(111111111)
  133. this.setData({
  134. canSpell:false
  135. })
  136. this.orderFunc(0)
  137. },
  138. //参与别人的拼团
  139. gotoPartner(){
  140. wx.navigateTo({
  141. url: `/pages/joinFrDpell/index?orderId=${this.data.spellData.orderId}&couponId=${this.data.spellData.couponId}&orderGroupId=${this.data.spellData.orderGroupId}&couponChannelId=${this.data.couponChannelId}&avatarUrl=${this.data.spellData.avatarUrl}&nickName=${this.data.spellData.nickName}`
  142. });
  143. },
  144. /**
  145. * 获取一个拼团信息
  146. */
  147. getOneSpell(couponId){
  148. let that = this;
  149. Http.get({
  150. url: config.api.queryRemainOne,
  151. data: {
  152. couponId: couponId
  153. }
  154. }).then(res => {
  155. wx.stopPullDownRefresh();
  156. if(res.data){
  157. that.countdown(res.data.expiredDate);
  158. that.setData({
  159. spellData: res.data
  160. });
  161. }
  162. });
  163. },
  164. /**
  165. * 获取券详情信息
  166. */
  167. getDetail(couponChannelId){
  168. let that = this;
  169. Http.get({
  170. url: config.api.couponDetail,
  171. data: {
  172. couponChannelId: couponChannelId
  173. }
  174. }).then(res => {
  175. wx.stopPullDownRefresh();
  176. let data = res.data;
  177. data.price = (data.price / 100).toFixed(2)
  178. data.salePrice = (data.salePrice / 100).toFixed(2);
  179. data.validStartDate = utils.formatTime(data.validStartDate,'yyyy-MM-dd')
  180. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  181. that.setData({
  182. data
  183. });
  184. });
  185. },
  186. /**
  187. * 去拼团
  188. */
  189. goToOrderGroup(orderId, orderGroupId, _this){
  190. console.log(22222222)
  191. let that = this;
  192. // 支付成功
  193. Http.post({
  194. url: config.api.toOrderGroup,
  195. data: {
  196. id: orderGroupId,
  197. orderId,
  198. couponId: _this.data.data.couponId
  199. }
  200. })
  201. .then(res => {
  202. wx.navigateTo({
  203. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}&couponChannelId=${_this.data.couponChannelId}`
  204. });
  205. })
  206. .catch(err => {
  207. console.log(err);
  208. })
  209. // return;
  210. },
  211. /**
  212. * 支付订单更新
  213. */
  214. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  215. console.log(orderGroupId,7777)
  216. let that = this;
  217. // 支付成功
  218. Http.post({
  219. url: config.api.payOrderUpdate,
  220. data: {
  221. payOrderId: payOrderId,
  222. orderId: orderId,
  223. status: status,
  224. reason: reason
  225. }
  226. })
  227. .then(res => {
  228. wx.hideLoading()
  229. if (orderGroupId==undefined){
  230. wx.navigateTo({
  231. url: '/pages/order/detail/index?orderId='+res.data.id,
  232. })
  233. }else{
  234. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  235. }
  236. })
  237. .catch(err => {
  238. console.log(err);
  239. if (err.code!=12002){
  240. setTimeout(function () {
  241. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  242. }, 2000)
  243. }
  244. })
  245. },
  246. /**
  247. * 发起支付
  248. */
  249. orderFunc(orderGroupId) {
  250. let data = {
  251. couponChannelId: this.data.data.id,
  252. couponId: this.data.data.couponId
  253. }
  254. /**
  255. * 拼团订单
  256. */
  257. if (orderGroupId==0){
  258. data.orderGroupId = orderGroupId
  259. }
  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. 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. showTime: true
  448. })
  449. if(this.data.spellData!=null){
  450. this.countdown()
  451. }
  452. },
  453. /**
  454. * 生命周期函数--监听页面隐藏
  455. */
  456. onHide: function () {
  457. },
  458. /**
  459. * 生命周期函数--监听页面卸载
  460. */
  461. onUnload: function () {
  462. this.setData({
  463. showTime: false
  464. })
  465. },
  466. /**
  467. * 页面相关事件处理函数--监听用户下拉动作
  468. */
  469. onPullDownRefresh: function (e) {
  470. let that=this;
  471. that.getDetail(that.data.couponChannelId);
  472. that.getOneSpell(that.data.couponId)
  473. },
  474. /**
  475. * 页面上拉触底事件的处理函数
  476. */
  477. onReachBottom: function () {
  478. }
  479. })