accountBalance.vue 9.9 KB

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