C端小程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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