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

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