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.

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