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.

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