index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view class="container" v-if="userInfo.id">
  3. <view class="account-panel dis-flex flex-y-center">
  4. <view class="panel-lable">
  5. <text>账户余额</text>
  6. </view>
  7. <view class="panel-balance flex-box">
  8. <text>¥{{ userInfo.balance }}</text>
  9. </view>
  10. </view>
  11. <view class="recharge-panel">
  12. <view class="recharge-label">
  13. <text>充值金额</text>
  14. </view>
  15. <view class="recharge-plan clearfix" v-if="setting.planList.length > 0">
  16. <block v-for="(item, index) in setting.planList" :key="index">
  17. <view class="recharge-plan_item" :class="{ active: rechargeAmount == item.rechargeAmount }" @click="onSelectPlan(item.rechargeAmount)">
  18. <view class="plan_money">
  19. <text>{{ item.rechargeAmount }}</text>
  20. </view>
  21. <view class="plan_gift" v-if="item.giveAmount > 0">
  22. <text>送{{ item.giveAmount }}</text>
  23. </view>
  24. </view>
  25. </block>
  26. </view>
  27. <!-- 手动充值输入框 -->
  28. <view class="recharge-input" v-if="!setting.planList || setting.planList.length < 1">
  29. <input type="digit" placeholder="请输入充值金额" v-model="inputValue" @input="onChangeMoney" />
  30. </view>
  31. <!-- 确认按钮 -->
  32. <view class="recharge-submit btn-submit">
  33. <form @submit="onSubmit">
  34. <button class="button" formType="submit" :disabled="disabled">立即充值</button>
  35. </form>
  36. </view>
  37. </view>
  38. <!-- 充值描述 -->
  39. <view class="recharge-describe" v-if="setting.remark.length > 0">
  40. <view class="recharge-label">
  41. <text>充值说明</text>
  42. </view>
  43. <view class="content">
  44. <text space="ensp">{{setting.remark}}</text>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import * as UserApi from '@/api/user'
  51. import * as BalanceApi from '@/api/balance'
  52. import { wxPayment } from '@/utils/app'
  53. export default {
  54. data() {
  55. return {
  56. // 正在加载
  57. isLoading: true,
  58. // 会员信息
  59. userInfo: {},
  60. // 充值设置
  61. setting: { isOpen: false, planList: [], remark: '' },
  62. // 按钮禁用
  63. disabled: false,
  64. // 当前选中的套餐id
  65. rechargeAmount: 0,
  66. // 自定义金额
  67. inputValue: '',
  68. }
  69. },
  70. /**
  71. * 生命周期函数--监听页面加载
  72. */
  73. onLoad(options) {
  74. // 获取页面数据
  75. this.getPageData()
  76. },
  77. methods: {
  78. /**
  79. * 选择充值套餐
  80. */
  81. onSelectPlan(rechargeAmount) {
  82. this.rechargeAmount = rechargeAmount
  83. this.inputValue = ''
  84. },
  85. // 金额输入框
  86. onChangeMoney(e) {
  87. this.inputValue = e.target.value
  88. this.rechargeAmount = 0
  89. },
  90. // 获取页面数据
  91. getPageData() {
  92. const app = this
  93. app.isLoading = true
  94. Promise.all([app.getUserInfo(), app.getSetting()])
  95. .then(() => app.isLoading = false)
  96. },
  97. // 获取会员信息
  98. getUserInfo() {
  99. const app = this
  100. return new Promise((resolve, reject) => {
  101. UserApi.info()
  102. .then(result => {
  103. app.userInfo = result.data.userInfo
  104. resolve(app.userInfo)
  105. })
  106. })
  107. },
  108. // 获取充值设置
  109. getSetting() {
  110. const app = this
  111. return new Promise((resolve, reject) => {
  112. BalanceApi.setting()
  113. .then(result => {
  114. app.setting = result.data
  115. console.log("app.setting = ", app.setting)
  116. resolve(app.setting)
  117. })
  118. })
  119. },
  120. // 立即充值
  121. onSubmit(e) {
  122. const app = this
  123. if (!app.setting.isOpen) {
  124. app.$error('当前未开启充值!')
  125. return false
  126. }
  127. if (parseFloat(app.rechargeAmount) <= 0 && app.inputValue.length < 1) {
  128. app.$error('请确认充值金额!')
  129. return false
  130. }
  131. // 按钮禁用
  132. app.disabled = true
  133. // 提交到后端
  134. BalanceApi.doRecharge(app.rechargeAmount, app.inputValue)
  135. .then(result => app.wxPayment(result.data.payment))
  136. .catch(err => app.$error('提交支付失败'))
  137. .finally(() => app.disabled = false)
  138. },
  139. // 发起微信支付
  140. wxPayment(option) {
  141. const app = this
  142. wxPayment(option)
  143. .then(() => {
  144. app.$success('支付成功')
  145. setTimeout(() => {
  146. // 获取页面数据
  147. app.getPageData()
  148. }, 1500)
  149. })
  150. .catch(err => app.$error('订单未支付'))
  151. }
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. page,
  157. .container {
  158. background: #fff;
  159. }
  160. .container {
  161. padding-bottom: 70rpx;
  162. }
  163. /* 账户面板 */
  164. .account-panel {
  165. width: 650rpx;
  166. height: 180rpx;
  167. margin: 50rpx auto;
  168. padding: 0 60rpx;
  169. box-sizing: border-box;
  170. border-radius: 8rpx;
  171. color: #fff;
  172. background: $fuint-theme;
  173. box-shadow: 0 5px 22px 0 rgba(0, 0, 0, 0.26);
  174. }
  175. .panel-lable {
  176. font-size: 32rpx;
  177. }
  178. .recharge-label {
  179. color: rgb(51, 51, 51);
  180. font-size: 32rpx;
  181. margin-bottom: 25rpx;
  182. }
  183. .panel-balance {
  184. text-align: right;
  185. font-size: 46rpx;
  186. }
  187. .recharge-panel {
  188. margin-top: 60rpx;
  189. padding: 0 60rpx;
  190. }
  191. /* 充值套餐 */
  192. .recharge-plan {
  193. .recharge-plan_item {
  194. width: 192rpx;
  195. padding: 15rpx 0;
  196. float: left;
  197. text-align: center;
  198. color: #888;
  199. border: 1rpx solid rgb(228, 228, 228);
  200. border-radius: 5rpx;
  201. margin: 0 20rpx 20rpx 0;
  202. &:nth-child(3n + 0) {
  203. margin-right: 0;
  204. }
  205. &.active {
  206. background: #E3BE83;
  207. color: #FFFFFF;
  208. border: 1rpx solid #EDD2A9;
  209. .plan_money {
  210. color: #FFFFFF;
  211. font-weight: bold;
  212. }
  213. }
  214. }
  215. }
  216. .plan_money {
  217. font-size: 32rpx;
  218. color: rgb(82, 82, 82);
  219. }
  220. .plan_gift {
  221. font-size: 25rpx;
  222. }
  223. .recharge-input {
  224. margin-top: 25rpx;
  225. input {
  226. border: 1rpx solid rgb(228, 228, 228);
  227. border-radius: 10rpx;
  228. padding: 15rpx 16rpx;
  229. font-size: 26rpx;
  230. }
  231. }
  232. /* 立即充值 */
  233. .recharge-submit {
  234. margin-top: 70rpx;
  235. }
  236. .btn-submit {
  237. .button {
  238. font-size: 30rpx;
  239. background: linear-gradient(to right, #f9211c, #ff6335);
  240. border: none;
  241. color: white;
  242. border-radius: 40rpx;
  243. padding: 0 120rpx;
  244. line-height: 3;
  245. }
  246. .button[disabled] {
  247. background: #ff6335;
  248. border-color: #ff6335;
  249. color: white;
  250. }
  251. }
  252. /* 充值说明 */
  253. .recharge-describe {
  254. margin-top: 50rpx;
  255. padding: 0 60rpx;
  256. .content {
  257. font-size: 26rpx;
  258. line-height: 1.6;
  259. color: #888;
  260. }
  261. }
  262. </style>