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

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