C端小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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