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.

recharge.vue 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. <template>
  2. <view class="wrapper">
  3. <view class="account">
  4. <image :src="`${$imgUrl}common/bg-recharge.png`" class="head-bg"></image>
  5. <view class="account-text">
  6. <view class="balance">
  7. <!-- <text class="balance-tit">账户余额</text> -->
  8. <!-- <text class="balance-val">20.00元</text> -->
  9. </view>
  10. <view class="right-box">
  11. <view class="no">账户编号:000100</view>
  12. <view class="btn btn-record">账户充值记录</view>
  13. </view>
  14. </view>
  15. </view>
  16. <view class="input-box">
  17. <input placeholder="请输入金额" class="input" />
  18. <view class="tips">最低充值金额不小于10元</view>
  19. </view>
  20. <view class="tabs">
  21. <view class="tab-tit">在线充值</view>
  22. <view class="tab">
  23. <view class="item">50元</view>
  24. <view class="item active">100元</view>
  25. <view class="item">200元</view>
  26. <view class="item">300元</view>
  27. <view class="item">500元</view>
  28. <view class="item">1000元</view>
  29. </view>
  30. </view>
  31. <view class="btn btn-primary" @click="rechargeAction">立即充值</view>
  32. </view>
  33. </template>
  34. <script setup lang="ts">
  35. import {
  36. stringToJson
  37. } from "@/utils/network/encryption";
  38. import {
  39. onLoad,
  40. onShow
  41. } from "@dcloudio/uni-app";
  42. import {
  43. quanCheck,
  44. quanApply,
  45. quanXf,
  46. cardCzPayResultCheck,
  47. cardCzApply,
  48. cardCzPay,
  49. cardCzPayResult,
  50. cardCzXFCheck,
  51. quanConfirm,
  52. wechatAppID,
  53. wechatPayConfigId,
  54. wechatSecret,
  55. } from "@/utils/network/api.js";
  56. import {
  57. request
  58. } from "@/utils/network/request.js";
  59. import {
  60. reactive
  61. } from "vue";
  62. import {
  63. msg
  64. } from "@/utils/utils";
  65. import {
  66. getItem,
  67. StorageKeys,
  68. setItem
  69. } from "@/utils/storage";
  70. const datas = require("../../static/etcUtil/datas.js");
  71. const bluetoothUtil = require("../../static/etcUtil/index.js");
  72. const cmd = require("../../static/etcUtil/cmdConfig.js");
  73. const tools = require("../../static/etcUtil/tools.js");
  74. const state = reactive({
  75. openid: "",
  76. connectSuccess: undefined,
  77. fee: 1,
  78. orderNum: '',
  79. cardId: '', //卡号 需要传参
  80. mockpreBalance: 2000 //fix:模拟余额 目前没有检测状态接口,第一次会模拟圈层检测来检测状态
  81. });
  82. const card = reactive({
  83. cardId: "",
  84. netId: "",
  85. cardType: "",
  86. startTime: "",
  87. endTime: "",
  88. userName: "",
  89. idNum: "",
  90. vehiclePlate: "",
  91. vehiclePlateColor: "",
  92. color: "",
  93. version: "",
  94. type: "",
  95. favourable: "",
  96. money: undefined,
  97. v_userType: "",
  98. });
  99. onLoad((option) => {
  100. /*获取openId 用途:(用户支付)*/
  101. getOpenID();
  102. /*传参*/
  103. state.cardId = option.cardId
  104. console.log(state.cardId);
  105. console.log('888888888')
  106. state.connectSuccess = option.connectSuccess
  107. state.orderNum = getItem("orderNum")
  108. if (state.connectSuccess === '1') {
  109. /*读卡*/
  110. getCardId()
  111. }
  112. });
  113. /*点击充值按钮*/
  114. const rechargeAction = () => {
  115. /*进行圈层检测,此处检测仅为校验圈层状态来决定去哪里*/
  116. console.log('进行圈层检测')
  117. quanCheckAction().then((val: any) => {
  118. //如果 圈层检测正常
  119. if (val.chargeStatus === 1) {
  120. console.log(`进行充值检测`)
  121. czCheckAction().then((checkResult: any) => {
  122. //判断订单逻辑如果有订单充值 则直接拿到订单去链接蓝牙
  123. if (checkResult.orders && checkResult.orders.length > 0) {
  124. //拿到订单,存起来
  125. state.orderNum = checkResult.orders[0].orderNum
  126. setItem("orderNum", state.orderNum)
  127. //链接蓝牙
  128. uni.navigateTo({
  129. url: `/pages/bluetooth/device-active-step3?routeType=2&&cardId=${state.cardId}`
  130. });
  131. } else {
  132. console.log(`进行充值申请`)
  133. cardCzApplyAction().then((applyResult: any) => {
  134. //拿到订单 存起来
  135. state.orderNum = applyResult.orderNum
  136. setItem("orderNum", state.orderNum)
  137. //如果订单没有支付 走支付
  138. if (applyResult.orderStatus === 'ORDER_NOT_PAY') {
  139. //走支付
  140. console.log('走支付')
  141. wxPayment()
  142. }
  143. })
  144. }
  145. })
  146. } else {
  147. //走蓝牙进行修复
  148. uni.navigateTo({
  149. url: `/pages/bluetooth/device-active-step3?routeType=2&&cardId=${state.cardId}`,
  150. });
  151. }
  152. })
  153. }
  154. /*读卡*/
  155. const getCardId = () => {
  156. console.log('======获取卡信息======')
  157. let cmdArr = [
  158. cmd.HOME_DIRECTORY,
  159. //选择主目
  160. cmd.APPLICATION_DIRECTORY,
  161. //选择文件1001--DF01联网收费应用目录
  162. cmd.CMD_READBINARY,
  163. //15文件--卡片发行基本数据文件
  164. cmd.CMD_GETBALANCE
  165. //钱包
  166. ];
  167. tools.showLoadingAlert("正在执行指令");
  168. bluetoothUtil.transCmd(cmdArr, "10", function(res) {
  169. tools.hideLoadingAlert();
  170. //10:写卡 20:写OBU
  171. let str = res[2].substring(res[2].length - 4, res[2].length);
  172. let str3 = res[3].substring(res[3].length - 4, res[3].length);
  173. if (str == "9000" || str3 == "9000") {
  174. if (res[2].length > 86 || res[3] >= 12) {
  175. card.cardId = res[2].substring(20, 40); //卡号
  176. card.startTime = res[2].substring(40, 48);
  177. card.endTime = res[2].substring(48, 56);
  178. card.version = res[2].substring(18, 19) >= 4 ? "4x" : "2x";
  179. card.netId = res[2].substring(20, 24);
  180. card.cardType = res[2].substring(28, 29) == 23 ? 1 : 2;
  181. card.vehiclePlateColor = parseInt(res[2].substring(82, 84), 16);
  182. card.money = parseInt(parseInt(res[3].substring(0, 8), 16), 10),
  183. console.log('======卡信息======', card)
  184. if (card.cardId !== state.cardId) {
  185. msg('设备卡信息与当前充值卡号不匹配,请核对卡号')
  186. return;
  187. }
  188. quanCheckActionTrue().then(val => {
  189. checkQuanCengEvent(val)
  190. })
  191. } else {
  192. console.error("CMD_READBINARY指令长度不符" + res[2])
  193. tools.hideLoadingAlert();
  194. }
  195. }
  196. console.error("CMD_READBINARY指令长度不符" + res[2])
  197. tools.hideLoadingAlert();
  198. });
  199. };
  200. const checkQuanCengEvent = (val: any) => {
  201. if (val.chargeStatus === 1) {
  202. let cmdArr = val.command.split(",");
  203. uni.showLoading({
  204. title: "写入中"
  205. })
  206. bluetoothUtil.transCmd(cmdArr, "10", function(res) {
  207. let response = res.toString();
  208. var dic = {
  209. command: val.command,
  210. cosResponse: response,
  211. }
  212. uni.hideLoading()
  213. cardCzXFCheckAction().then(xfRes => {
  214. quanApplyAction(dic).then(value => {
  215. console.log('圈层申请完后的结果')
  216. console.log(value)
  217. //圈存初始化验证通过 , 进行圈存
  218. if (value.commandType === 2) {
  219. uanConfirmAction(value).then(
  220. confirmResult => {
  221. msg('充值成功')
  222. })
  223. } else {
  224. msg('圈存初始化指令验证失败, 重新初始化')
  225. }
  226. })
  227. })
  228. });
  229. } else {
  230. let cmdArr = val.command.split(",");
  231. uni.showLoading({
  232. title: "写入中"
  233. })
  234. bluetoothUtil.transCmd(cmdArr, "10", function(res) {
  235. var status = res[1].substring(res[1].length - 4,
  236. res[1].length);
  237. console.log('打印状态')
  238. console.log(status)
  239. if (status === '9000') {
  240. console.log('修复指令入参')
  241. let response = res.toString();
  242. var dic = {
  243. command: val.command,
  244. cosResponse: response,
  245. rechargeId: val.rechargeId
  246. }
  247. quanFixAction(dic).then(value => {
  248. console.log(value)
  249. console.log('修复结果返回')
  250. var fixStatus = value.fixStatus;
  251. //圈存修复COS指令Response信息不足,重新进行修复初始化
  252. if (fixStatus === 3) {
  253. let xfcmdArr = value.command.split(",");
  254. bluetoothUtil.transCmd(xfcmdArr, "10", function(resValueData) {
  255. var status = resValueData[1].substring(resValueData[1].length -
  256. 4,
  257. resValueData[1].length);
  258. console.log('打印状态')
  259. console.log(status)
  260. if (status === '9000') {
  261. console.log(resValueData)
  262. var valueResponse = resValueData.toString();
  263. console.log(response)
  264. var applyDic = {
  265. command: value.command,
  266. cosResponse: valueResponse,
  267. rechargeId: value.rechargeId
  268. }
  269. console.log('消费成功')
  270. quanApplyAction(applyDic).then(
  271. applyValue => {
  272. uanConfirmAction(applyValue).then(
  273. confirmResult => {
  274. msg('充值成功')
  275. })
  276. })
  277. }
  278. })
  279. } else if (fixStatus === 2) {
  280. uanConfirmSucessAction(value).then(
  281. confirmResult => {
  282. msg('充值成功')
  283. })
  284. } else if (fixStatus === 1) {
  285. }
  286. })
  287. uni.hideLoading()
  288. }
  289. });
  290. }
  291. }
  292. /*透传*/
  293. const transCmd = (cmd) => {
  294. bluetoothUtil.transCmd(cmd, "10", function(res) {
  295. var status = res[1].substring(res[1].length -
  296. 4,
  297. res[1].length);
  298. if (status === '9000') {
  299. return res.toString()
  300. }
  301. })
  302. }
  303. /*圈层检测 -假数据校验*/
  304. const quanCheckAction = () => {
  305. var data = {
  306. cardId: state.cardId, //卡号
  307. fee: state.fee,
  308. preBalance: state.mockpreBalance, //假余额
  309. tradeType: 14,
  310. };
  311. const options = {
  312. type: 2,
  313. data: data,
  314. method: "POST",
  315. showLoading: true,
  316. };
  317. return new Promise(async (resolve, reject) => {
  318. const res = await request(quanCheck, options);
  319. const data = stringToJson(res.bizContent);
  320. resolve(data);
  321. }).catch((error) => {
  322. reject(error);
  323. });
  324. }
  325. const quanCheckActionTrue = () => {
  326. console.log('进行真实圈层检测')
  327. var data = {
  328. cardId: card.cardId,
  329. fee: state.fee,
  330. preBalance: card.money,
  331. tradeType: 14,
  332. };
  333. const options = {
  334. type: 2,
  335. data: data,
  336. method: "POST",
  337. showLoading: true,
  338. };
  339. return new Promise(async (resolve, reject) => {
  340. const res = await request(quanCheck, options);
  341. const data = stringToJson(res.bizContent);
  342. resolve(data);
  343. }).catch((error) => {
  344. reject(error);
  345. });
  346. }
  347. /*充值消费*/
  348. const cardCzXFCheckAction = () => {
  349. var data = {
  350. cardId: card.cardId, //修复初始化的指令
  351. openId: getItem(StorageKeys.OpenId), //修复初始化结果
  352. orderNum: state.orderNum, //充值流水号
  353. };
  354. const options = {
  355. type: 2,
  356. data: data,
  357. method: "POST",
  358. showLoading: true,
  359. };
  360. return new Promise(async (resolve, reject) => {
  361. const res = await request(cardCzXFCheck, options);
  362. const data = stringToJson(res.bizContent);
  363. resolve(data);
  364. }).catch((error) => {
  365. reject(error);
  366. });
  367. }
  368. /*圈存修复*/
  369. const quanFixAction = (val) => {
  370. var data = {
  371. command: val.command, //修复初始化的指令
  372. cosResponse: val.cosResponse, //修复初始化结果
  373. rechargeId: val.rechargeId, //充值流水号
  374. };
  375. const options = {
  376. type: 2,
  377. data: data,
  378. method: "POST",
  379. showLoading: true,
  380. };
  381. return new Promise(async (resolve, reject) => {
  382. const res = await request(quanXf, options);
  383. const data = stringToJson(res.bizContent);
  384. resolve(data);
  385. }).catch((error) => {
  386. reject(error);
  387. });
  388. }
  389. /*圈层申请*/
  390. const quanApplyAction = (data) => {
  391. var form = {
  392. cardId: card.cardId,
  393. fee: state.fee,
  394. preBalance: card.money,
  395. tradeType: 14,
  396. command: data.command,
  397. cosResponse: data.cosResponse,
  398. orderId: state.orderNum,
  399. rechargeId: data.rechargeId
  400. };
  401. const options = {
  402. type: 2,
  403. data: form,
  404. method: "POST",
  405. showLoading: true,
  406. };
  407. return new Promise(async (resolve, reject) => {
  408. const res = await request(quanApply, options);
  409. const data = stringToJson(res.bizContent);
  410. resolve(data);
  411. }).catch((error) => {
  412. reject(error);
  413. });
  414. }
  415. /*圈层确认*/
  416. const uanConfirmAction = (data) => {
  417. console.log('圈层确认进入')
  418. let cmdArr = data.command.split(",");
  419. console.log(cmdArr)
  420. bluetoothUtil.transCmd(cmdArr, "10", function(res) {
  421. console.log('圈层透传')
  422. console.log(res)
  423. var arraylenth = res.length
  424. var status = res[arraylenth - 1].substring(res[arraylenth - 1].length -
  425. 4,
  426. res[arraylenth - 1].length);
  427. console.log('打印圈层确认指令状态')
  428. console.log(status)
  429. if (status === '9000') {
  430. var form = {
  431. command: data.command,
  432. cosResponse: res.toString(),
  433. rechargeId: data.rechargeId,
  434. paidAmount: state.fee,
  435. giftAmount: 0,
  436. };
  437. const options = {
  438. type: 2,
  439. data: form,
  440. method: "POST",
  441. showLoading: true,
  442. };
  443. return new Promise(async (resolve, reject) => {
  444. const res = await request(quanConfirm, options);
  445. const data = stringToJson(res.bizContent);
  446. resolve(data);
  447. }).catch((error) => {
  448. reject(error);
  449. });
  450. }
  451. })
  452. }
  453. const uanConfirmSucessAction = (data) => {
  454. var form = {
  455. command: data.command,
  456. cosResponse: '9000',
  457. rechargeId: data.rechargeId,
  458. paidAmount: state.fee,
  459. giftAmount: 0,
  460. };
  461. const options = {
  462. type: 2,
  463. data: form,
  464. method: "POST",
  465. showLoading: true,
  466. };
  467. return new Promise(async (resolve, reject) => {
  468. const res = await request(quanConfirm, options);
  469. const data = stringToJson(res.bizContent);
  470. resolve(data);
  471. }).catch((error) => {
  472. reject(error);
  473. });
  474. }
  475. /*充值检测*/
  476. const czCheckAction = () => {
  477. var form = {
  478. cardId: state.cardId,
  479. openId: getItem(StorageKeys.OpenId),
  480. };
  481. const options = {
  482. type: 2,
  483. data: form,
  484. method: "POST",
  485. showLoading: true,
  486. };
  487. return new Promise(async (resolve, reject) => {
  488. const res = await request(cardCzPayResultCheck, options);
  489. const data = stringToJson(res.bizContent);
  490. resolve(data);
  491. }).catch((error) => {
  492. reject(error);
  493. });
  494. }
  495. /*充值申请*/
  496. const cardCzApplyAction = () => {
  497. var data = {
  498. cardId: state.cardId,
  499. openId: getItem(StorageKeys.OpenId),
  500. rechargeMoney: state.fee,
  501. };
  502. const options = {
  503. type: 2,
  504. data: data,
  505. method: "POST",
  506. showLoading: true,
  507. };
  508. return new Promise(async (resolve, reject) => {
  509. const res = await request(cardCzApply, options);
  510. const data = stringToJson(res.bizContent);
  511. resolve(data);
  512. }).catch((error) => {
  513. reject(error);
  514. });
  515. }
  516. //获取微信小程序openid
  517. const getOpenID = () => {
  518. uni.login({
  519. provider: "weixin",
  520. success: function(e) {
  521. wx.request({
  522. url: `https://api.weixin.qq.com/sns/jscode2session?appid=${wechatAppID}&secret=${wechatSecret}&js_code=${e.code}&grant_type=authorization_code`,
  523. success: (res: any) => {
  524. state.openid = res.data.openid;
  525. },
  526. });
  527. },
  528. fail: function() {
  529. msg('获取不到oppenId,请检查AppID和Secret是否争取')
  530. }
  531. });
  532. };
  533. //掉起微信支付
  534. const wxPayment = () => {
  535. // #ifdef MP-WEIXIN
  536. const options = {
  537. type: 2,
  538. data: {
  539. openId: getItem(StorageKeys.OpenId),
  540. wxOpenId: state.openid,
  541. cardId: state.cardId,
  542. orderNum: state.orderNum,
  543. payConfigId: wechatPayConfigId,
  544. body: '储值卡充值'
  545. },
  546. method: "POST",
  547. showLoading: true,
  548. };
  549. request(cardCzPay, options).then((res) => {
  550. const data = stringToJson(res.bizContent);
  551. uni.requestPayment({
  552. provider: "wxpay",
  553. orderInfo: "",
  554. timeStamp: data.timestamp,
  555. nonceStr: data.noncestr,
  556. package: data.wxPackage ?? "",
  557. signType: data.signType,
  558. paySign: data.sign,
  559. success: function() {
  560. //回调订单状态
  561. console.log('回调订单状态')
  562. checkOrder();
  563. },
  564. fail: function(err) {
  565. confirm(err, () => {}, "支付失败", false);
  566. },
  567. });
  568. });
  569. // #endif
  570. };
  571. //支付成功改变订单状态
  572. const checkOrder = () => {
  573. const options = {
  574. type: 2,
  575. data: {
  576. cardId: state.cardId,
  577. openId: getItem(StorageKeys.OpenId),
  578. orderNum: state.orderNum
  579. },
  580. method: "POST",
  581. showLoading: true,
  582. };
  583. request(cardCzPayResult, options).then((res) => {
  584. const data = stringToJson(res.bizContent);
  585. if (data.tradeState === 'SUCCESS')
  586. uni.navigateTo({
  587. url: `/pages/bluetooth/device-active-step3?routeType=2&&cardId=${state.cardId}`
  588. });
  589. console.log(data)
  590. });
  591. };
  592. </script>
  593. <style>
  594. .account {
  595. height: 224rpx;
  596. width: 690rpx;
  597. margin: 30rpx;
  598. position: relative;
  599. }
  600. .account .account-text {
  601. display: flex;
  602. justify-content: space-between;
  603. padding: 20rpx 50rpx;
  604. height: 100%;
  605. }
  606. .account .balance {
  607. display: flex;
  608. flex-direction: column;
  609. justify-content: center;
  610. align-items: flex-start;
  611. color: #ffffff;
  612. }
  613. .account .balance-tit {
  614. font-size: 26rpx;
  615. margin-bottom: 10rpx;
  616. }
  617. .account .balance-val {
  618. font-size: 56rpx;
  619. }
  620. .account .right-box {
  621. display: flex;
  622. flex-direction: column;
  623. justify-content: flex-start;
  624. align-items: flex-end;
  625. }
  626. .account .right-box .no {
  627. font-size: 26rpx;
  628. color: #ffffff;
  629. }
  630. .account .right-box .btn {
  631. font-size: 26rpx;
  632. padding: 0 20rpx;
  633. box-sizing: border-box;
  634. height: 42rpx;
  635. line-height: 42rpx;
  636. color: #28d20f;
  637. background: #ffffff;
  638. border-radius: 21rpx;
  639. display: inline-block;
  640. margin-top: 18rpx;
  641. }
  642. .account .head-bg {
  643. width: 690rpx;
  644. height: 224rpx;
  645. position: absolute;
  646. left: 0;
  647. top: 0;
  648. z-index: -99;
  649. }
  650. .input-box {
  651. display: flex;
  652. flex-direction: column;
  653. justify-content: center;
  654. align-items: center;
  655. padding: 20rpx 0;
  656. }
  657. .input-box .input {
  658. width: 430rpx;
  659. height: 98rpx;
  660. line-height: 98rpx;
  661. background: #f8f8f8;
  662. border: 1px solid #999999;
  663. border-radius: 6rpx;
  664. font-size: 30rpx;
  665. text-align: center;
  666. }
  667. .input-box .tips {
  668. color: #666666;
  669. font-size: 26rpx;
  670. margin-top: 18rpx;
  671. }
  672. .tabs {}
  673. .tabs .tab-tit {
  674. font-size: 30rpx;
  675. color: #333;
  676. padding: 45rpx 0 22rpx 30rpx;
  677. }
  678. .tabs .tab {
  679. display: flex;
  680. flex-wrap: wrap;
  681. justify-content: space-evenly;
  682. }
  683. .tabs .tab .item {
  684. width: 210rpx;
  685. height: 100rpx;
  686. background: #f6fff7;
  687. border: 1px solid #dcdde1;
  688. box-sizing: border-box;
  689. line-height: 100rpx;
  690. border-radius: 6rpx;
  691. text-align: center;
  692. margin-bottom: 24rpx;
  693. color: #333333;
  694. font-size: 32rpx;
  695. }
  696. .tabs .tab .item.active {
  697. border: 1px solid #24cc49;
  698. color: #24cc49;
  699. }
  700. .btn-primary {
  701. width: 670rpx;
  702. height: 80rpx;
  703. line-height: 80rpx;
  704. background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
  705. border-radius: 40rpx;
  706. font-size: 32rpx;
  707. color: #ffffff;
  708. margin: 200rpx 40rpx 0;
  709. text-align: center;
  710. }
  711. </style>