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

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