accountBalance.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <template>
  2. <view class="container">
  3. <view class="line"></view>
  4. <view class="top">
  5. <view class="it">
  6. <view class="nv">
  7. <image src="../static/images/ye.png" mode=""></image>
  8. <view class="title">账户余额</view>
  9. <view class="bal">¥{{balance.toFixed(2)}}</view>
  10. </view>
  11. </view>
  12. <view class="it">
  13. <navigator class="nv" url="/pagesAir/rechargeRecord/rechargeRecord">
  14. <image src="../static/images/jl.png" mode=""></image>
  15. <view class="title">充值记录</view>
  16. </navigator>
  17. </view>
  18. </view>
  19. <view class="line"></view>
  20. <view class="middle-title">
  21. <image src="../static/images/cz.png" mode=""></image>
  22. <view class="txt">充值</view>
  23. </view>
  24. <view class="line"></view>
  25. <view class="middle-balance">
  26. <view class="balance" @tap="Recharge(10)" :class="{balanceborder: amount == 10}">10元</view>
  27. <view class="balance" @tap="Recharge(20)" :class="{balanceborder: amount == 20}">20元</view>
  28. <view class=" balance" @tap="Recharge(30)" :class="{balanceborder: amount == 30}">30元</view>
  29. <view class=" balance" @tap="Recharge(40)" :class="{balanceborder: amount == 40}">40元</view>
  30. <view class=" balance" @tap="Recharge(50)" :class="{balanceborder: amount == 50}">50元</view>
  31. <view class=" balance" @tap="Recharge(60)" :class="{balanceborder: amount == 60}">60元</view>
  32. </view>
  33. <view class=" line"></view>
  34. <input class="input-balance" type="text" v-model="amount" @input="onInput" @blur="onBlur"
  35. placeholder="请输入充值金额,金额大于10" placeholder-class="pc" maxlength="4" />
  36. <view class="line"></view>
  37. <view class="btn-pay">
  38. <button class="btn" url="/pagesAir/rechargeRecord/rechargeRecord" @tap="btn_click">
  39. <text class="">立即付款</text>
  40. </button>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. export default {
  46. data() {
  47. return {
  48. ceshi: 'air',
  49. id_card: '',
  50. appkey: '3183DC96A6DABA8D', // 商户appkey
  51. ocode: '1015730314', // 获取用户信息
  52. app_secret: '05742955578EC5BD29B7BC4CAC5AFACA', // 获取用户信息
  53. balance: 0.00, // 余额
  54. amount: '', // 充值金额
  55. IP: '',
  56. test: this.$store.state.test
  57. }
  58. },
  59. onLoad(options) {
  60. // 系统正在维护...
  61. if (this.test == 'weihuzhong') {
  62. uni.redirectTo({
  63. url: '../../pages/index/index'
  64. })
  65. return;
  66. }
  67. // 测试环境
  68. if (this.test) {
  69. this.amount = 0.01
  70. }
  71. // 获取身份证号
  72. this.get_base_info(options)
  73. },
  74. methods: {
  75. /**
  76. * 获取身份证号
  77. */
  78. get_base_info(options) {
  79. try {
  80. if (this.id_card == '' || typeof(this.id_card) == 'undefined') {
  81. const userinfo = uni.getStorageSync('userinfo_storage_key')
  82. if (userinfo) {
  83. this.id_card = userinfo.id_card
  84. } else {
  85. uni.navigateTo({
  86. url: '../index/index?from=' + options.from
  87. })
  88. uni.showToast({
  89. icon: 'none',
  90. title: '身份证号为空,请进行授权',
  91. duration: 3000
  92. });
  93. return
  94. }
  95. }
  96. } catch (e) {
  97. console.log('获取基本信息:' + e.message);
  98. }
  99. // 请求服务器,获得余额
  100. this.getBalance()
  101. },
  102. /**
  103. * 请求服务器,获得余额
  104. */
  105. async getBalance() {
  106. const res = await this.$myRequest({
  107. host: this.ceshi,
  108. url: '/airManage/usersqueryYu.action',
  109. method: 'POST',
  110. header: {
  111. 'content-type': 'application/x-www-form-urlencoded'
  112. },
  113. data: {
  114. // sfzh: '3602123654'
  115. sfzh: this.id_card
  116. }
  117. })
  118. // console.log(res.data);
  119. let data = res.data
  120. if (data.code === 200) {
  121. if (typeof data.data != 'undefined' && data.data != '' && JSON.stringify(data.data) != '{}') {
  122. this.balance = data.data.balance
  123. } else {
  124. uni.showToast({
  125. title: '未查询到余额!',
  126. icon: 'success'
  127. });
  128. }
  129. } else {
  130. uni.showToast({
  131. title: data.data.message
  132. })
  133. }
  134. },
  135. /**
  136. * 选择了充值金额
  137. * @param {Object} param
  138. */
  139. Recharge(param) {
  140. this.amount = param
  141. },
  142. /**
  143. * 获得code
  144. */
  145. getCode() {
  146. uni.login({
  147. success: (res) => {
  148. // console.log('recharge', res);
  149. if (res.code) {
  150. // 发起支付
  151. this.jsapi(res.code)
  152. } else {
  153. uni.showToast({
  154. title: res.errMsg,
  155. icon: 'none'
  156. });
  157. }
  158. }
  159. })
  160. },
  161. /**
  162. * 请求服务器,获取支付参数,并支付
  163. */
  164. async jsapi(param_code) {
  165. if (param_code == '' || this.amount == '' || this.id_card == '') {
  166. uni.showToast({
  167. icon: 'none',
  168. title: 'code、充值金额 或 身份证号为空',
  169. duration: 3000
  170. });
  171. return
  172. }
  173. const res = await this.$myRequest({
  174. host: this.ceshi,
  175. url: '/airManage/rechargepay_ns.action',
  176. method: 'POST',
  177. header: {
  178. 'content-type': 'application/x-www-form-urlencoded'
  179. },
  180. data: {
  181. sfzh: this.id_card,
  182. code: param_code,
  183. num: this.amount
  184. }
  185. })
  186. // console.log(res);
  187. if (res.statusCode !== 200) {
  188. uni.showToast({
  189. title: '未获得支付参数',
  190. icon: 'success',
  191. duration: 3000
  192. });
  193. } else {
  194. if (res.data.code === 205) {
  195. uni.showToast({
  196. title: '服务器返回:' + res.data.message,
  197. icon: 'none',
  198. duration: 3000
  199. });
  200. return
  201. }
  202. // 调起微信支付
  203. wx.requestPayment({
  204. timeStamp: res.data.timeStamp,
  205. nonceStr: res.data.nonceStr,
  206. package: res.data.package,
  207. signType: res.data.signType,
  208. paySign: res.data.paySign,
  209. success: (res) => {
  210. if (res.errMsg == 'requestPayment:ok') {
  211. let _this = this;
  212. // 提示用户支付成功
  213. uni.showToast({
  214. title: '支付成功',
  215. icon: 'success',
  216. duration: 2000,
  217. success() {
  218. // 请求服务器,获得余额
  219. _this.getBalance()
  220. }
  221. });
  222. }
  223. },
  224. fail: (res) => {
  225. if (res.errMsg == 'requestPayment:fail cancel') {
  226. // 提示用户已经取消支付了
  227. uni.showToast({
  228. title: '支付已取消',
  229. icon: 'success',
  230. duration: 2000
  231. });
  232. }
  233. },
  234. complete: (res) => {
  235. // console.log(res);
  236. }
  237. });
  238. }
  239. },
  240. /**
  241. * 输入充值金额
  242. */
  243. onInput(e) {
  244. if (this.test) { // 测试环境
  245. return
  246. }
  247. const v = e.detail.value
  248. const zero = /^(0{1,})|[^0-9]/g
  249. let final = 0
  250. if (!v) {
  251. final = 0
  252. } else {
  253. final = v.toString().replace(zero, (v) => {
  254. return 0
  255. })
  256. if (final.split('')[0] * 1 === 0) {
  257. final = final.slice(1) - 0 || 0
  258. }
  259. if (final > 100) {
  260. final = 100
  261. }
  262. }
  263. this.$nextTick(() => {
  264. if (final.toString() == '0') {
  265. this.amount = ''
  266. } else {
  267. this.amount = final.toString() || '0'
  268. }
  269. })
  270. },
  271. /**
  272. * 输入框,失去焦点时
  273. */
  274. onBlur() {
  275. if (this.test) { // 测试环境
  276. return
  277. }
  278. if (this.amount < 10) {
  279. this.amount = 10
  280. }
  281. },
  282. /**
  283. * 立即付款
  284. */
  285. btn_click() {
  286. // console.log(this.amount);
  287. if (isNaN(this.amount)) {
  288. uni.showToast({
  289. title: '请输入正确金额',
  290. duration: 2000
  291. })
  292. return
  293. }
  294. // 测试环境下执行
  295. if (this.test) { // 测试环境
  296. uni.showModal({
  297. title: '提示',
  298. content: '您选择了充值:¥' + this.amount + ' 元',
  299. cancelText: '算了',
  300. confirmText: '充值',
  301. success: (res) => {
  302. if (res.confirm) {
  303. // 获取code
  304. this.getCode()
  305. } else if (res.cancel) {
  306. return
  307. }
  308. }
  309. })
  310. return
  311. }
  312. // 生产环境
  313. if (this.amount < 10 || this.amount > 100) {
  314. uni.showToast({
  315. icon: 'none',
  316. title: '最少充值10元,最多充值100元!',
  317. mask: true,
  318. duration: 3000
  319. })
  320. return
  321. }
  322. // 确认
  323. uni.showModal({
  324. title: '提示',
  325. content: '您选择了充值:¥' + this.amount + ' 元',
  326. cancelText: '算了',
  327. confirmText: '充值',
  328. success: (res) => {
  329. if (res.confirm) {
  330. // 获取code
  331. this.getCode()
  332. } else if (res.cancel) {
  333. return
  334. }
  335. }
  336. })
  337. }
  338. }
  339. }
  340. </script>
  341. <style lang="scss" scoped>
  342. .container {
  343. display: flex;
  344. flex-direction: column;
  345. font-size: 28rpx;
  346. font-family: "Microsoft YaHei-3970(82674968)";
  347. width: 730rpx;
  348. padding: 10rpx;
  349. .line {
  350. height: 20rpx;
  351. }
  352. .top {
  353. display: flex;
  354. justify-content: space-around;
  355. height: 220rpx;
  356. border-radius: 10rpx;
  357. padding-top: 30rpx;
  358. background-color: #EFF2F9;
  359. .it {
  360. display: flex;
  361. flex-direction: column;
  362. align-items: center;
  363. .nv {
  364. display: flex;
  365. flex-direction: column;
  366. align-items: center;
  367. padding: 18rpx;
  368. border-radius: 10rpx;
  369. }
  370. image {
  371. width: 60rpx;
  372. height: 60rpx;
  373. margin-bottom: 14rpx;
  374. }
  375. .title {
  376. font-size: 36upx;
  377. }
  378. .bal {
  379. font-size: 26upx;
  380. color: #666666;
  381. }
  382. }
  383. }
  384. .middle-title {
  385. display: flex;
  386. align-items: center;
  387. image {
  388. width: 36rpx;
  389. height: 36rpx;
  390. margin-right: 10rpx;
  391. }
  392. .txt {
  393. font-size: 32upx;
  394. }
  395. }
  396. .middle-balance {
  397. display: flex;
  398. justify-content: space-around;
  399. align-items: center;
  400. flex-wrap: wrap;
  401. height: 280rpx;
  402. .balance {
  403. display: flex;
  404. justify-content: center;
  405. align-items: center;
  406. font-size: 36upx;
  407. font-family: Microsoft YaHei-3970(82674968);
  408. font-weight: bold;
  409. color: #333333;
  410. padding: 36rpx 0;
  411. width: 220rpx;
  412. background-color: #EFF2F9;
  413. border-radius: 6rpx;
  414. }
  415. .balanceborder {
  416. border: 1px solid #3869FC;
  417. width: 218rpx;
  418. padding: 34rpx 0;
  419. }
  420. }
  421. .input-balance {
  422. font-size: 40upx;
  423. height: 80rpx;
  424. background-color: #F2F2F2;
  425. border-radius: 6rpx;
  426. margin: 0 10rpx;
  427. padding: 10rpx 20rpx;
  428. }
  429. .pc {
  430. color: #B3B3B3;
  431. }
  432. .btn-pay {
  433. display: flex;
  434. justify-content: center;
  435. align-items: center;
  436. .btn {
  437. display: flex;
  438. justify-content: center;
  439. align-items: center;
  440. margin: 30rpx;
  441. width: 280rpx;
  442. height: 100rpx;
  443. background: #3869FC;
  444. border-radius: 6px;
  445. font-size: 36upx;
  446. font-family: Microsoft YaHei-3970(82674968);
  447. font-weight: 400;
  448. color: #FFFFFF;
  449. }
  450. }
  451. }
  452. </style>