C端小程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

552 wiersze
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) {
  278. wx.navigateTo({
  279. url: `/pages/order/detail/index?orderId=${
  280. orderId
  281. }`
  282. });
  283. }
  284. })
  285. .catch(err => {
  286. console.log(err);
  287. if (!type) {
  288. setTimeout(function () {
  289. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  290. }, 2000)
  291. }
  292. })
  293. },
  294. /**
  295. * 发起支付
  296. */
  297. orderFunc(flag) {
  298. var that = this;
  299. // wx.showLoading({
  300. // title: "加载中..."
  301. // });
  302. if (that.data.data.type == 6) {} else {
  303. Http.post({
  304. url: config.api.checkPhoneStatus,
  305. data: {}
  306. })
  307. .then(res => {
  308. var data = {
  309. couponChannelId: "" + that.data.couponChannelId,
  310. couponId: "" + that.data.couponId
  311. };
  312. if (that.data.couponChannelId == null) {
  313. var data = {
  314. couponId: "" + that.data.couponId
  315. };
  316. }
  317. /**
  318. * orderSave 下单
  319. */
  320. return Http.post({
  321. url: config.api.orderSave,
  322. data: data
  323. });
  324. })
  325. .catch(err => {
  326. if (err.code == "2011") {
  327. wx.showToast({
  328. title: "商户信息没找到",
  329. image: "./../../../assets/img/fail.png",
  330. duration: 2000,
  331. mask: false
  332. });
  333. } else if (err.code == "2013") {
  334. wx.showToast({
  335. title: "商户信息禁用",
  336. image: "./../../../assets/img/fail.png",
  337. duration: 2000,
  338. mask: false
  339. });
  340. } else if (err.code == "3000") {
  341. wx.showToast({
  342. title: "库存不足",
  343. image: "./../../../assets/img/fail.png",
  344. duration: 2000,
  345. mask: false
  346. });
  347. } else if (err.code == "3001") {
  348. wx.showToast({
  349. title: "领取达到上限",
  350. image: "./../../../assets/img/fail.png",
  351. duration: 2000,
  352. mask: false
  353. });
  354. } else if (err.code == "3002") {
  355. wx.showToast({
  356. title: "订单失败",
  357. image: "./../../../assets/img/fail.png",
  358. duration: 2000,
  359. mask: false
  360. });
  361. } else if (err.code == "3003") {
  362. wx.showToast({
  363. title: "订单不存在",
  364. image: "./../../../assets/img/fail.png",
  365. duration: 2000,
  366. mask: false
  367. });
  368. } else if (err.code == "3004") {
  369. wx.showToast({
  370. title: "订单不存在",
  371. image: "./../../../assets/img/fail.png",
  372. duration: 2000,
  373. mask: false
  374. });
  375. } else if (err.code == "4003") {
  376. wx.showToast({
  377. title: "卡券已作废",
  378. image: "./../../../assets/img/fail.png",
  379. duration: 2000,
  380. mask: false
  381. });
  382. } else if (err.code == 11005) {
  383. /**
  384. * 将值传到用户手机号授权的页面
  385. *
  386. */
  387. wx.redirectTo({
  388. url: "/pages/getphoneInfo/index?couponChannelId=" +
  389. that.data.couponChannelId +
  390. "&couponId=" +
  391. that.data.couponId
  392. });
  393. } else if (err.code == 11006) {
  394. // 用户手机已加密
  395. wx.redirectTo({
  396. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  397. that.data.couponChannelId +
  398. "&couponId=" +
  399. that.data.couponId
  400. });
  401. } else {
  402. wx.showToast({
  403. title: err.message,
  404. icon: 'none',
  405. duration: 2000,
  406. mask: false
  407. });
  408. }
  409. })
  410. .then(res => {
  411. if (typeof (res) != "undefined") {
  412. let orderId = "" + res.data.id;
  413. that.setData({
  414. orderId: orderId
  415. });
  416. if (res.data.payment > 0) {
  417. // 支付金额不为0
  418. /**
  419. * 支付订单创建
  420. */
  421. Http.post({
  422. url: config.api.payOrderCreate,
  423. data: {
  424. orderId: orderId
  425. }
  426. }).then(res => {
  427. var payOrderId = "" + res.data.payOrderId;
  428. wx.hideLoading();
  429. wx.requestPayment({
  430. timeStamp: res.data.timeStamp,
  431. nonceStr: res.data.nonceStr,
  432. package: res.data.package,
  433. signType: (res.data.signType) ? res.data.signType : "MD5",
  434. paySign: res.data.paySign,
  435. success: res => {
  436. wx.showLoading({
  437. title: '订单正在处理中...',
  438. })
  439. setTimeout(function(){
  440. wx.hideLoading()
  441. },5000)
  442. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  443. if (res.errMsg == "requestPayment:ok") {
  444. setTimeout(function () {
  445. wx.hideLoading();
  446. }, 2000);
  447. /**
  448. * 用户支付成功以后跳转到券包列表
  449. */
  450. wx.setStorage({
  451. key: 'couponNum',
  452. data: "couponNum",
  453. })
  454. // if (that.data.data.type == 5) {
  455. // setTimeout(() => {
  456. // wx.switchTab({
  457. // url: '/pages/passCar/passCar'
  458. // });
  459. // }, 1600);
  460. // }
  461. }
  462. },
  463. fail: res => {
  464. /**
  465. * 支付失败,需要更新订单的状态
  466. */
  467. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  468. that.setData({
  469. showbutton: false
  470. })
  471. return;
  472. },
  473. complete: res => {}
  474. });
  475. /// End payment --------
  476. })
  477. .catch(err => {
  478. wx.showToast({
  479. title: err.message,
  480. icon: 'none',
  481. duration: 2000,
  482. mask: false
  483. });
  484. })
  485. } else {
  486. // 免费券
  487. that.payOrderUpdate(orderId, "0", 1, '', 'fail');
  488. wx.setStorage({
  489. key: 'couponNum',
  490. data: "couponNum"
  491. })
  492. wx.navigateTo({
  493. url: `/pages/order/detail/index?orderId=${
  494. that.data.orderId
  495. }`
  496. });
  497. // setTimeout(function () {
  498. // }, 1000)
  499. // if (that.data.data.type == 5) {
  500. // setTimeout(() => {
  501. // wx.switchTab({
  502. // url: '/pages/passCar/passCar'
  503. // });
  504. // }, 1600);
  505. // }
  506. }
  507. }
  508. })
  509. .catch(err => {
  510. wx.showToast({
  511. title: err.message,
  512. icon: 'none',
  513. duration: 2000,
  514. mask: false
  515. });
  516. })
  517. }
  518. },
  519. onShow(){
  520. this.setData({
  521. showbutton:false
  522. })
  523. },
  524. onShareAppMessage: function (options) {
  525. var that = this;
  526. var shareObj = {
  527. title: that.data.data.title,
  528. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.couponId}`,
  529. success: function (res) {
  530. if (res.errMsg == 'shareAppMessage:ok') {}
  531. },
  532. fail: function (error) {
  533. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  534. }
  535. };
  536. // 来自页面内的按钮的转发
  537. if (options.from == 'button') {
  538. var eData = options.target.dataset.id;
  539. var couponId = options.target.dataset.couponid;
  540. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  541. }
  542. // 返回shareObj
  543. return shareObj;
  544. },
  545. });