C端小程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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