C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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