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

454 行
13 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. carList: [],
  9. couponChannelId: null,
  10. couponId: null,
  11. orderId: "",
  12. day: "",
  13. hour: "",
  14. minute: ""
  15. },
  16. phone: function () {
  17. let that = this;
  18. console.log(that.data);
  19. if (that.data.data.merchantLinkPhone) {
  20. wx.makePhoneCall({
  21. phoneNumber: that.data.data.merchantLinkPhone
  22. });
  23. }
  24. },
  25. onLoad(options) {
  26. let that = this;
  27. console.log(options);
  28. console.log("couponChannelId" + options.couponChannelId);
  29. console.log("couponId" + options.couponId);
  30. console.log("传的变量的名字叫couponId");
  31. wx.showLoading({
  32. title: "加载中..."
  33. });
  34. /**
  35. * 暂时注销
  36. * this.orderFunc()
  37. */
  38. // if (options.flag) {
  39. // // this.orderFunc();
  40. // }
  41. // else {
  42. // }
  43. that.setData({
  44. couponChannelId: options.couponChannelId,
  45. couponId: options.couponId
  46. });
  47. var parmer = {
  48. url: config.api.couponDetail,
  49. data: {
  50. couponChannelId: options.couponChannelId
  51. }
  52. };
  53. Http.get(parmer).then(res => {
  54. console.log(res);
  55. console.log("details");
  56. //当前时间与优惠券下架时间做计算
  57. var endTime = util.fmtDate(res.data.endTime);
  58. util.timechuo(endTime);
  59. that.setData({
  60. endtime: util.timechuo(endTime)
  61. });
  62. wx.hideLoading();
  63. that.setData({
  64. data: res.data
  65. });
  66. });
  67. // }
  68. },
  69. /**
  70. * 支付订单更新
  71. */
  72. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  73. // 支付成功
  74. Http.post({
  75. url: config.api.payOrderUpdate,
  76. data: {
  77. payOrderId: payOrderId,
  78. orderId: orderId,
  79. status: status,
  80. reason: reason
  81. }
  82. })
  83. .then(res => {
  84. console.log("payOrderUpdate then", res);
  85. // wx.showToast({
  86. // title: "购买成功",
  87. // duration: 2500
  88. // });
  89. })
  90. .catch(err => {
  91. console.log("payOrderUpdate catch", err);
  92. });
  93. },
  94. /**
  95. * 拉取车牌列表
  96. */
  97. init: function () {
  98. var that = this
  99. app.parkInitCallback = token => {
  100. that.initPark()
  101. that.initUsrCarList()
  102. if (!app.globalData.carLogin) {
  103. // 共同登录
  104. Http.post({
  105. url: config.api.carInit,
  106. data: {
  107. phone: app.globalData.phone
  108. }
  109. }).then(res => {
  110. app.globalData.carLogin = true
  111. app.globalData.parkVendor = res.data.vendor
  112. if (res.data.token != "undefined") {
  113. app.globalData.etcpToken = res.data.token
  114. console.log("etcpToken", app.globalData.etcpToken)
  115. }
  116. // 获取 停车费
  117. that.getStopFee()
  118. })
  119. }
  120. }
  121. if (app.globalData.token && app.globalData.token != null) {
  122. app.parkInitCallback(app.globalData.token)
  123. }
  124. },
  125. getStopFee: function () {
  126. var that = this
  127. // carLogin
  128. for (var i = 0; i < that.data.carList.length; i++) {
  129. that.getStopFeeItem(that.data.carList[i], i)
  130. }
  131. // console.log(JSON.stringify(getStopFeeItem))
  132. },
  133. initPark: function () {
  134. var that = this
  135. // 车场信息获取
  136. Http.get({
  137. url: config.api.getParkInfo,
  138. data: {}
  139. })
  140. .then(res => {
  141. console.log(res)
  142. that.setData({
  143. park: res.data,
  144. })
  145. })
  146. },
  147. initUsrCarList: function () {
  148. var that = this
  149. // 绑定车获取
  150. Http.get({
  151. url: config.api.getUserCarList,
  152. data: {}
  153. }).then(res => {
  154. console.log('>>>>>>>>>>>>>>>>>1' + JSON.stringify(res));
  155. that.setData({
  156. carList: res.data
  157. })
  158. })
  159. },
  160. /**
  161. * 发起支付
  162. */
  163. orderFunc() {
  164. var that = this;
  165. wx.showLoading({
  166. title: "加载中..."
  167. });
  168. if (that.data.data.type == 6) {
  169. this.initUsrCarList()
  170. // 领取 5.停车券
  171. // TODO 选取用户下的车牌
  172. } else {
  173. Http.post({
  174. url: config.api.checkPhoneStatus,
  175. data: {}
  176. })
  177. .then(res => {
  178. console.log(res);
  179. var data = {
  180. couponChannelId: "" + that.data.couponChannelId,
  181. couponId: "" + that.data.couponId
  182. };
  183. if (that.data.couponChannelId == null) {
  184. var data = {
  185. couponId: "" + that.data.couponId
  186. };
  187. }
  188. /**
  189. * orderSave 下单
  190. */
  191. return Http.post({
  192. url: config.api.orderSave,
  193. data: data
  194. });
  195. })
  196. .catch(err => {
  197. console.log(err);
  198. console.log(that.data.couponChannelId);
  199. console.log(that.data.couponId);
  200. //console.log("我即将要跳转到用户手机号授权的页面");
  201. if (err.code == "2011") {
  202. wx.showToast({
  203. title: "商户信息没找到",
  204. image: "./../../../assets/img/fail.png",
  205. duration: 2000,
  206. mask: false
  207. });
  208. }
  209. else if (err.code == "2013") {
  210. wx.showToast({
  211. title: "商户信息禁用",
  212. image: "./../../../assets/img/fail.png",
  213. duration: 2000,
  214. mask: false
  215. });
  216. }
  217. else if (err.code == "3000") {
  218. wx.showToast({
  219. title: "库存不足",
  220. image: "./../../../assets/img/fail.png",
  221. duration: 2000,
  222. mask: false
  223. });
  224. }
  225. else if (err.code == "3001") {
  226. wx.showToast({
  227. title: "领取达到上限",
  228. image: "./../../../assets/img/fail.png",
  229. duration: 2000,
  230. mask: false
  231. });
  232. }
  233. else if (err.code == "3002") {
  234. wx.showToast({
  235. title: "订单失败",
  236. image: "./../../../assets/img/fail.png",
  237. duration: 2000,
  238. mask: false
  239. });
  240. }
  241. else if (err.code == "3003") {
  242. wx.showToast({
  243. title: "订单不存在",
  244. image: "./../../../assets/img/fail.png",
  245. duration: 2000,
  246. mask: false
  247. });
  248. }
  249. else if (err.code == "3004") {
  250. wx.showToast({
  251. title: "订单不存在",
  252. image: "./../../../assets/img/fail.png",
  253. duration: 2000,
  254. mask: false
  255. });
  256. }
  257. else if (err.code == "4003") {
  258. wx.showToast({
  259. title: "卡券已作废",
  260. image: "./../../../assets/img/fail.png",
  261. duration: 2000,
  262. mask: false
  263. });
  264. }
  265. else if (err.code == 11005) {
  266. // 用户手机未授权
  267. /**
  268. * 将值传到用户手机号授权的页面
  269. *
  270. */
  271. wx.redirectTo({
  272. url: "/pages/getphoneInfo/index?couponChannelId=" +
  273. that.data.couponChannelId +
  274. "&couponId=" +
  275. that.data.couponId
  276. });
  277. }
  278. else if (err.code == 11006) {
  279. // 用户手机已加密
  280. wx.redirectTo({
  281. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  282. that.data.couponChannelId +
  283. "&couponId=" +
  284. that.data.couponId
  285. });
  286. } else {
  287. wx.showToast({
  288. title: err.message,
  289. image: "./../../../assets/img/fail.png",
  290. duration: 2000,
  291. mask: false
  292. });
  293. }
  294. })
  295. .then(res => {
  296. console.log(res);
  297. console.log("点击领取卡券一直在loading");
  298. if (typeof (res) != "undefined") {
  299. console.log("", res);
  300. let orderId = "" + res.data.id;
  301. that.setData({
  302. orderId: orderId
  303. });
  304. if (res.data.payment > 0) {
  305. // 支付金额不为0
  306. /**
  307. * 支付订单创建
  308. */
  309. Http.post({
  310. url: config.api.payOrderCreate,
  311. data: {
  312. orderId: orderId
  313. }
  314. }).then(res => {
  315. console.log(res);
  316. /// Begin payment ----
  317. var payOrderId = "" + res.data.payOrderId;
  318. wx.hideLoading();
  319. wx.requestPayment({
  320. timeStamp: res.data.timeStamp,
  321. nonceStr: res.data.nonceStr,
  322. package: res.data.package,
  323. signType: (res.data.signType) ? res.data.signType : "MD5",
  324. paySign: res.data.paySign,
  325. success: res => {
  326. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  327. console.log(res);
  328. if (res.errMsg == "requestPayment:ok") {
  329. wx.showTabBarRedDot({
  330. index: 2
  331. })
  332. wx.showToast({
  333. title: "购买成功",
  334. image: "./../../../assets/img/success.png",
  335. duration: 1000,
  336. mask: false,
  337. success: function () {
  338. setTimeout(function () {
  339. wx.hideLoading();
  340. }, 1600);
  341. setTimeout(() => {
  342. wx.navigateTo({
  343. url: `/pages/order/detail/index?orderId=${
  344. that.data.orderId
  345. }`
  346. });
  347. }, 1000)
  348. }
  349. });
  350. /**
  351. * 用户支付成功以后跳转到券包列表
  352. */
  353. wx.setStorage({
  354. key: 'couponNum',
  355. data: "couponNum",
  356. })
  357. if (that.data.data.type == 5) {
  358. setTimeout(() => {
  359. wx.switchTab({
  360. url: '/pages/passCar/passCar'
  361. });
  362. }, 1600);
  363. // this.initUsrCarList()
  364. // 领取 5.停车券
  365. // TODO 选取用户下的车牌
  366. }
  367. }
  368. },
  369. fail: res => {
  370. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  371. console.log(res);
  372. // wx.showToast({
  373. // title: "支付失败",
  374. // image: "./../../../assets/img/fail.png",
  375. // duration: 2000,
  376. // mask: false
  377. // });
  378. return;
  379. },
  380. complete: res => {
  381. // console.log(res);
  382. // console.log("支付完成");
  383. // if (res.errMsg == "requestPayment:ok") {
  384. // } else {
  385. // wx.showToast({
  386. // title: "支付失败",
  387. // image: "./../../../assets/img/fail.png",
  388. // duration: 2000,
  389. // mask: false
  390. // });
  391. // }
  392. // return;
  393. }
  394. });
  395. /// End payment --------
  396. ///// virtual pay
  397. //var payOrderId = "" + res.data.out_trade_no;
  398. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  399. ///// end virtual pay
  400. });
  401. } else {
  402. // 免费券
  403. that.payOrderUpdate(orderId, "0", 1);
  404. console.log("小红点 小红点 小红点")
  405. wx.setStorage({
  406. key: 'couponNum',
  407. data: "couponNum",
  408. })
  409. wx.showToast({
  410. title: "领取成功",
  411. image: "./../../../assets/img/success.png",
  412. duration: 1000,
  413. mask: false,
  414. success:function(){
  415. setTimeout(function(){
  416. wx.navigateTo({
  417. url: `/pages/order/detail/index?orderId=${
  418. that.data.orderId
  419. }`
  420. });
  421. },1000)
  422. }
  423. });
  424. //////////////////////////停车券
  425. console.log(that.data.data.type)
  426. if (that.data.data.type == 5) {
  427. setTimeout(() => {
  428. wx.switchTab({
  429. url: '/pages/passCar/passCar'
  430. });
  431. }, 1600);
  432. }
  433. }
  434. }
  435. })
  436. .catch(err => {
  437. console.log("ERR", err);
  438. });
  439. }
  440. }
  441. });