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.

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