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.

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