index.vue 6.7 KB

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