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.

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