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.

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