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.

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