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

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