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.

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