C端小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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