C端小程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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