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.

548 lines
15 KiB

  1. var config = require("../../../config/config.js");
  2. var app = getApp();
  3. const Http = require("../../../utils/HttpBasics");
  4. const util = require("../../../utils/util");
  5. const imgurl = require("../../../utils/imgurl");
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. bannerUrl: imgurl.banner.url,
  12. data: null,
  13. from: null,
  14. hour: null,
  15. min: null,
  16. sec: null,
  17. showPage: false,
  18. discountStatus:null,
  19. disabled:false,
  20. display:"block!important",
  21. showButton: false
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function(options) {
  27. let that = this;
  28. console.log(options)
  29. if (options.orderId || options.orderId && options.from == 'discount') {
  30. if (options.from) {
  31. that.setData({
  32. orderId: options.orderId,
  33. from: "discount"
  34. })
  35. } else {
  36. that.setData({
  37. orderId: options.orderId
  38. })
  39. }
  40. that.pressOrderDetail(options.orderId);
  41. }
  42. },
  43. onShow: function () {
  44. let that = this;
  45. console.log("--------------------------------onShow-------------------------------")
  46. var todayDate = new Date().getTime();
  47. that.setData({
  48. todayDate: todayDate,
  49. showButton:false
  50. })
  51. },
  52. goback:function(){
  53. wx.switchTab({
  54. url: '/pages/login/index',
  55. })
  56. },
  57. /**
  58. * 更多砍价商品
  59. */
  60. gotomore: function() {
  61. wx.navigateTo({
  62. url: '/pages/bargain/bargain',
  63. })
  64. },
  65. /**
  66. * @param {砍价状态(1:我发起的砍价,2:未参与的砍价, 3:已参与的砍价)} orderId
  67. */
  68. getPressOrderStatus: function(orderId) {
  69. let that = this;
  70. Http.get({
  71. url: config.api.getPressOrderStatus,
  72. data: {
  73. orderId: orderId
  74. }
  75. })
  76. .then(res => {
  77. console.log(res)
  78. console.log("--------------------------------status------------------------------------------")
  79. if (res.data.status == 1) {
  80. that.setData({
  81. discountStatus: 1
  82. })
  83. } else if (res.data.status == 2) {
  84. that.setData({
  85. discountStatus: 2
  86. })
  87. } else if (res.data.status == 3) {
  88. that.setData({
  89. discountStatus: 3
  90. })
  91. }
  92. wx.stopPullDownRefresh();
  93. })
  94. .catch(err => {
  95. console.log(err)
  96. wx.stopPullDownRefresh();
  97. wx.showToast({
  98. title: err.errMsg,
  99. icon: "none"
  100. })
  101. })
  102. },
  103. helpDiscount: function() {
  104. let that = this;
  105. that.pressOrderJoin(that.data.orderId);
  106. },
  107. //参与砍价
  108. pressOrderJoin: function(orderId) {
  109. let that = this;
  110. Http.post({
  111. url: config.api.pressOrderJoin,
  112. data: {
  113. orderId: orderId
  114. }
  115. })
  116. .then(res => {
  117. console.log(res);
  118. // id 是订单号
  119. that.pressOrderDetail(orderId);
  120. })
  121. .catch(err => {
  122. console.log(err)
  123. wx.showToast({
  124. title: err.message,
  125. icon: "none"
  126. })
  127. })
  128. },
  129. // 订单详情
  130. pressOrderDetail: function(orderId) {
  131. let that = this;
  132. Http.get({
  133. url: config.api.pressOrderDetail,
  134. data: {
  135. orderId: orderId
  136. }
  137. })
  138. .then(res => {
  139. console.log(res);
  140. res.data.orderPressList.map(file => {
  141. file.createDate = diffTime(file.createDate)
  142. })
  143. that.countdown(res.data.pressEndDate);
  144. res.data.orderPressList.map(file=>{
  145. compare(file.first);
  146. })
  147. console.log(res.data.orderPressList.sort(compare('first')))
  148. that.setData({
  149. data: res.data,
  150. already: (res.data.price / 100 - res.data.salePrice / 100 - res.data.pressCurrentValue / 100).toFixed(2),
  151. remain: (res.data.pressCurrentValue / 100).toFixed(2),
  152. showPage: true
  153. })
  154. that.getPressOrderStatus(orderId);
  155. })
  156. .catch(err => {
  157. wx.showToast({
  158. title: err.message,
  159. icon: "none"
  160. })
  161. wx.stopPullDownRefresh();
  162. })
  163. },
  164. countdown(end_time) {
  165. let that = this;
  166. var EndTime = end_time;
  167. var NowTime = new Date().getTime();
  168. var total_micro_second = EndTime - NowTime || [];
  169. // 渲染倒计时时钟
  170. let obj = that.dateformat(total_micro_second);
  171. if (total_micro_second > 0) {
  172. that.setData({
  173. clock: obj,
  174. day: obj.a1,
  175. hour: obj.b1,
  176. min: obj.c1,
  177. sec: obj.d1,
  178. })
  179. } else {
  180. that.setData({
  181. clock: "00",
  182. day: "00",
  183. hour: "00",
  184. min: "00",
  185. sec: "00",
  186. })
  187. }
  188. setTimeout(function() {
  189. total_micro_second -= 1000;
  190. that.countdown(end_time);
  191. }, 1000)
  192. },
  193. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  194. dateformat(micro_second) {
  195. // 总秒数
  196. var second = Math.floor(micro_second / 1000);
  197. // 天数
  198. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  199. // 小时
  200. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  201. // 分钟
  202. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  203. // 秒
  204. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  205. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  206. return {
  207. a1: day,
  208. b1: hr,
  209. c1: min,
  210. d1: sec
  211. }
  212. },
  213. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  214. let that = this;
  215. console.log(that)
  216. console.log(_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. _this.setData({
  230. display: "none!important",
  231. })
  232. // 有价券
  233. if (!type && type != 'free') {
  234. wx.navigateTo({
  235. url: `/pages/order/detail/index?orderId=${
  236. orderId
  237. }`
  238. });
  239. } else if (type == 'free') {
  240. wx.navigateTo({
  241. url: `/pages/order/detail/index?orderId=${
  242. orderId
  243. }`
  244. });
  245. }
  246. })
  247. .catch(err => {
  248. console.log(err);
  249. if (!type) {
  250. setTimeout(function() {
  251. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  252. }, 2000)
  253. }
  254. })
  255. },
  256. /**
  257. * 发起支付
  258. */
  259. orderFunc(e) {
  260. var that = this;
  261. that.setData({
  262. showButton:true
  263. })
  264. Http.post({
  265. url: config.api.checkPhoneStatus,
  266. data: {}
  267. })
  268. .then(res => {
  269. that.setData({
  270. showButton: false
  271. })
  272. if (typeof(res) != "undefined") {
  273. let orderId = "" + e.currentTarget.dataset.orderid;
  274. that.setData({
  275. orderId: orderId
  276. });
  277. if (e.currentTarget.dataset.saleprice / 100 > 0 || e.currentTarget.dataset.price / 100 > 0) {
  278. /**
  279. * 支付订单创建
  280. */
  281. Http.post({
  282. url: config.api.payOrderCreate,
  283. data: {
  284. orderId: orderId
  285. }
  286. })
  287. .then(res => {
  288. var payOrderId = "" + res.data.payOrderId;
  289. wx.hideLoading();
  290. wx.requestPayment({
  291. timeStamp: res.data.timeStamp,
  292. nonceStr: res.data.nonceStr,
  293. package: res.data.package,
  294. signType: (res.data.signType) ? res.data.signType : "MD5",
  295. paySign: res.data.paySign,
  296. success: res => {
  297. wx.showLoading({
  298. title: '订单正在处理中...',
  299. })
  300. setTimeout(function() {
  301. wx.hideLoading()
  302. }, 5000)
  303. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  304. if (res.errMsg == "requestPayment:ok") {
  305. setTimeout(function() {
  306. wx.hideLoading();
  307. }, 2000);
  308. /**
  309. * 用户支付成功以后跳转到券包列表
  310. */
  311. if (that.data.cardType == 100) {
  312. wx.setStorage({
  313. key: 'couponNum2',
  314. data: "couponNum2"
  315. })
  316. } else {
  317. wx.setStorage({
  318. key: 'couponNum',
  319. data: "couponNum"
  320. })
  321. }
  322. }
  323. },
  324. fail: res => {
  325. /**
  326. * 支付失败,需要更新订单的状态
  327. */
  328. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  329. that.setData({
  330. showbutton: false
  331. })
  332. return;
  333. },
  334. complete: res => {}
  335. });
  336. /// End payment --------
  337. })
  338. .catch(err => {
  339. console.log(err);
  340. wx.showToast({
  341. title: err.message,
  342. icon: 'none',
  343. duration: 2000,
  344. mask: false
  345. });
  346. })
  347. } else {
  348. // 免费券
  349. that.payOrderUpdate(orderId, "0", 1, '', 'free',that);
  350. if (that.data.cardType == 100) {
  351. wx.setStorage({
  352. key: 'couponNum2',
  353. data: "couponNum2"
  354. })
  355. } else {
  356. wx.setStorage({
  357. key: 'couponNum',
  358. data: "couponNum"
  359. })
  360. }
  361. }
  362. }
  363. })
  364. .catch(err => {
  365. that.setData({
  366. showButton: true
  367. })
  368. if (err.code == 2011) {
  369. wx.showToast({
  370. title: "商户信息没找到",
  371. image: './../../../assets/images/fail.png',
  372. duration: 2000,
  373. mask: false
  374. });
  375. } else if (err.code == 2013) {
  376. wx.showToast({
  377. title: "商户信息禁用",
  378. image: './../../../assets/images/fail.png',
  379. duration: 2000,
  380. mask: false
  381. });
  382. } else if (err.code == 3000) {
  383. wx.showToast({
  384. title: "库存不足",
  385. image: './../../../assets/images/fail.png',
  386. duration: 2000,
  387. mask: false
  388. });
  389. } else if (err.code == 3001) {
  390. wx.showToast({
  391. title: "超过限购条件",
  392. image: './../../../assets/images/fail.png',
  393. duration: 2000,
  394. mask: false
  395. });
  396. } else if (err.code == 3002) {
  397. wx.showToast({
  398. title: "订单失败",
  399. image: './../../../assets/images/fail.png',
  400. duration: 2000,
  401. mask: false
  402. });
  403. } else if (err.code == 3003) {
  404. wx.showToast({
  405. title: "订单不存在",
  406. image: './../../../assets/images/fail.png',
  407. duration: 2000,
  408. mask: false
  409. });
  410. } else if (err.code == 3004) {
  411. wx.showToast({
  412. title: "订单不存在",
  413. image: './../../../assets/images/fail.png',
  414. duration: 2000,
  415. mask: false
  416. });
  417. } else if (err.code == 4003) {
  418. wx.showToast({
  419. title: "卡券已作废",
  420. image: './../../../assets/images/fail.png',
  421. duration: 2000,
  422. mask: false
  423. });
  424. } else if (err.code == 11005) {
  425. /**
  426. * 将值传到用户手机号授权的页面
  427. *
  428. */
  429. wx.redirectTo({
  430. url: "/pages/getphoneInfo/index?couponChannelId=" +
  431. that.data.couponChannelId +
  432. "&couponId=" +
  433. that.data.couponId
  434. });
  435. } else if (err.code == 11006) {
  436. // 用户手机已加密
  437. wx.redirectTo({
  438. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  439. that.data.couponChannelId +
  440. "&couponId=" +
  441. that.data.couponId
  442. });
  443. } else {
  444. wx.showToast({
  445. title: err.message,
  446. icon: 'none',
  447. duration: 2000,
  448. mask: false
  449. });
  450. }
  451. })
  452. },
  453. /**
  454. *
  455. * @param {朋友邀请砍价} e
  456. */
  457. barginAgain: function(e) {
  458. let that = this;
  459. let couponChannelId = e.currentTarget.dataset.couponchannelid;
  460. let couponId = e.currentTarget.dataset.couponid;
  461. wx.navigateTo({
  462. url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`,
  463. })
  464. },
  465. /**
  466. * 刷新
  467. */
  468. onPullDownRefresh: function (e) {
  469. let that = this;
  470. if (that.data.orderId || that.data.orderId && that.data.from == 'discount') {
  471. if (that.data.from) {
  472. that.setData({
  473. orderId: that.data.orderId,
  474. from: "discount"
  475. })
  476. } else {
  477. that.setData({
  478. orderId: that.data.orderId
  479. })
  480. }
  481. that.pressOrderDetail(that.data.orderId);
  482. }
  483. },
  484. onShareAppMessage: function(options) {
  485. console.log(options)
  486. var that = this;
  487. console.log(that.data.data.id)
  488. var shareObj = {
  489. title: that.data.data.title,
  490. path: `/pages/index/index?orderId=${that.data.data.id}&from=${"discount"}`,
  491. success: function(res) {
  492. if (res.errMsg == 'shareAppMessage:ok') {}
  493. },
  494. fail: function(error) {
  495. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  496. }
  497. };
  498. // 来自页面内的按钮的转发
  499. if (options.from == 'button') {
  500. var orderId = options.target.dataset.orderid;
  501. shareObj.path = `/pages/index/index?orderId=${orderId}&from=${"discount"}`;
  502. }
  503. // 返回shareObj
  504. return shareObj;
  505. },
  506. })
  507. function diffTime(startDate) {
  508. var diff = new Date().getTime() - startDate;
  509. var days = Math.floor(diff / (24 * 3600 * 1000));
  510. var leave1 = diff % (24 * 3600 * 1000);
  511. var hours = Math.floor(leave1 / (3600 * 1000));
  512. var leave2 = leave1 % (3600 * 1000);
  513. var minutes = Math.floor(leave2 / (60 * 1000));
  514. var leave3 = leave2 % (60 * 1000);
  515. var seconds = Math.round(leave3 / 1000);
  516. var returnStr = seconds + "秒";
  517. if (minutes > 0) {
  518. returnStr = minutes + "分" + returnStr;
  519. }
  520. if (hours > 0) {
  521. returnStr = hours + "小时" + returnStr;
  522. }
  523. if (days > 0) {
  524. returnStr = days + "天" + returnStr;
  525. }
  526. if (returnStr.indexOf('-1')==-1){
  527. return returnStr;
  528. }else{
  529. return '1秒'
  530. }
  531. }
  532. function compare(property) {
  533. return function (a, b) {
  534. var value1 = a[property];
  535. var value2 = b[property];
  536. return value2 - value1;
  537. }
  538. }