accountBalance.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. <view class="line"></view>
  36. <view class="btn-pay">
  37. <button class="btn" url="/pagesAir/rechargeRecord/rechargeRecord" @tap="btn_click">
  38. <text class="">立即付款</text>
  39. </button>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. export default {
  45. data() {
  46. return {
  47. balance: 18.88, // 余额
  48. amount: '', // 充值金额
  49. IP: '',
  50. ceshi: 'code',
  51. test: this.$store.state.test
  52. }
  53. },
  54. onLoad() {
  55. if (this.test) {
  56. this.amount = 0.01
  57. }
  58. },
  59. methods: {
  60. /**
  61. * 选择了充值金额
  62. * @param {Object} param
  63. */
  64. Recharge(param) {
  65. this.amount = param
  66. },
  67. /**
  68. * 获取IP
  69. */
  70. async getIP() {
  71. const res = await this.$myRequest({
  72. host: 'ip',
  73. url: '?ie=utf-8',
  74. method: 'POST'
  75. })
  76. if (res) {
  77. // console.log(res);
  78. const reg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
  79. let ip = reg.exec(res.data);
  80. // 获取code
  81. this.getCode(ip[0])
  82. }
  83. },
  84. /**
  85. * 获得code
  86. */
  87. getCode(param_ip) {
  88. uni.login({
  89. success: (res) => {
  90. // console.log('recharge', res);
  91. if (res.code) {
  92. // 组合地址,发起支付
  93. this.jsapi(param_ip, res.code)
  94. } else {
  95. uni.showToast({
  96. title: res.errMsg,
  97. icon: 'none'
  98. });
  99. }
  100. }
  101. })
  102. },
  103. /**
  104. * 请求服务器,获取支付参数,并支付
  105. */
  106. // async jsapi(param_ip, param_code) {
  107. // if (param_code == '' || this.amount == '' || param_ip == '') {
  108. // uni.showToast({
  109. // icon: 'none',
  110. // title: 'code、充值金额或IP为空'
  111. // });
  112. // return
  113. // }
  114. // const res = await this.$myRequest({
  115. // host: this.ceshi,
  116. // url: '/HotWaters/wpPay.action',
  117. // method: 'POST',
  118. // header: {
  119. // 'content-type': 'application/x-www-form-urlencoded'
  120. // },
  121. // data: {
  122. // code: param_code,
  123. // num: this.amount,
  124. // ip: param_ip
  125. // }
  126. // })
  127. // // console.log(res);
  128. // if (res.data.pay == 'error') {
  129. // uni.showToast({
  130. // title: '未获得支付参数',
  131. // icon: 'success',
  132. // duration: 3000
  133. // });
  134. // } else {
  135. // wx.requestPayment({
  136. // timeStamp: res.data.pay.timeStamp,
  137. // nonceStr: res.data.pay.nonceStr,
  138. // package: 'prepay_id=' + res.data.pay.prepay_id,
  139. // signType: res.data.pay.signType,
  140. // paySign: res.data.pay.paySign,
  141. // success: (res) => {
  142. // if (res.errMsg == 'requestPayment:ok') {
  143. // this.$store.state.payInfo.from = 'reshui_pay'
  144. // this.$store.state.payInfo.resultMsg = '支付成功'
  145. // this.$store.state.reshui_amount = this.amount
  146. // uni.navigateBack({
  147. // delta: 1
  148. // })
  149. // }
  150. // },
  151. // fail: (res) => {
  152. // if (res.errMsg == 'requestPayment:fail cancel') {
  153. // uni.showToast({
  154. // title: '支付已取消',
  155. // icon: 'success',
  156. // duration: 2000
  157. // });
  158. // }
  159. // },
  160. // complete: (res) => {
  161. // // console.log(res);
  162. // }
  163. // });
  164. // }
  165. // },
  166. /**
  167. * 输入充值金额
  168. */
  169. onInput(e) {
  170. if (this.test) { // 测试环境
  171. return
  172. }
  173. const v = e.detail.value
  174. const zero = /^(0{1,})|[^0-9]/g
  175. let final = 0
  176. if (!v) {
  177. final = 0
  178. } else {
  179. final = v.toString().replace(zero, (v) => {
  180. return 0
  181. })
  182. if (final.split('')[0] * 1 === 0) {
  183. final = final.slice(1) - 0 || 0
  184. }
  185. if (final > 100) {
  186. final = 100
  187. }
  188. }
  189. this.$nextTick(() => {
  190. if (final.toString() == '0') {
  191. this.amount = ''
  192. } else {
  193. this.amount = final.toString() || '0'
  194. }
  195. })
  196. },
  197. /**
  198. * 输入框,失去焦点时
  199. */
  200. onBlur() {
  201. if (this.test) { // 测试环境
  202. return
  203. }
  204. if (this.amount < 10) {
  205. this.amount = 10
  206. }
  207. },
  208. /**
  209. * 立即付款
  210. */
  211. btn_click() {
  212. // console.log(this.amount);
  213. if (isNaN(this.amount)) {
  214. uni.showToast({
  215. title: '请输入正确金额',
  216. duration: 2000
  217. })
  218. return
  219. }
  220. // 测试环境下执行
  221. if (this.test) { // 测试环境
  222. uni.showModal({
  223. title: '提示',
  224. content: '您选择了充值:¥' + this.amount + ' 元',
  225. cancelText: '算了',
  226. confirmText: '充值',
  227. success: (res) => {
  228. if (res.confirm) {
  229. // 获取IP
  230. this.getIP()
  231. } else if (res.cancel) {
  232. return
  233. }
  234. }
  235. })
  236. return
  237. }
  238. // 生产环境
  239. if (this.amount < 10 || this.amount > 100) {
  240. uni.showToast({
  241. icon: 'none',
  242. title: '最少充值10元,最多充值100元!',
  243. mask: true,
  244. duration: 3000
  245. })
  246. return
  247. }
  248. // 确认
  249. uni.showModal({
  250. title: '提示',
  251. content: '您选择了充值:¥' + this.amount + ' 元',
  252. cancelText: '算了',
  253. confirmText: '充值',
  254. success: (res) => {
  255. if (res.confirm) {
  256. // 获取IP
  257. this.getIP()
  258. } else if (res.cancel) {
  259. return
  260. }
  261. }
  262. })
  263. }
  264. }
  265. }
  266. </script>
  267. <style lang="scss" scoped>
  268. .container {
  269. display: flex;
  270. flex-direction: column;
  271. font-size: 28rpx;
  272. font-family: "Microsoft YaHei-3970(82674968)";
  273. width: 730rpx;
  274. padding: 10rpx;
  275. .line {
  276. height: 20rpx;
  277. }
  278. .top {
  279. display: flex;
  280. justify-content: space-around;
  281. height: 220rpx;
  282. border-radius: 10rpx;
  283. padding-top: 30rpx;
  284. background-color: #EFF2F9;
  285. .it {
  286. display: flex;
  287. flex-direction: column;
  288. align-items: center;
  289. .nv {
  290. display: flex;
  291. flex-direction: column;
  292. align-items: center;
  293. padding: 18rpx;
  294. border-radius: 10rpx;
  295. }
  296. image {
  297. width: 60rpx;
  298. height: 60rpx;
  299. margin-bottom: 14rpx;
  300. }
  301. .title {
  302. font-size: 36upx;
  303. }
  304. .bal {
  305. font-size: 26upx;
  306. color: #666666;
  307. }
  308. }
  309. }
  310. .middle-title {
  311. display: flex;
  312. align-items: center;
  313. image {
  314. width: 36rpx;
  315. height: 36rpx;
  316. margin-right: 10rpx;
  317. }
  318. .txt {
  319. font-size: 32upx;
  320. }
  321. }
  322. .middle-balance {
  323. display: flex;
  324. justify-content: space-around;
  325. align-items: center;
  326. flex-wrap: wrap;
  327. height: 280rpx;
  328. .balance {
  329. display: flex;
  330. justify-content: center;
  331. align-items: center;
  332. font-size: 36upx;
  333. font-family: Microsoft YaHei-3970(82674968);
  334. font-weight: bold;
  335. color: #333333;
  336. padding: 36rpx 0;
  337. width: 220rpx;
  338. background-color: #EFF2F9;
  339. border-radius: 6rpx;
  340. }
  341. .balanceborder {
  342. border: 1px solid #3869FC;
  343. width: 218rpx;
  344. padding: 34rpx 0;
  345. }
  346. }
  347. .input-balance {
  348. font-size: 40upx;
  349. height: 80rpx;
  350. background-color: #F2F2F2;
  351. border-radius: 6rpx;
  352. margin: 0 10rpx;
  353. padding: 10rpx 20rpx;
  354. }
  355. .pc {
  356. color: #B3B3B3;
  357. }
  358. .btn-pay {
  359. display: flex;
  360. justify-content: center;
  361. align-items: center;
  362. .btn {
  363. display: flex;
  364. justify-content: center;
  365. align-items: center;
  366. margin: 30rpx;
  367. width: 280rpx;
  368. height: 100rpx;
  369. background: #3869FC;
  370. border-radius: 6px;
  371. font-size: 36upx;
  372. font-family: Microsoft YaHei-3970(82674968);
  373. font-weight: 400;
  374. color: #FFFFFF;
  375. }
  376. }
  377. }
  378. </style>