index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <template>
  2. <view v-if="personal.user_id" class="container" :style="appThemeStyle">
  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>¥{{ personal.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">
  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 class="input" type="digit" placeholder="可输入自定义充值金额" v-model="inputValue" @input="onChangeMoney" />
  30. </view>
  31. <!-- 支付方式 -->
  32. <view class="recharge-label m-top60">
  33. <text>支付方式</text>
  34. </view>
  35. <view class="payment-method">
  36. <view v-for="(item, index) in methods" :key="index" class="pay-item dis-flex flex-x-between" @click="handleSelectPayType(index)">
  37. <view class="item-left dis-flex flex-y-center">
  38. <view class="item-left_icon" :class="[item.method]">
  39. <text class="iconfont" :class="[PayMethodIconEnum[item.method]]"></text>
  40. </view>
  41. <view class="item-left_text">
  42. <text>{{ PayMethodEnum[item.method].name }}</text>
  43. </view>
  44. </view>
  45. <view class="item-right col-m" v-if="curPaymentItem && curPaymentItem.method == item.method">
  46. <text class="iconfont icon-check"></text>
  47. </view>
  48. </view>
  49. </view>
  50. <!-- 确认按钮 -->
  51. <view class="recharge-submit btn-submit">
  52. <form @submit="onSubmit">
  53. <button class="button" formType="submit" :disabled="disabled">立即充值</button>
  54. </form>
  55. </view>
  56. </view>
  57. <!-- 充值描述 -->
  58. <view class="describe-panel">
  59. <view class="recharge-label">
  60. <text>充值说明</text>
  61. </view>
  62. <view class="content">
  63. <text space="ensp">{{ setting.describe }}</text>
  64. </view>
  65. </view>
  66. <!-- 支付确认弹窗 -->
  67. <!-- #ifdef H5 -->
  68. <u-modal v-if="tempUnifyData" v-model="showConfirmModal" title="支付确认" show-cancel-button confirm-text="已完成支付"
  69. :confirm-color="appTheme.mainBg" negative-top="100" :asyncClose="true"
  70. @confirm="onTradeQuery(tempUnifyData.outTradeNo, tempUnifyData.method)">
  71. <view class="modal-content">
  72. <text>请在{{ PayMethodClientNameEnum[tempUnifyData.method] }}内完成支付,如果您已经支付成功,请点击“已完成支付”按钮</text>
  73. </view>
  74. </u-modal>
  75. <!-- #endif -->
  76. </view>
  77. </template>
  78. <script>
  79. import storage from '@/utils/storage'
  80. import * as RechargeApi from '@/api/recharge'
  81. import { PayMethodEnum } from '@/common/enum/payment'
  82. import { inArray, urlEncode } from '@/utils/util'
  83. import { Alipay, Wechat } from '@/core/payment'
  84. // 支付方式对应的图标
  85. const PayMethodIconEnum = {
  86. [PayMethodEnum.WECHAT.value]: 'icon-wechat-pay',
  87. [PayMethodEnum.ALIPAY.value]: 'icon-alipay',
  88. }
  89. // 支付方式的终端名称
  90. const PayMethodClientNameEnum = {
  91. [PayMethodEnum.WECHAT.value]: '微信',
  92. [PayMethodEnum.ALIPAY.value]: '支付宝'
  93. }
  94. // H5端支付下单时的数据
  95. // 用于从第三方支付页返回到收银台页面后拿到下单数据
  96. const getTempUnifyData = orderKey => {
  97. // if (window.performance && window.performance.navigation.type == 2) {
  98. const tempUnifyData = storage.get('tempUnifyData_' + orderKey)
  99. if (tempUnifyData) {
  100. storage.remove('tempUnifyData_' + orderKey)
  101. return tempUnifyData
  102. }
  103. // }
  104. return null
  105. }
  106. export default {
  107. data() {
  108. return {
  109. // 正在加载
  110. isLoading: true,
  111. // 按钮禁用
  112. disabled: false,
  113. // 枚举类
  114. PayMethodEnum,
  115. PayMethodIconEnum,
  116. PayMethodClientNameEnum,
  117. // 个人信息
  118. personal: { balance: '0.00' },
  119. // 充值设置
  120. setting: {},
  121. // 充值方案列表
  122. planList: [],
  123. // 当前客户端的支付方式列表(后端根据platform判断)
  124. methods: [],
  125. // 当前选中的套餐ID
  126. selectedPlanId: 0,
  127. // 自定义金额
  128. inputValue: '',
  129. // 当前选中的支付方式
  130. curPaymentItem: null,
  131. // 支付确认弹窗
  132. showConfirmModal: false,
  133. // #ifdef H5
  134. // 当前第三方支付信息 (临时数据, 仅用于H5端)
  135. tempUnifyData: { outTradeNo: '', method: '' },
  136. // #endif
  137. }
  138. },
  139. /**
  140. * 生命周期函数--监听页面显示
  141. */
  142. onShow() {
  143. // 获取页面数据
  144. this.getPageData()
  145. },
  146. methods: {
  147. // 选择充值套餐
  148. onSelectPlan(planId) {
  149. this.selectedPlanId = planId
  150. this.inputValue = ''
  151. },
  152. // 金额输入框
  153. onChangeMoney({ detail }) {
  154. this.inputValue = detail.value
  155. this.selectedPlanId = 0
  156. },
  157. // 选择支付方式
  158. handleSelectPayType(index) {
  159. this.curPaymentItem = this.methods[index]
  160. },
  161. // 获取页面数据
  162. getPageData() {
  163. const app = this
  164. app.isLoading = true
  165. return new Promise((resolve, reject) => {
  166. RechargeApi.center({ client: app.platform })
  167. .then(result => {
  168. app.setting = result.data.setting
  169. app.personal = result.data.personal
  170. app.planList = result.data.planList
  171. app.methods = result.data.paymentMethods
  172. app.isLoading = false
  173. // 默认选中的支付方式
  174. app.setDefaultPayType()
  175. // #ifdef H5
  176. // 判断当前页面来源于浏览器返回
  177. this.performance()
  178. // #endif
  179. })
  180. })
  181. },
  182. // 默认选中的支付方式
  183. setDefaultPayType() {
  184. if (!this.curPaymentItem) {
  185. this.handleSelectPayType(0)
  186. }
  187. },
  188. // 判断当前页面来源于浏览器返回并提示手动查单
  189. // #ifdef H5
  190. performance() {
  191. const app = this
  192. const performanceData = getTempUnifyData('recharge')
  193. if (performanceData) {
  194. app.tempUnifyData = performanceData
  195. app.showConfirmModal = true
  196. }
  197. },
  198. // #endif
  199. // 立即充值
  200. onSubmit(e) {
  201. const app = this
  202. // 判断是否选择了支付方式
  203. if (!app.curPaymentItem) {
  204. app.$toast('您还没有选择支付方式')
  205. return
  206. }
  207. // 按钮禁用
  208. if (app.disabled) return
  209. app.disabled = true
  210. // 提交到后端
  211. RechargeApi.submit({
  212. planId: app.selectedPlanId,
  213. customMoney: app.inputValue ? app.inputValue : '',
  214. method: app.curPaymentItem.method,
  215. client: app.platform,
  216. extra: app.getExtraAsUnify(app.curPaymentItem.method)
  217. })
  218. .then(result => app.onSubmitCallback(result))
  219. .finally(err => {
  220. setTimeout(() => app.disabled = false, 10)
  221. })
  222. },
  223. // 获取第三方支付的扩展参数
  224. getExtraAsUnify(method) {
  225. if (method === PayMethodEnum.ALIPAY.value) {
  226. return Alipay.extraAsUnify()
  227. }
  228. if (method === PayMethodEnum.WECHAT.value) {
  229. return Wechat.extraAsUnify()
  230. }
  231. return {}
  232. },
  233. // 订单提交成功后回调
  234. onSubmitCallback(result) {
  235. const app = this
  236. const method = app.curPaymentItem.method
  237. const paymentData = result.data.payment
  238. // 余额支付
  239. if (method === PayMethodEnum.BALANCE.value) {
  240. app.onShowSuccess(result)
  241. }
  242. // 发起支付宝支付
  243. if (method === PayMethodEnum.ALIPAY.value) {
  244. console.log('paymentData', paymentData)
  245. Alipay.payment({ orderKey: 'recharge', ...paymentData })
  246. .then(res => app.onPaySuccess(res))
  247. .catch(err => app.onPayFail(err))
  248. }
  249. // 发起微信支付
  250. if (method === PayMethodEnum.WECHAT.value) {
  251. console.log('paymentData', paymentData)
  252. Wechat.payment({ orderKey: 'recharge', ...paymentData })
  253. .then(res => app.onPaySuccess(res))
  254. .catch(err => app.onPayFail(err))
  255. }
  256. },
  257. // 订单支付成功的回调方法
  258. // 这里只是前端支付api返回结果success,实际订单是否支付成功 以后端的查单和异步通知为准
  259. onPaySuccess({ res, option: { isRequireQuery, outTradeNo, method } }) {
  260. const app = this
  261. // 判断是否需要主动查单
  262. // isRequireQuery为true代表需要主动查单
  263. if (isRequireQuery) {
  264. app.onTradeQuery(outTradeNo, method)
  265. return true
  266. }
  267. this.onShowSuccess(res)
  268. },
  269. // 显示支付成功信息并页面跳转
  270. onShowSuccess({ message }) {
  271. this.$toast(message || '订单支付成功')
  272. this.onSuccessNav()
  273. },
  274. // 订单支付失败
  275. onPayFail(err) {
  276. console.log('onPayFail', err)
  277. const errMsg = err.message || '订单未支付'
  278. this.$error(errMsg)
  279. },
  280. // 已完成支付按钮事件: 请求后端查单
  281. onTradeQuery(outTradeNo, method) {
  282. const app = this
  283. // 交易查询
  284. // 查询第三方支付订单是否付款成功
  285. RechargeApi.tradeQuery({ outTradeNo, method, client: app.platform })
  286. .then(result => result.data.isPay ? app.onShowSuccess(result) : app.onPayFail(result))
  287. .finally(() => app.showConfirmModal = false)
  288. },
  289. // 支付成功后的跳转
  290. onSuccessNav() {
  291. const pages = getCurrentPages()
  292. const lastPage = pages.length < 2 ? null : pages[pages.length - 2]
  293. const backRoutes = ['pages/wallet/index']
  294. if (lastPage && inArray(lastPage.route, backRoutes)) {
  295. setTimeout(() => uni.navigateBack(), 1000)
  296. } else {
  297. setTimeout(() => {
  298. this.$navTo('pages/wallet/index', {}, 'redirectTo')
  299. }, 1200)
  300. }
  301. },
  302. }
  303. }
  304. </script>
  305. <style lang="scss" scoped>
  306. page,
  307. .container {
  308. background: #fff;
  309. }
  310. .container {
  311. padding-bottom: 70rpx;
  312. }
  313. .m-top60 {
  314. margin-top: 60rpx;
  315. }
  316. // 账户面板
  317. .account-panel {
  318. width: 650rpx;
  319. height: 180rpx;
  320. margin: 50rpx auto;
  321. padding: 0 60rpx;
  322. box-sizing: border-box;
  323. border-radius: 12rpx;
  324. color: #fff;
  325. background: linear-gradient(-125deg, #a46bff, #786cff);
  326. box-shadow: 0 5px 22px 0 rgba(0, 0, 0, 0.26);
  327. }
  328. .panel-lable {
  329. font-size: 32rpx;
  330. }
  331. .recharge-label {
  332. color: rgb(51, 51, 51);
  333. font-size: 30rpx;
  334. margin-bottom: 25rpx;
  335. }
  336. .panel-balance {
  337. text-align: right;
  338. font-size: 46rpx;
  339. }
  340. .recharge-panel {
  341. margin-top: 60rpx;
  342. padding: 0 60rpx;
  343. }
  344. // 充值套餐
  345. .recharge-plan {
  346. margin-bottom: -20rpx;
  347. .recharge-plan_item {
  348. width: 192rpx;
  349. padding: 15rpx 0;
  350. float: left;
  351. text-align: center;
  352. color: #888;
  353. border: 1rpx solid rgb(228, 228, 228);
  354. border-radius: 10rpx;
  355. margin: 0 20rpx 20rpx 0;
  356. &:nth-child(3n + 0) {
  357. margin-right: 0;
  358. }
  359. &.active {
  360. color: #786cff;
  361. border: 1rpx solid #786cff;
  362. .plan_money {
  363. color: #786cff;
  364. }
  365. }
  366. }
  367. }
  368. .plan_money {
  369. font-size: 32rpx;
  370. color: rgb(82, 82, 82);
  371. }
  372. .plan_gift {
  373. font-size: 25rpx;
  374. }
  375. .recharge-input {
  376. margin-top: 40rpx;
  377. .input {
  378. display: block;
  379. border: 1rpx solid rgb(228, 228, 228);
  380. border-radius: 10rpx;
  381. padding: 20rpx 26rpx;
  382. font-size: 28rpx;
  383. }
  384. }
  385. // 立即充值
  386. .recharge-submit {
  387. margin-top: 70rpx;
  388. }
  389. .btn-submit {
  390. .button {
  391. font-size: 30rpx;
  392. background: #786cff;
  393. border: none;
  394. color: white;
  395. border-radius: 50rpx;
  396. padding: 0 120rpx;
  397. line-height: 3;
  398. }
  399. .button[disabled] {
  400. background: #a098ff;
  401. border-color: #a098ff;
  402. color: white;
  403. }
  404. }
  405. // 充值说明
  406. .describe-panel {
  407. margin-top: 50rpx;
  408. padding: 0 60rpx;
  409. .content {
  410. font-size: 26rpx;
  411. line-height: 1.6;
  412. color: #888;
  413. }
  414. }
  415. // 支付方式
  416. .payment-method {
  417. .pay-item {
  418. padding: 14rpx 0;
  419. font-size: 26rpx;
  420. .item-left_icon {
  421. margin-right: 20rpx;
  422. font-size: 44rpx;
  423. &.wechat {
  424. color: #00c800;
  425. }
  426. &.alipay {
  427. color: #009fe8;
  428. }
  429. }
  430. .item-left_text {
  431. font-size: 30rpx;
  432. }
  433. .item-right {
  434. font-size: 30rpx;
  435. }
  436. .user-balance {
  437. margin-left: 20rpx;
  438. font-size: 26rpx;
  439. }
  440. }
  441. }
  442. // 支付确认弹窗
  443. .modal-content {
  444. padding: 40rpx 48rpx;
  445. font-size: 30rpx;
  446. line-height: 50rpx;
  447. text-align: left;
  448. color: #606266;
  449. // height: 620rpx;
  450. box-sizing: border-box;
  451. }
  452. </style>