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.

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