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.

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