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

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