C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

602 行
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') {
  348. var data = {
  349. couponChannelId: "" + that.data.couponChannelId,
  350. couponId: "" + that.data.couponId,
  351. press: true
  352. }
  353. } else if (discount == 'discount1'){
  354. var data = {
  355. couponChannelId: "" + that.data.couponChannelId,
  356. couponId: "" + that.data.couponId,
  357. press: false
  358. }
  359. }else if (that.data.couponChannelId == null) {
  360. var data = {
  361. couponId: "" + that.data.couponId
  362. };
  363. } else {
  364. var data = {
  365. couponChannelId: "" + that.data.couponChannelId,
  366. couponId: "" + that.data.couponId,
  367. }
  368. }
  369. /**
  370. * orderSave 下单
  371. */
  372. return Http.post({
  373. url: config.api.orderSave,
  374. data: data
  375. });
  376. })
  377. .catch(err => {
  378. console.log(err);
  379. that.setData({
  380. showbutton: false,
  381. showbutton1:false
  382. })
  383. if (err.code == 2011) {
  384. wx.showToast({
  385. title: "商户信息没找到",
  386. image: './../../../assets/images/fail.png',
  387. duration: 2000,
  388. mask: false
  389. });
  390. } else if (err.code == 2013) {
  391. wx.showToast({
  392. title: "商户信息禁用",
  393. image: './../../../assets/images/fail.png',
  394. duration: 2000,
  395. mask: false
  396. });
  397. } else if (err.code == 3000) {
  398. wx.showToast({
  399. title: "库存不足",
  400. image: './../../../assets/images/fail.png',
  401. duration: 2000,
  402. mask: false
  403. });
  404. } else if (err.code == 3001) {
  405. wx.showToast({
  406. title: "超过限购条件",
  407. image: './../../../assets/images/fail.png',
  408. duration: 2000,
  409. mask: false
  410. });
  411. } else if (err.code == 3002) {
  412. wx.showToast({
  413. title: "订单失败",
  414. image: './../../../assets/images/fail.png',
  415. duration: 2000,
  416. mask: false
  417. });
  418. } else if (err.code == 3003) {
  419. wx.showToast({
  420. title: "订单不存在",
  421. image: './../../../assets/images/fail.png',
  422. duration: 2000,
  423. mask: false
  424. });
  425. } else if (err.code == 3004) {
  426. wx.showToast({
  427. title: "订单不存在",
  428. image: './../../../assets/images/fail.png',
  429. duration: 2000,
  430. mask: false
  431. });
  432. } else if (err.code == 4003) {
  433. wx.showToast({
  434. title: "卡券已作废",
  435. image: './../../../assets/images/fail.png',
  436. duration: 2000,
  437. mask: false
  438. });
  439. } else if (err.code == 11005) {
  440. /**
  441. * 将值传到用户手机号授权的页面
  442. *
  443. */
  444. wx.redirectTo({
  445. url: "/pages/getphoneInfo/index?couponChannelId=" +
  446. that.data.couponChannelId +
  447. "&couponId=" +
  448. that.data.couponId
  449. });
  450. } else if (err.code == 11006) {
  451. // 用户手机已加密
  452. wx.redirectTo({
  453. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  454. that.data.couponChannelId +
  455. "&couponId=" +
  456. that.data.couponId
  457. });
  458. } else {
  459. wx.showToast({
  460. title: err.message,
  461. icon: 'none',
  462. duration: 2000,
  463. mask: false
  464. });
  465. }
  466. })
  467. .then(res => {
  468. console.log(res)
  469. if (discount != 'discount') {
  470. if (typeof (res) != "undefined") {
  471. let orderId = "" + res.data.id;
  472. that.setData({
  473. orderId: orderId
  474. });
  475. if (res.data.payment > 0) {
  476. // 支付金额不为0
  477. /**
  478. * 支付订单创建
  479. */
  480. Http.post({
  481. url: config.api.payOrderCreate,
  482. data: {
  483. orderId: orderId
  484. }
  485. })
  486. .then(res => {
  487. var payOrderId = "" + res.data.payOrderId;
  488. wx.hideLoading();
  489. wx.requestPayment({
  490. timeStamp: res.data.timeStamp,
  491. nonceStr: res.data.nonceStr,
  492. package: res.data.package,
  493. signType: (res.data.signType) ? res.data.signType : "MD5",
  494. paySign: res.data.paySign,
  495. success: res => {
  496. wx.showLoading({
  497. title: '订单正在处理中...',
  498. })
  499. setTimeout(function () {
  500. wx.hideLoading()
  501. }, 5000)
  502. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  503. if (res.errMsg == "requestPayment:ok") {
  504. setTimeout(function () {
  505. wx.hideLoading();
  506. }, 2000);
  507. /**
  508. * 用户支付成功以后跳转到券包列表
  509. */
  510. if (that.data.cardType == 100) {
  511. wx.setStorage({
  512. key: 'couponNum2',
  513. data: "couponNum2"
  514. })
  515. } else if(that.data.data.type!=5){
  516. wx.setStorage({
  517. key: 'couponNum',
  518. data: "couponNum"
  519. })
  520. }
  521. }
  522. },
  523. fail: res => {
  524. /**
  525. * 支付失败,需要更新订单的状态
  526. */
  527. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  528. that.setData({
  529. showbutton: false
  530. })
  531. return;
  532. },
  533. complete: res => {}
  534. });
  535. /// End payment --------
  536. })
  537. .catch(err => {
  538. wx.showToast({
  539. title: err.message,
  540. icon: 'none',
  541. duration: 2000,
  542. mask: false
  543. });
  544. })
  545. } else {
  546. console.log(that.data.type)
  547. // 免费券
  548. that.payOrderUpdate(orderId, "0", 1, '', 'free');
  549. if (that.data.cardType == 100) {
  550. wx.setStorage({
  551. key: 'couponNum2',
  552. data: "couponNum2"
  553. })
  554. } else if (that.data.data.type != "5"){
  555. wx.setStorage({
  556. key: 'couponNum',
  557. data: "couponNum"
  558. })
  559. }
  560. }
  561. }
  562. } else {
  563. if(res){
  564. wx.navigateTo({
  565. url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.id}`,
  566. })
  567. }
  568. }
  569. })
  570. },
  571. onShow() {
  572. this.setData({
  573. showbutton: false
  574. })
  575. },
  576. onShareAppMessage: function (options) {
  577. var that = this;
  578. var shareObj = {
  579. title: that.data.data.title,
  580. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.couponId}`,
  581. success: function (res) {
  582. if (res.errMsg == 'shareAppMessage:ok') {}
  583. },
  584. fail: function (error) {
  585. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  586. }
  587. };
  588. // 来自页面内的按钮的转发
  589. if (options.from == 'button') {
  590. var eData = options.target.dataset.id;
  591. var couponId = options.target.dataset.couponid;
  592. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  593. }
  594. // 返回shareObj
  595. return shareObj;
  596. },
  597. });