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.

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