C端小程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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