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.

571 line
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. that.setData({
  270. validDays: res.data.validDays
  271. });
  272. }
  273. }).catch(err => {
  274. wx.showToast({
  275. title: err.message,
  276. icon: 'none',
  277. duration: 2000,
  278. mask: false
  279. });
  280. })
  281. },
  282. /**
  283. * 支付订单更新
  284. */
  285. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  286. let that = this;
  287. // 支付成功
  288. Http.post({
  289. url: config.api.payOrderUpdate,
  290. data: {
  291. payOrderId: payOrderId,
  292. orderId: orderId,
  293. status: status,
  294. reason: reason
  295. }
  296. })
  297. .then(res => {
  298. wx.hideLoading()
  299. // 有价券
  300. if (!type&&type!='free') {
  301. wx.navigateTo({
  302. url: `/pages/order/detail/index?orderId=${
  303. orderId
  304. }`
  305. });
  306. }else if(type=='free'){
  307. wx.navigateTo({
  308. url: `/pages/order/detail/index?orderId=${
  309. orderId
  310. }`
  311. });
  312. }
  313. })
  314. .catch(err => {
  315. console.log(err);
  316. if (!type) {
  317. setTimeout(function () {
  318. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  319. }, 2000)
  320. }
  321. })
  322. },
  323. /**
  324. * 发起支付
  325. */
  326. orderFunc(flag) {
  327. var that = this;
  328. // wx.showLoading({
  329. // title: "加载中..."
  330. // });
  331. Http.post({
  332. url: config.api.checkPhoneStatus,
  333. data: {}
  334. })
  335. .then(res => {
  336. var data = {
  337. couponChannelId: "" + that.data.couponChannelId,
  338. couponId: "" + that.data.couponId
  339. };
  340. if (that.data.couponChannelId == null) {
  341. var data = {
  342. couponId: "" + that.data.couponId
  343. };
  344. }
  345. /**
  346. * orderSave 下单
  347. */
  348. return Http.post({
  349. url: config.api.orderSave,
  350. data: data
  351. });
  352. })
  353. .catch(err => {
  354. if (err.code == "2011") {
  355. wx.showToast({
  356. title: "商户信息没找到",
  357. image:'./../../../assets/images/fail.png',
  358. duration: 2000,
  359. mask: false
  360. });
  361. } else if (err.code == "2013") {
  362. wx.showToast({
  363. title: "商户信息禁用",
  364. image:'./../../../assets/images/fail.png',
  365. duration: 2000,
  366. mask: false
  367. });
  368. } else if (err.code == "3000") {
  369. wx.showToast({
  370. title: "库存不足",
  371. image:'./../../../assets/images/fail.png',
  372. duration: 2000,
  373. mask: false
  374. });
  375. } else if (err.code == "3001") {
  376. wx.showToast({
  377. title: "领取达到上限",
  378. image:'./../../../assets/images/fail.png',
  379. duration: 2000,
  380. mask: false
  381. });
  382. } else if (err.code == "3002") {
  383. wx.showToast({
  384. title: "订单失败",
  385. image:'./../../../assets/images/fail.png',
  386. duration: 2000,
  387. mask: false
  388. });
  389. } else if (err.code == "3003") {
  390. wx.showToast({
  391. title: "订单不存在",
  392. image:'./../../../assets/images/fail.png',
  393. duration: 2000,
  394. mask: false
  395. });
  396. } else if (err.code == "3004") {
  397. wx.showToast({
  398. title: "订单不存在",
  399. image:'./../../../assets/images/fail.png',
  400. duration: 2000,
  401. mask: false
  402. });
  403. } else if (err.code == "4003") {
  404. wx.showToast({
  405. title: "卡券已作废",
  406. image:'./../../../assets/images/fail.png',
  407. duration: 2000,
  408. mask: false
  409. });
  410. } else if (err.code == 11005) {
  411. /**
  412. * 将值传到用户手机号授权的页面
  413. *
  414. */
  415. wx.redirectTo({
  416. url: "/pages/getphoneInfo/index?couponChannelId=" +
  417. that.data.couponChannelId +
  418. "&couponId=" +
  419. that.data.couponId
  420. });
  421. } else if (err.code == 11006) {
  422. // 用户手机已加密
  423. wx.redirectTo({
  424. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  425. that.data.couponChannelId +
  426. "&couponId=" +
  427. that.data.couponId
  428. });
  429. } else {
  430. wx.showToast({
  431. title: err.message,
  432. icon: 'none',
  433. duration: 2000,
  434. mask: false
  435. });
  436. }
  437. })
  438. .then(res => {
  439. if (typeof (res) != "undefined") {
  440. let orderId = "" + res.data.id;
  441. that.setData({
  442. orderId: orderId
  443. });
  444. if (res.data.payment > 0) {
  445. // 支付金额不为0
  446. /**
  447. * 支付订单创建
  448. */
  449. Http.post({
  450. url: config.api.payOrderCreate,
  451. data: {
  452. orderId: orderId
  453. }
  454. }).then(res => {
  455. var payOrderId = "" + res.data.payOrderId;
  456. wx.hideLoading();
  457. wx.requestPayment({
  458. timeStamp: res.data.timeStamp,
  459. nonceStr: res.data.nonceStr,
  460. package: res.data.package,
  461. signType: (res.data.signType) ? res.data.signType : "MD5",
  462. paySign: res.data.paySign,
  463. success: res => {
  464. wx.showLoading({
  465. title: '订单正在处理中...',
  466. })
  467. setTimeout(function(){
  468. wx.hideLoading()
  469. },5000)
  470. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  471. if (res.errMsg == "requestPayment:ok") {
  472. setTimeout(function () {
  473. wx.hideLoading();
  474. }, 2000);
  475. /**
  476. * 用户支付成功以后跳转到券包列表
  477. */
  478. if(that.data.cardType == 7){
  479. wx.setStorage({
  480. key: 'couponNum2',
  481. data: "couponNum2"
  482. })
  483. }else{
  484. wx.setStorage({
  485. key: 'couponNum',
  486. data: "couponNum"
  487. })
  488. }
  489. }
  490. },
  491. fail: res => {
  492. /**
  493. * 支付失败,需要更新订单的状态
  494. */
  495. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  496. that.setData({
  497. showbutton: false
  498. })
  499. return;
  500. },
  501. complete: res => {}
  502. });
  503. /// End payment --------
  504. })
  505. .catch(err => {
  506. wx.showToast({
  507. title: err.message,
  508. icon: 'none',
  509. duration: 2000,
  510. mask: false
  511. });
  512. })
  513. } else {
  514. // 免费券
  515. that.payOrderUpdate(orderId, "0", 1, '', 'free');
  516. if(that.data.cardType == 7){
  517. wx.setStorage({
  518. key: 'couponNum2',
  519. data: "couponNum2"
  520. })
  521. }else{
  522. wx.setStorage({
  523. key: 'couponNum',
  524. data: "couponNum"
  525. })
  526. }
  527. }
  528. }
  529. })
  530. .catch(err => {
  531. wx.showToast({
  532. title: err.message,
  533. icon: 'none',
  534. duration: 2000,
  535. mask: false
  536. });
  537. })
  538. },
  539. onShow(){
  540. this.setData({
  541. showbutton:false
  542. })
  543. },
  544. onShareAppMessage: function (options) {
  545. var that = this;
  546. var shareObj = {
  547. title: that.data.data.title,
  548. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.couponId}`,
  549. success: function (res) {
  550. if (res.errMsg == 'shareAppMessage:ok') {}
  551. },
  552. fail: function (error) {
  553. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  554. }
  555. };
  556. // 来自页面内的按钮的转发
  557. if (options.from == 'button') {
  558. var eData = options.target.dataset.id;
  559. var couponId = options.target.dataset.couponid;
  560. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  561. }
  562. // 返回shareObj
  563. return shareObj;
  564. },
  565. });