C端小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

548 行
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. 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. orderId: 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.showToast({
  396. title: "您有未支付订单",
  397. image: './../../../assets/images/fail.png',
  398. duration: 2000,
  399. mask: false
  400. });
  401. } else if (err.code == 11005) {
  402. /**
  403. * 将值传到用户手机号授权的页面
  404. *
  405. */
  406. wx.redirectTo({
  407. url: "/pages/getphoneInfo/index?couponChannelId=" +
  408. that.data.couponChannelId +
  409. "&couponId=" +
  410. that.data.couponId
  411. });
  412. } else if (err.code == 11006) {
  413. // 用户手机已加密
  414. wx.redirectTo({
  415. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  416. that.data.couponChannelId +
  417. "&couponId=" +
  418. that.data.couponId
  419. });
  420. } else {
  421. wx.showToast({
  422. title: err.message,
  423. icon: 'none',
  424. duration: 2000,
  425. mask: false
  426. });
  427. }
  428. })
  429. .then(res => {
  430. if (typeof (res) != "undefined") {
  431. let orderId = "" + res.data.id;
  432. that.setData({
  433. orderId: orderId
  434. });
  435. if (res.data.payment > 0) {
  436. // 支付金额不为0
  437. /**
  438. * 支付订单创建
  439. */
  440. Http.post({
  441. url: config.api.payOrderCreate,
  442. data: {
  443. orderId: orderId
  444. }
  445. }).then(res => {
  446. var payOrderId = "" + res.data.payOrderId;
  447. wx.hideLoading();
  448. wx.requestPayment({
  449. timeStamp: res.data.timeStamp,
  450. nonceStr: res.data.nonceStr,
  451. package: res.data.package,
  452. signType: (res.data.signType) ? res.data.signType : "MD5",
  453. paySign: res.data.paySign,
  454. success: res => {
  455. wx.showLoading({
  456. title: '订单正在处理中...',
  457. })
  458. setTimeout(function () {
  459. wx.hideLoading()
  460. }, 5000)
  461. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  462. if (res.errMsg == "requestPayment:ok") {
  463. setTimeout(function () {
  464. wx.hideLoading();
  465. }, 2000);
  466. /**
  467. * 用户支付成功以后跳转到券包列表
  468. */
  469. wx.setStorage({
  470. key: 'couponNum',
  471. data: "couponNum",
  472. })
  473. }
  474. },
  475. fail: res => {
  476. /**
  477. * 支付失败,需要更新订单的状态
  478. */
  479. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  480. that.setData({
  481. showbutton: false
  482. })
  483. return;
  484. },
  485. complete: res => { }
  486. });
  487. /// End payment --------
  488. })
  489. .catch(err => {
  490. wx.showToast({
  491. title: err.message,
  492. icon: 'none',
  493. duration: 2000,
  494. mask: false
  495. });
  496. })
  497. } else {
  498. // 免费券
  499. that.payOrderUpdate(orderId, "0", 1, '', 'free');
  500. wx.setStorage({
  501. key: 'couponNum',
  502. data: "couponNum"
  503. })
  504. }
  505. }
  506. })
  507. .catch(err => {
  508. wx.showToast({
  509. title: err.message,
  510. icon: 'none',
  511. duration: 2000,
  512. mask: false
  513. });
  514. })
  515. },
  516. onShow() {
  517. this.setData({
  518. showbutton: false
  519. })
  520. },
  521. onShareAppMessage: function (options) {
  522. var that = this;
  523. var shareObj = {
  524. title: that.data.data.title,
  525. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.couponId}`,
  526. success: function (res) {
  527. if (res.errMsg == 'shareAppMessage:ok') { }
  528. },
  529. fail: function (error) {
  530. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  531. }
  532. };
  533. // 来自页面内的按钮的转发
  534. if (options.from == 'button') {
  535. var eData = options.target.dataset.id;
  536. var couponId = options.target.dataset.couponid;
  537. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  538. }
  539. // 返回shareObj
  540. return shareObj;
  541. },
  542. });