C端小程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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