You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
6.0 KiB

4 years ago
  1. <template>
  2. <view class="app">
  3. <view v-if="!openExamine">
  4. <view class="page-tit">
  5. <text>充值金额</text>
  6. </view>
  7. <view class="price-list">
  8. <view
  9. class="p-item center"
  10. :class="{active: item.checked}"
  11. v-for="(item, index) in moneyList"
  12. :key="index"
  13. @click="checkePrice(item)"
  14. >
  15. <text>{{ item.money }}</text>
  16. </view>
  17. </view>
  18. <view class="input-wrap row">
  19. <text>其他金额</text>
  20. <input v-model="money" type="number" maxlength="10" class="input" placeholder="请输入其他金额" placeholder-style="color: #999" />
  21. </view>
  22. <view class="page-tit">
  23. <text class="f-b">选择支付方式</text>
  24. </view>
  25. <view class="cell row b-b" @click="checkType('wxpay')">
  26. <image class="icon" :src="'/static/icon/wxpay.png'"></image>
  27. <view class="column fill">
  28. <text class="tit">微信支付</text>
  29. </view>
  30. <text v-if="curType === 'wxpay'" class="mix-icon icon-xuanzhong"></text>
  31. </view>
  32. <!-- #ifndef MP-WEIXIN -->
  33. <view class="cell row b-b" @click="checkType('alipay')">
  34. <image class="icon" :src="'/static/icon/alipay.png'"></image>
  35. <view class="column fill">
  36. <text class="tit">支付宝</text>
  37. </view>
  38. <text v-if="curType === 'alipay'" class="mix-icon icon-xuanzhong"></text>
  39. </view>
  40. <!-- #endif -->
  41. <mix-button ref="confirmBtn" text="确认充值" marginTop="80rpx" @onConfirm="confirm"></mix-button>
  42. </view>
  43. <mix-empty v-else></mix-empty>
  44. <mix-loading v-if="isLoading" :mask="true"></mix-loading>
  45. <!-- 支付成功面板 -->
  46. <success-modal ref="successModal" :price="payPrice" btnText="确定" tip="余额充值成功" @onConfirm="successCallback"></success-modal>
  47. </view>
  48. </template>
  49. <script>
  50. import successModal from './components/success-modal.vue'
  51. export default {
  52. components:{
  53. successModal
  54. },
  55. data() {
  56. return {
  57. curType: 'wxpay',
  58. moneyList: [
  59. {money: 50, checked: true},
  60. {money: 100, checked: false},
  61. {money: 200, checked: false},
  62. {money: 500, checked: false},
  63. ],
  64. money: '',
  65. payPrice: 0,
  66. }
  67. },
  68. computed: {
  69. openExamine(){
  70. return this.$store.state.openExamine;
  71. }
  72. },
  73. watch: {
  74. money(val){
  75. if(val){
  76. this.moneyList.forEach(item=> {
  77. item.checked = false;
  78. })
  79. }
  80. }
  81. },
  82. onLoad() {
  83. if(!this.openExamine){
  84. uni.setNavigationBarTitle({
  85. title: '余额充值'
  86. })
  87. }else{
  88. uni.setNavigationBarTitle({
  89. title: '消费明细'
  90. })
  91. }
  92. },
  93. methods: {
  94. confirm(){
  95. let {curType, moneyList, money} = this;
  96. const mIndex = moneyList.findIndex(item=> item.checked);
  97. if(!money && mIndex === -1){
  98. this.$util.msg('请选择或输入充值金额');
  99. this.$refs.confirmBtn.stop();
  100. return;
  101. }
  102. money = this.money || this.moneyList[mIndex].money;
  103. if(isNaN(money)){
  104. this.$util.msg('请输入正确的充值金额');
  105. this.$refs.confirmBtn.stop();
  106. return;
  107. }
  108. this.$util.throttle(()=>{
  109. this.wxpay((+money).toFixed(2));
  110. })
  111. },
  112. //微信支付
  113. async wxpay(money){
  114. const res = await this.$request('payment', 'recharge', {
  115. money: +money,
  116. pay_type: 'wxpay',
  117. // #ifdef MP-WEIXIN
  118. code: await this.getMpCode('weixin'),
  119. // #endif
  120. }, {showLoading: true})
  121. this.log(res)
  122. if(res.status !== 1){
  123. this.$refs.confirmBtn.stop();
  124. this.$util.msg(res.msg);
  125. return;
  126. }
  127. const orderInfo = res.data.orderInfo;
  128. const {timeStamp, nonceStr, paySign} = orderInfo;
  129. const payParams = {
  130. provider: this.curType,
  131. orderInfo: res.data.orderInfo,
  132. timeStamp,
  133. nonceStr,
  134. package: orderInfo.package,
  135. signType: 'MD5',
  136. paySign,
  137. success: e=>{
  138. this.payPrice = money;
  139. this.$refs.confirmBtn.death();
  140. this.$refs.successModal.open();
  141. },
  142. fail: err=>{
  143. this.$refs.confirmBtn.stop();
  144. if(err.errMsg.indexOf('取消') > -1 || err.errMsg.indexOf('cancel') >1 || err.errMsg.indexOf('-2') > -1){
  145. this.$util.msg('取消支付');
  146. }else{
  147. this.$util.msg('支付遇到错误,请稍候重试');
  148. console.log(err);
  149. }
  150. }
  151. };
  152. uni.requestPayment(payParams);
  153. },
  154. //统一支付回调
  155. successCallback(){
  156. this.$store.dispatch('getUserInfo');
  157. uni.navigateBack();
  158. },
  159. checkePrice(item){
  160. this.moneyList.forEach(item=> {
  161. item.checked = false;
  162. })
  163. this.money = '';
  164. item.checked = true;
  165. },
  166. checkType(type){
  167. this.curType = type;
  168. },
  169. //获取小程序code
  170. // #ifdef MP
  171. getMpCode(provider){
  172. return new Promise((resolve)=>{
  173. uni.login({
  174. provider,
  175. success(res) {
  176. resolve(res.code)
  177. }
  178. })
  179. })
  180. }
  181. // #endif
  182. }
  183. }
  184. </script>
  185. <style scoped lang="scss">
  186. .page-tit{
  187. padding: 40rpx 0 30rpx 40rpx;
  188. font-size: 32rpx;
  189. color: #333;
  190. line-height: 48rpx ;
  191. font-weight: 700;
  192. }
  193. .price-list{
  194. display: flex;
  195. flex-wrap: wrap;
  196. padding-left: 40rpx;
  197. .p-item{
  198. width: 160rpx;
  199. height: 78rpx;
  200. border-radius: 14rpx;
  201. margin-bottom: 20rpx;
  202. margin-right: 10rpx;
  203. background-color: #efefef;
  204. font-size: 28rpx;
  205. color: #999;
  206. border: 1rpx solid #d7d7d7;
  207. &.active{
  208. background:linear-gradient(131deg, #ff7e94, #fb4361);
  209. color: #fff;
  210. border: 0;
  211. }
  212. }
  213. }
  214. .input-wrap{
  215. height: 130rpx;
  216. padding: 10rpx 40rpx 0;
  217. font-size: 32rpx;
  218. color: #333;
  219. .input{
  220. flex: 1;
  221. text-align: right;
  222. font-size: 28rpx;
  223. color: #333;
  224. }
  225. }
  226. .cell{
  227. margin: 0 40rpx;
  228. height: 124rpx;
  229. .icon{
  230. width: 40rpx;
  231. height: 40rpx;
  232. margin-right: 32rpx;
  233. }
  234. .tit{
  235. flex: 1;
  236. font-size: 30rpx;
  237. color: #333;
  238. font-weight: 700;
  239. }
  240. .tip{
  241. margin-top: 14rpx;
  242. font-size: 24rpx;
  243. color: #999;
  244. }
  245. .icon-xuanzhong{
  246. font-size: 36rpx;
  247. color: $base-color;
  248. }
  249. }
  250. </style>