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.

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