C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

677 line
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. that.setData({
  224. couponChannelId: options.couponChannelId,
  225. couponId: options.couponId,
  226. title: that.data.data.title ? that.data.data.title : '',
  227. });
  228. if (options.cardType) {
  229. that.setData({
  230. cardType: options.cardType
  231. })
  232. }
  233. var parmer = {
  234. url: config.api.couponDetail,
  235. data: {
  236. couponChannelId: options.couponChannelId
  237. }
  238. };
  239. Http.get(parmer)
  240. .then(res => {
  241. if (res.code == 200) {
  242. that.setData({
  243. showPage: true
  244. })
  245. }
  246. if (res.data.type == 100) {
  247. wx.setNavigationBarTitle({
  248. title: "卡详情"
  249. })
  250. }
  251. if (res.data.endTime) {
  252. that.countdown(res.data.endTime);
  253. //当前时间与优惠券下架时间做计算
  254. var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dd hh:mm:ss");
  255. if (util.timechuo(endTime).indexOf('-') == 0) {
  256. that.setData({
  257. endtime: "活动已结束",
  258. });
  259. } else {
  260. that.setData({
  261. endtime: util.timechuo(endTime)
  262. });
  263. }
  264. }
  265. wx.hideLoading();
  266. that.setData({
  267. data: res.data
  268. });
  269. if (res.data.validType == 1) {
  270. that.setData({
  271. validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dd"),
  272. validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dd"),
  273. });
  274. } else {
  275. if (res.data.validDays) {
  276. that.setData({
  277. validDays: res.data.validDays
  278. });
  279. }
  280. }
  281. }).catch(err => {
  282. wx.showToast({
  283. title: err.message,
  284. icon: 'none',
  285. duration: 2000,
  286. mask: false
  287. });
  288. })
  289. },
  290. /**
  291. * 邀请好友继续砍价
  292. */
  293. inviteFriend: function () {
  294. let that = this;
  295. that.orderFunc("discount");
  296. that.setData({
  297. showbutton1:true
  298. })
  299. },
  300. /**
  301. * 支付订单更新
  302. */
  303. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  304. let that = this;
  305. // 支付成功
  306. Http.post({
  307. url: config.api.payOrderUpdate,
  308. data: {
  309. payOrderId: payOrderId,
  310. orderId: orderId,
  311. status: status,
  312. reason: reason
  313. }
  314. })
  315. .then(res => {
  316. wx.hideLoading()
  317. // 有价券
  318. if (!type && type != 'free') {
  319. wx.navigateTo({
  320. url: `/pages/order/detail/index?orderId=${
  321. orderId
  322. }`
  323. });
  324. } else if (type == 'free') {
  325. wx.navigateTo({
  326. url: `/pages/order/detail/index?orderId=${
  327. orderId
  328. }`
  329. });
  330. }
  331. })
  332. .catch(err => {
  333. console.log(err);
  334. if (!type) {
  335. setTimeout(function () {
  336. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  337. }, 2000)
  338. }
  339. })
  340. },
  341. /**
  342. * 发起支付
  343. */
  344. orderFunc(discount) {
  345. console.log(discount)
  346. var that = this;
  347. Http.post({
  348. url: config.api.checkPhoneStatus,
  349. data: {}
  350. })
  351. .then(res => {
  352. if (discount == 'discount') {
  353. var data = {
  354. couponChannelId: "" + that.data.couponChannelId,
  355. couponId: "" + that.data.couponId,
  356. press: true
  357. }
  358. } else if (discount == 'discount1'){
  359. var data = {
  360. couponChannelId: "" + that.data.couponChannelId,
  361. couponId: "" + that.data.couponId,
  362. press: false
  363. }
  364. }else if (that.data.couponChannelId == null) {
  365. var data = {
  366. couponId: "" + that.data.couponId
  367. };
  368. } else {
  369. var data = {
  370. couponChannelId: "" + that.data.couponChannelId,
  371. couponId: "" + that.data.couponId,
  372. }
  373. }
  374. /**
  375. * orderSave 下单
  376. */
  377. return Http.post({
  378. url: config.api.orderSave,
  379. data: data
  380. });
  381. })
  382. .catch(err => {
  383. console.log(err);
  384. that.setData({
  385. showbutton: false,
  386. showbutton1:false
  387. })
  388. if (err.code == 2011) {
  389. wx.showToast({
  390. title: "商户信息没找到",
  391. image: './../../../assets/images/fail.png',
  392. duration: 2000,
  393. mask: false
  394. });
  395. } else if (err.code == 2013) {
  396. wx.showToast({
  397. title: "商户信息禁用",
  398. image: './../../../assets/images/fail.png',
  399. duration: 2000,
  400. mask: false
  401. });
  402. } else if (err.code == 3000) {
  403. wx.showToast({
  404. title: "库存不足",
  405. image: './../../../assets/images/fail.png',
  406. duration: 2000,
  407. mask: false
  408. });
  409. } else if (err.code == 3001) {
  410. wx.showToast({
  411. title: "超过限购条件",
  412. image: './../../../assets/images/fail.png',
  413. duration: 2000,
  414. mask: false
  415. });
  416. } else if (err.code == 3002) {
  417. wx.showToast({
  418. title: "订单失败",
  419. image: './../../../assets/images/fail.png',
  420. duration: 2000,
  421. mask: false
  422. });
  423. } else if (err.code == 3003) {
  424. wx.showToast({
  425. title: "订单不存在",
  426. image: './../../../assets/images/fail.png',
  427. duration: 2000,
  428. mask: false
  429. });
  430. } else if (err.code == 3004) {
  431. wx.showToast({
  432. title: "订单不存在",
  433. image: './../../../assets/images/fail.png',
  434. duration: 2000,
  435. mask: false
  436. });
  437. } else if (err.code == 4003) {
  438. wx.showToast({
  439. title: "卡券已作废",
  440. image: './../../../assets/images/fail.png',
  441. duration: 2000,
  442. mask: false
  443. });
  444. } else if (err.code == 11005) {
  445. /**
  446. * 将值传到用户手机号授权的页面
  447. *
  448. */
  449. wx.redirectTo({
  450. url: "/pages/getphoneInfo/index?couponChannelId=" +
  451. that.data.couponChannelId +
  452. "&couponId=" +
  453. that.data.couponId
  454. });
  455. } else if (err.code == 11006) {
  456. // 用户手机已加密
  457. wx.redirectTo({
  458. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  459. that.data.couponChannelId +
  460. "&couponId=" +
  461. that.data.couponId
  462. });
  463. } else {
  464. wx.showToast({
  465. title: err.message,
  466. icon: 'none',
  467. duration: 2000,
  468. mask: false
  469. });
  470. }
  471. })
  472. .then(res => {
  473. console.log(res)
  474. if (discount != 'discount') {
  475. if (typeof (res) != "undefined") {
  476. let orderId = "" + res.data.id;
  477. that.setData({
  478. orderId: orderId
  479. });
  480. if (res.data.payment > 0) {
  481. // 支付金额不为0
  482. /**
  483. * 支付订单创建
  484. */
  485. Http.post({
  486. url: config.api.payOrderCreate,
  487. data: {
  488. orderId: orderId
  489. }
  490. })
  491. .then(res => {
  492. var payOrderId = "" + res.data.payOrderId;
  493. wx.hideLoading();
  494. wx.requestPayment({
  495. timeStamp: res.data.timeStamp,
  496. nonceStr: res.data.nonceStr,
  497. package: res.data.package,
  498. signType: (res.data.signType) ? res.data.signType : "MD5",
  499. paySign: res.data.paySign,
  500. success: res => {
  501. wx.showLoading({
  502. title: '订单正在处理中...',
  503. })
  504. setTimeout(function () {
  505. wx.hideLoading()
  506. }, 5000)
  507. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  508. if (res.errMsg == "requestPayment:ok") {
  509. setTimeout(function () {
  510. wx.hideLoading();
  511. }, 2000);
  512. /**
  513. * 用户支付成功以后跳转到券包列表
  514. */
  515. if (that.data.cardType == 100) {
  516. wx.setStorage({
  517. key: 'couponNum2',
  518. data: "couponNum2"
  519. })
  520. } else if(that.data.data.type!=5){
  521. wx.setStorage({
  522. key: 'couponNum',
  523. data: "couponNum"
  524. })
  525. }
  526. }
  527. },
  528. fail: res => {
  529. /**
  530. * 支付失败,需要更新订单的状态
  531. */
  532. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  533. that.setData({
  534. showbutton: false
  535. })
  536. return;
  537. },
  538. complete: res => {}
  539. });
  540. /// End payment --------
  541. })
  542. .catch(err => {
  543. wx.showToast({
  544. title: err.message,
  545. icon: 'none',
  546. duration: 2000,
  547. mask: false
  548. });
  549. })
  550. } else {
  551. console.log(that.data.type)
  552. // 免费券
  553. that.payOrderUpdate(orderId, "0", 1, '', 'free');
  554. if (that.data.cardType == 100) {
  555. wx.setStorage({
  556. key: 'couponNum2',
  557. data: "couponNum2"
  558. })
  559. } else if (that.data.data.type != "5"){
  560. wx.setStorage({
  561. key: 'couponNum',
  562. data: "couponNum"
  563. })
  564. }
  565. }
  566. }
  567. } else {
  568. if(res){
  569. wx.navigateTo({
  570. url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.id}`,
  571. })
  572. }
  573. }
  574. })
  575. },
  576. onShow() {
  577. this.setData({
  578. showbutton: false
  579. })
  580. },
  581. onShareAppMessage: function (options) {
  582. var that = this;
  583. var shareObj = {
  584. title: that.data.data.title,
  585. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.couponId}`,
  586. success: function (res) {
  587. if (res.errMsg == 'shareAppMessage:ok') {}
  588. },
  589. fail: function (error) {
  590. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  591. }
  592. };
  593. // 来自页面内的按钮的转发
  594. if (options.from == 'button') {
  595. var eData = options.target.dataset.id;
  596. var couponId = options.target.dataset.couponid;
  597. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  598. }
  599. // 返回shareObj
  600. return shareObj;
  601. },
  602. /**
  603. * 用户登录
  604. */
  605. userLogin: function (couponChannelId, couponId, orderId) {
  606. var that = this;
  607. // 登录
  608. wx.login({
  609. success: ({
  610. code
  611. }) => {
  612. wx.getSystemInfo({
  613. success: function (res) {
  614. that.setData({
  615. systemInfo: JSON.stringify(res)
  616. })
  617. }
  618. })
  619. var usrdata = {
  620. appId: config.weapp.AppId,
  621. code: code,
  622. sceneAddress: app.globalData.sceneAddress,
  623. scene: that.data.scene,
  624. systemInfo: that.data.systemInfo
  625. };
  626. if (app.globalData.locationInfo) {
  627. usrdata = {
  628. appId: config.weapp.AppId,
  629. code: code,
  630. sceneAddress: app.globalData.sceneAddress,
  631. latitude: "" + app.globalData.locationInfo.latitude,
  632. longitude: "" + app.globalData.locationInfo.longitude,
  633. scene: that.data.scene,
  634. systemInfo: that.data.systemInfo
  635. };
  636. }
  637. Http.post({
  638. url: config.api.login,
  639. data: usrdata
  640. })
  641. .then(res => {
  642. that.setData({
  643. showPages: true
  644. })
  645. app.globalData.token = res.data.token;
  646. Http.setToken(res.data.token);
  647. if (couponChannelId && couponId || orderId) {
  648. that.checkuerstatus(couponChannelId, couponId, orderId);
  649. } else {
  650. that.checkuerstatus();
  651. }
  652. })
  653. .catch(err => {
  654. wx.showModal({
  655. title: '提示',
  656. showCancel: false,
  657. content: '登录失败,请重新尝试',
  658. success: function (res) {
  659. if (res.cancel) {
  660. //点击取消,默认隐藏弹框
  661. } else {
  662. //点击确定
  663. wx.reLaunch({
  664. url: '/pages/index/index',
  665. })
  666. }
  667. }
  668. })
  669. });
  670. }
  671. });
  672. },
  673. });