C端小程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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