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.

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