C端小程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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