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.

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