accountBalance.vue 11 KB

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