123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <template>
- <div class="j-pay">
- <div class="j-main">
- <div class="price-info">
- <span class="info-text">需支付金额</span>
- <span class="price-count">
- <span class="pc-l">¥</span>
- <span class="pc-r">{{ orderInfo.price }}</span>
- </span>
- </div>
- <van-radio-group v-model="payWay">
- <van-cell-group>
- <van-cell v-for="(item, index) in payPlatFormWayMap[env.platform]" :key="index" center clickable :title="item.title" @click="radioCheck(item.type)">
- <template #icon>
- <div class="label-icon j-icon" :class="item.iconClass"></div>
- </template>
- <template #right-icon>
- <van-radio :name="item.type">
- <template #icon>
- <div class="radio-icon j-icon icon-duihao"></div>
- </template>
- </van-radio>
- </template>
- </van-cell>
- </van-cell-group>
- </van-radio-group>
- </div>
- <div class="j-button-group j-footer">
- <button class="j-button-confirm" @click="confirmPay">确认支付</button>
- </div>
- </div>
- </template>
- <script lang="ts">
- import { Component, Vue } from 'vue-property-decorator'
- import { Cell, CellGroup, Radio, RadioGroup } from 'vant'
- import { mapActions } from 'vuex'
- declare const JyObj: any
- @Component({
- name: 'pay',
- components: {
- [Cell.name]: Cell,
- [CellGroup.name]: CellGroup,
- [Radio.name]: Radio,
- [RadioGroup.name]: RadioGroup
- },
- methods: {
- ...mapActions({
- getOrderDetail: 'pay/orderDetail',
- getPaySign: 'pay/paySign',
- isPaySuccess: 'pay/isPaySuccess'
- })
- }
- })
- export default class Pay extends Vue {
- protected getOrderDetail!: any
- protected getPaySign!: any
- protected isPaySuccess!: any
- orderInfo = {
- ordercode: '',
- paymoney: ''
- }
- // 将原型的全局变量提出来
- env = this.$env
- payPlatFormWayMap = {
- wx: [
- {
- title: '微信支付',
- type: 'wx_js',
- iconClass: 'icon-wx-pay'
- }
- ],
- app: [
- {
- title: '微信支付',
- type: 'wx_app',
- iconClass: 'icon-wx-pay'
- },
- {
- title: '支付宝支付',
- type: 'ali_app',
- iconClass: 'icon-ali-pay'
- }
- ]
- }
- payWay = this.payPlatFormWayMap[this.env.platform][0].type
- created () {
- this.orderInfo.ordercode = this.$route.params.ordercode
- }
- mounted () {
- this.getInfo()
- console.log(this.$wxSdk)
- }
- radioCheck (type) {
- this.payWay = type
- }
- // 获取订单金额
- getInfo () {
- const toast = this.$toast.loading({
- message: '加载中...',
- forbidClick: true,
- duration: 0
- })
- this.getOrderDetail({ ordercode: this.orderInfo.ordercode }).then(res => {
- toast.clear()
- if (res.error_code === 0) {
- if (res.data.status === 1) {
- this.orderInfo.paymoney = res.data.paymoney
- } else {
- this.$toast(res.error_msg || '查询订单信息失败')
- }
- }
- })
- }
- // 确认支付
- confirmPay () {
- this.$toast.loading({
- message: '加载中...',
- forbidClick: true,
- duration: 0
- })
- const data = {
- payway: this.payWay,
- ordercode: this.orderInfo.ordercode
- }
- this.getPaySign(data).then(res => {
- if (res.error_code === 0) {
- this.$toast.clear()
- if (res.status === 1) {
- // 获取参数成功,判断是微信h5还是非微信h5
- if (this.env.isWeiXinBrowser) {
- this.wxPayCallBack(res.data.payStr)
- } else {
- this.appPayCallBack(res.data.payStr)
- }
- } else {
- this.$toast(res.error_msg || '获取支付信息失败')
- }
- }
- })
- }
- // 微信h5支付逻辑
- wxPayCallBack (paySign) {
- console.log('wxPay')
- console.log(paySign)
- if (!this.$wxSdk) {
- return this.$toast({
- message: '微信Sdk初始化失败',
- forbidClick: true,
- duration: 1000
- })
- }
- const sign = {
- timestamp: '',
- nonceStr: '',
- package: '',
- signType: '',
- paySign: '',
- appId: ''
- }
- // 进行支付
- console.log(this.$wxSdk)
- this.$wxSdk.chooseWXPay({
- config: {
- timestamp: sign.timestamp,
- nonceStr: sign.nonceStr,
- package: sign.package,
- signType: sign.signType,
- paySign: sign.paySign,
- appId: sign.appId
- }
- })
- }
- // app支付逻辑
- appPayCallBack (paySign) {
- console.log('appPay')
- if (this.payWay === 'wx_app') {
- // 调微信支付
- try {
- JyObj.wxPay(paySign)
- } catch (error) {
- console.log('请在指定环境运行', error)
- }
- } else if (this.payWay === 'ali_app') {
- // 调支付宝支付
- try {
- JyObj.aliPay(paySign)
- } catch (error) {
- console.log('请在指定环境运行', error)
- }
- }
- this.checkAppPaySuccess()
- }
- // 开启定时任务,3s查询一次是否支付成功
- checkAppPaySuccess () {
- const ordercode = this.orderInfo.ordercode
- const checkPaySuccessTimer = setInterval(() => {
- this.isPaySuccess({ ordercode }).then(res => {
- if (res.error_code === 0 && res.data.status === 1) {
- // 支付完成订单,关闭定时器
- clearInterval(checkPaySuccessTimer)
- this.$router.push(`/pay-success/${ordercode}`)
- }
- })
- }, 3000)
- }
- }
- </script>
- <style lang="scss">
- .j-pay {
- .price-info {
- display: flex;
- align-items: center;
- justify-content: center;
- flex-direction: column;
- margin-bottom: 8px;
- height: 80px;
- background-color: #fff;
- .info-text {
- color: #9B9CA3;
- font-size: 12px;
- line-height: 18px;
- margin-bottom: 4px;
- }
- .price-count {
- color: #FB483D;
- font-size: 18px;
- line-height: 30px;
- .pc-r {
- margin-left: 4px;
- font-size: 22px;
- }
- }
- }
- .van-cell {
- height: 54px;
- }
- .van-radio-group {
- .label-icon {
- width: 28px;
- height: 28px;
- }
- .van-cell__title {
- margin-left: 14px;
- font-size: 15px;
- line-height: 22px;
- color: #171826;
- }
- .radio-icon {
- display: none;
- }
- .van-radio__icon--checked {
- .radio-icon {
- display: inline-block;
- }
- }
- }
- }
- </style>
|