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.

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