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.

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