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.

589 lines
16 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. data: {
  8. fenxiangUrl: imgurl.fenxiang.url,
  9. clockUrl: imgurl.clock.url,
  10. teljpgUrl: imgurl.teljpg.url,
  11. closedUrl: imgurl.closed.url,
  12. quesGouUrl: imgurl.ques_gou.url,
  13. headbgUrl: imgurl.headbg.url,
  14. quesBgUrl: imgurl.ques_bg.url,
  15. data: {
  16. title: null
  17. },
  18. showPage: false,
  19. questions1: null,
  20. questions2: null,
  21. carList: [],
  22. couponChannelId: null,
  23. couponId: null,
  24. orderId: "",
  25. hour: null,
  26. minute: "",
  27. tempFilePaths: null,
  28. userInfo: {},
  29. hasUserInfo: false,
  30. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  31. id: null,
  32. result: [],
  33. end_time: null,
  34. checked:false,
  35. clock: "已经截止",
  36. questionnaire: {},
  37. questionId: null,
  38. widthScreen: null,
  39. moveData: null,
  40. rotateData: null,
  41. alphaData: null,
  42. scaleData: null,
  43. skewData: null,
  44. matrixData: null,
  45. opacity: 0,
  46. queueData: null,
  47. zIndex: 11,
  48. display: "none",
  49. showbutton: false
  50. },
  51. phone: function (e) {
  52. let that = this;
  53. wx.makePhoneCall({
  54. phoneNumber: e.target.dataset.merchantlinkphone
  55. });
  56. },
  57. /**
  58. * 点击提交问题单选
  59. */
  60. formSubmit: function (e) {
  61. console.log(e);
  62. let that = this;
  63. /**
  64. * 多选
  65. */
  66. if (e.currentTarget.dataset.flags == 'multi') {
  67. if (that.data.anwserId.length == 0) {
  68. var answserIs = null
  69. } else {
  70. var answserIs = that.data.anwserId.join(",");
  71. }
  72. }
  73. else if (e.currentTarget.dataset.flags == 'single') {
  74. var answserIs = e.detail.value;
  75. }
  76. console.log(answserIs)
  77. console.log(e.currentTarget.dataset.questionid)
  78. Http.post({
  79. url: config.api.answerQuestion,
  80. data: {
  81. answer: answserIs,
  82. questionId: e.currentTarget.dataset.questionid
  83. }
  84. })
  85. .then(res => {
  86. that.closeQuestion();
  87. })
  88. .catch(err => {
  89. wx.showToast({
  90. title: err.message,
  91. icon: 'none',
  92. duration: 2000,
  93. mask: false
  94. });
  95. })
  96. },
  97. /**
  98. * 多选
  99. */
  100. checkboxChange: function (e) {
  101. console.log(e)
  102. this.setData({
  103. anwserId: e.detail.value
  104. })
  105. },
  106. closeQuestion: function () {
  107. let that = this;
  108. that.setData({
  109. display: "none",
  110. zIndex: 11,
  111. opacity: 0
  112. })
  113. setTimeout(function () {
  114. that.orderFunc();
  115. }, 500)
  116. },
  117. /**
  118. * gotopay
  119. */
  120. gotopay: function (e) {
  121. let that = this;
  122. var discount = e.currentTarget.dataset.discount;
  123. that.setData({
  124. queueData: null,
  125. showbutton: true
  126. })
  127. Http.get({
  128. url: config.api.getQuestion,
  129. data: {
  130. couponType: JSON.stringify(that.data.data.type)
  131. }
  132. })
  133. .then(res => {
  134. console.log(res);
  135. if (res.data == undefined) {
  136. that.orderFunc(discount);
  137. that.setData({
  138. flag: false
  139. })
  140. } else if (res.data) {
  141. var animation = wx.createAnimation({});
  142. animation.translate((that.data.widthScreen - that.data.widthScreen), 0).scale(1).opacity(1).step({
  143. duration: 500
  144. })
  145. that.setData({
  146. queueData: animation.export(),
  147. zIndex: 9,
  148. opacity: 1,
  149. display: "block",
  150. questionnaire: JSON.parse(res.data.content),
  151. questionId: res.data.id
  152. });
  153. }
  154. })
  155. .catch(err => {
  156. wx.showToast({
  157. title: err.message,
  158. icon: 'none',
  159. duration: 2000,
  160. mask: false
  161. });
  162. })
  163. },
  164. countdown(end_time) {
  165. let that = this;
  166. var EndTime = end_time;
  167. var NowTime = new Date().getTime();
  168. var total_micro_second = EndTime - NowTime || [];
  169. // 渲染倒计时时钟
  170. let obj = that.dateformat(total_micro_second);
  171. if (total_micro_second > 0) {
  172. that.setData({
  173. clock: obj,
  174. day: obj.a1,
  175. hour: obj.b1,
  176. min: obj.c1,
  177. sec: obj.d1,
  178. })
  179. } else {
  180. that.setData({
  181. clock: "00",
  182. day: "00",
  183. hour: "00",
  184. min: "00",
  185. sec: "00",
  186. })
  187. }
  188. setTimeout(function () {
  189. total_micro_second -= 1000;
  190. that.countdown(end_time);
  191. }, 1000)
  192. },
  193. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  194. dateformat(micro_second) {
  195. // 总秒数
  196. var second = Math.floor(micro_second / 1000);
  197. // 天数
  198. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  199. // 小时
  200. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  201. // 分钟
  202. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  203. // 秒
  204. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  205. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  206. return {
  207. a1: day,
  208. b1: hr,
  209. c1: min,
  210. d1: sec
  211. }
  212. },
  213. onLoad(options) {
  214. let that = this;
  215. wx.showLoading({
  216. title: "加载中..."
  217. });
  218. that.setData({
  219. couponChannelId: options.couponChannelId,
  220. couponId: options.couponId,
  221. title: that.data.data.title ? that.data.data.title : '',
  222. });
  223. if (options.cardType) {
  224. that.setData({
  225. cardType: options.cardType
  226. })
  227. }
  228. var parmer = {
  229. url: config.api.couponDetail,
  230. data: {
  231. couponChannelId: options.couponChannelId
  232. }
  233. };
  234. Http.get(parmer)
  235. .then(res => {
  236. if (res.code == 200) {
  237. that.setData({
  238. showPage: true
  239. })
  240. }
  241. if (res.data.type == 7) {
  242. wx.setNavigationBarTitle({
  243. title: "卡详情"
  244. })
  245. }
  246. if (res.data.endTime) {
  247. that.countdown(res.data.endTime);
  248. //当前时间与优惠券下架时间做计算
  249. var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dd hh:mm:ss");
  250. if (util.timechuo(endTime).indexOf('-') == 0) {
  251. that.setData({
  252. endtime: "活动已结束",
  253. });
  254. } else {
  255. that.setData({
  256. endtime: util.timechuo(endTime)
  257. });
  258. }
  259. }
  260. wx.hideLoading();
  261. that.setData({
  262. data: res.data
  263. });
  264. if (res.data.validType == 1) {
  265. that.setData({
  266. validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dd"),
  267. validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dd"),
  268. });
  269. } else {
  270. if (res.data.validDays) {
  271. that.setData({
  272. validDays: res.data.validDays
  273. });
  274. }
  275. }
  276. }).catch(err => {
  277. wx.showToast({
  278. title: err.message,
  279. icon: 'none',
  280. duration: 2000,
  281. mask: false
  282. });
  283. })
  284. },
  285. /**
  286. * 邀请好友继续砍价
  287. */
  288. inviteFriend: function () {
  289. let that = this;
  290. that.orderFunc("discount");
  291. },
  292. /**
  293. * 支付订单更新
  294. */
  295. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  296. let that = this;
  297. // 支付成功
  298. Http.post({
  299. url: config.api.payOrderUpdate,
  300. data: {
  301. payOrderId: payOrderId,
  302. orderId: orderId,
  303. status: status,
  304. reason: reason
  305. }
  306. })
  307. .then(res => {
  308. wx.hideLoading()
  309. // 有价券
  310. if (!type && type != 'free') {
  311. wx.navigateTo({
  312. url: `/pages/order/detail/index?orderId=${
  313. orderId
  314. }`
  315. });
  316. } else if (type == 'free') {
  317. wx.navigateTo({
  318. url: `/pages/order/detail/index?orderId=${
  319. orderId
  320. }`
  321. });
  322. }
  323. })
  324. .catch(err => {
  325. console.log(err);
  326. if (!type) {
  327. setTimeout(function () {
  328. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  329. }, 2000)
  330. }
  331. })
  332. },
  333. /**
  334. * 发起支付
  335. */
  336. orderFunc(discount) {
  337. console.log(discount)
  338. var that = this;
  339. Http.post({
  340. url: config.api.checkPhoneStatus,
  341. data: {}
  342. })
  343. .then(res => {
  344. if (discount == 'discount' || discount == 'discount1') {
  345. var data = {
  346. couponChannelId: "" + that.data.couponChannelId,
  347. couponId: "" + that.data.couponId,
  348. press: true
  349. }
  350. } else if (that.data.couponChannelId == null) {
  351. var data = {
  352. couponId: "" + that.data.couponId
  353. };
  354. } else {
  355. var data = {
  356. couponChannelId: "" + that.data.couponChannelId,
  357. couponId: "" + that.data.couponId,
  358. }
  359. }
  360. /**
  361. * orderSave 下单
  362. */
  363. return Http.post({
  364. url: config.api.orderSave,
  365. data: data
  366. });
  367. })
  368. .catch(err => {
  369. console.log(err);
  370. if (err.code == 2011) {
  371. wx.showToast({
  372. title: "商户信息没找到",
  373. image: './../../../assets/images/fail.png',
  374. duration: 2000,
  375. mask: false
  376. });
  377. } else if (err.code == 2013) {
  378. wx.showToast({
  379. title: "商户信息禁用",
  380. image: './../../../assets/images/fail.png',
  381. duration: 2000,
  382. mask: false
  383. });
  384. } else if (err.code == 3000) {
  385. wx.showToast({
  386. title: "库存不足",
  387. image: './../../../assets/images/fail.png',
  388. duration: 2000,
  389. mask: false
  390. });
  391. } else if (err.code == 3001) {
  392. wx.showToast({
  393. title: "领取达到上限",
  394. image: './../../../assets/images/fail.png',
  395. duration: 2000,
  396. mask: false
  397. });
  398. } else if (err.code == 3002) {
  399. wx.showToast({
  400. title: "订单失败",
  401. image: './../../../assets/images/fail.png',
  402. duration: 2000,
  403. mask: false
  404. });
  405. } else if (err.code == 3003) {
  406. wx.showToast({
  407. title: "订单不存在",
  408. image: './../../../assets/images/fail.png',
  409. duration: 2000,
  410. mask: false
  411. });
  412. } else if (err.code == 3004) {
  413. wx.showToast({
  414. title: "订单不存在",
  415. image: './../../../assets/images/fail.png',
  416. duration: 2000,
  417. mask: false
  418. });
  419. } else if (err.code == 4003) {
  420. wx.showToast({
  421. title: "卡券已作废",
  422. image: './../../../assets/images/fail.png',
  423. duration: 2000,
  424. mask: false
  425. });
  426. } else if (err.code == 11005) {
  427. /**
  428. * 将值传到用户手机号授权的页面
  429. *
  430. */
  431. wx.redirectTo({
  432. url: "/pages/getphoneInfo/index?couponChannelId=" +
  433. that.data.couponChannelId +
  434. "&couponId=" +
  435. that.data.couponId
  436. });
  437. } else if (err.code == 11006) {
  438. // 用户手机已加密
  439. wx.redirectTo({
  440. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  441. that.data.couponChannelId +
  442. "&couponId=" +
  443. that.data.couponId
  444. });
  445. } else {
  446. wx.showToast({
  447. title: err.message,
  448. icon: 'none',
  449. duration: 2000,
  450. mask: false
  451. });
  452. }
  453. })
  454. .then(res => {
  455. console.log(res)
  456. if (discount != 'discount') {
  457. if (typeof (res) != "undefined") {
  458. let orderId = "" + res.data.id;
  459. that.setData({
  460. orderId: orderId
  461. });
  462. if (res.data.payment > 0) {
  463. // 支付金额不为0
  464. /**
  465. * 支付订单创建
  466. */
  467. Http.post({
  468. url: config.api.payOrderCreate,
  469. data: {
  470. orderId: orderId
  471. }
  472. })
  473. .then(res => {
  474. var payOrderId = "" + res.data.payOrderId;
  475. wx.hideLoading();
  476. wx.requestPayment({
  477. timeStamp: res.data.timeStamp,
  478. nonceStr: res.data.nonceStr,
  479. package: res.data.package,
  480. signType: (res.data.signType) ? res.data.signType : "MD5",
  481. paySign: res.data.paySign,
  482. success: res => {
  483. wx.showLoading({
  484. title: '订单正在处理中...',
  485. })
  486. setTimeout(function () {
  487. wx.hideLoading()
  488. }, 5000)
  489. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  490. if (res.errMsg == "requestPayment:ok") {
  491. setTimeout(function () {
  492. wx.hideLoading();
  493. }, 2000);
  494. /**
  495. * 用户支付成功以后跳转到券包列表
  496. */
  497. if (that.data.cardType == 7) {
  498. wx.setStorage({
  499. key: 'couponNum2',
  500. data: "couponNum2"
  501. })
  502. } else {
  503. wx.setStorage({
  504. key: 'couponNum',
  505. data: "couponNum"
  506. })
  507. }
  508. }
  509. },
  510. fail: res => {
  511. /**
  512. * 支付失败,需要更新订单的状态
  513. */
  514. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  515. that.setData({
  516. showbutton: false
  517. })
  518. return;
  519. },
  520. complete: res => {}
  521. });
  522. /// End payment --------
  523. })
  524. .catch(err => {
  525. wx.showToast({
  526. title: err.message,
  527. icon: 'none',
  528. duration: 2000,
  529. mask: false
  530. });
  531. })
  532. } else {
  533. // 免费券
  534. that.payOrderUpdate(orderId, "0", 1, '', 'free');
  535. if (that.data.cardType == 7) {
  536. wx.setStorage({
  537. key: 'couponNum2',
  538. data: "couponNum2"
  539. })
  540. } else {
  541. wx.setStorage({
  542. key: 'couponNum',
  543. data: "couponNum"
  544. })
  545. }
  546. }
  547. }
  548. } else {
  549. if(res){
  550. wx.navigateTo({
  551. url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.id}`,
  552. })
  553. }
  554. }
  555. })
  556. },
  557. onShow() {
  558. this.setData({
  559. showbutton: false
  560. })
  561. },
  562. onShareAppMessage: function (options) {
  563. var that = this;
  564. var shareObj = {
  565. title: that.data.data.title,
  566. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.couponId}`,
  567. success: function (res) {
  568. if (res.errMsg == 'shareAppMessage:ok') {}
  569. },
  570. fail: function (error) {
  571. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  572. }
  573. };
  574. // 来自页面内的按钮的转发
  575. if (options.from == 'button') {
  576. var eData = options.target.dataset.id;
  577. var couponId = options.target.dataset.couponid;
  578. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  579. }
  580. // 返回shareObj
  581. return shareObj;
  582. },
  583. });