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.

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