peixue-dev/peidu/uniapp/mixins/providerAuth.js

147 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import request from '@/utils/request'
/**
* 服务商权限检查 Mixin
* 用于需要服务商权限的页面
*/
export default {
data() {
return {
providerStatus: null, // null: 未申请, pending: 审核中, approved: 已通过, rejected: 已拒绝
isProviderChecking: false
}
},
methods: {
/**
* 检查服务商状态
* @param {Object} options 配置选项
* @param {Boolean} options.showGuide 是否显示引导(默认 true
* @param {Boolean} options.blockAccess 是否阻止访问(默认 true
* @returns {Promise<Boolean>} 是否有权限
*/
async checkProviderStatus(options = {}) {
const { showGuide = true, blockAccess = true } = options
if (this.isProviderChecking) {
return false
}
this.isProviderChecking = true
try {
const res = await request({
url: '/api/provider/status',
method: 'GET'
})
if (res && res.code === 200 && res.data) {
this.providerStatus = res.data.status
// 已通过认证
if (this.providerStatus === 'approved') {
return true
}
// 审核中
if (this.providerStatus === 'pending') {
if (showGuide) {
this.showPendingTip()
}
if (blockAccess) {
setTimeout(() => uni.navigateBack(), 2000)
}
return false
}
// 未申请或已拒绝
if (showGuide) {
this.showApplyGuide()
} else if (blockAccess) {
uni.navigateBack()
}
return false
}
// 接口返回错误,可能是未申请
if (res && res.code === 500 && res.message && res.message.includes('不是服务商')) {
this.providerStatus = null
if (showGuide) {
this.showApplyGuide()
} else if (blockAccess) {
uni.navigateBack()
}
return false
}
return false
} catch (error) {
console.error('检查服务商状态失败:', error)
// 网络错误,允许继续访问但提示
uni.showToast({
title: '网络错误,请稍后重试',
icon: 'none'
})
return false
} finally {
this.isProviderChecking = false
}
},
/**
* 显示申请引导
*/
showApplyGuide() {
uni.showModal({
title: '权限不足',
content: '您还不是服务商,需要先申请认证才能使用此功能',
confirmText: '去申请',
cancelText: '返回',
success: (res) => {
if (res.confirm) {
uni.redirectTo({
url: '/provider-package/pages/service-provider/apply'
})
} else {
uni.navigateBack()
}
}
})
},
/**
* 显示审核中提示
*/
showPendingTip() {
uni.showModal({
title: '审核中',
content: '您的服务商申请正在审核中,请耐心等待。审核通过后即可使用全部功能。',
showCancel: false,
confirmText: '我知道了'
})
},
/**
* 显示已拒绝提示
*/
showRejectedTip(reason) {
uni.showModal({
title: '审核未通过',
content: reason || '您的服务商申请未通过审核,请重新提交申请',
confirmText: '重新申请',
cancelText: '返回',
success: (res) => {
if (res.confirm) {
uni.redirectTo({
url: '/provider-package/pages/service-provider/apply'
})
} else {
uni.navigateBack()
}
}
})
}
}
}