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.

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