pay-employee/src/common/store/useWeAppAuthStore.ts

117 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-07-09 00:34:00 +08:00
import { Apis } from '@/gen/Apis'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getApiLoading } from '../libraries/apiLoading'
import { setTabBar } from '../libraries/setTabBar'
2025-07-08 16:49:39 +08:00
type DataType = {
user?: {
2025-07-09 00:34:00 +08:00
id: number
username: string
}
orgs?: { id?: number; name?: string }[]
selectedOrg?: { id?: number; name?: string; is_show_procedure?: number }
}
2025-07-08 16:49:39 +08:00
2025-07-09 00:34:00 +08:00
export const useWeAppAuthStore = defineStore('we_app_auth', () => {
const loading = ref<boolean>(true)
const hasError = ref<boolean>(false)
2025-07-08 16:49:39 +08:00
const data = ref<DataType>({
2025-07-09 00:34:00 +08:00
user: { id: 0, username: '' }
})
2025-07-08 16:49:39 +08:00
function login(app: any) {
2025-07-09 00:34:00 +08:00
const res = uni.getSystemInfoSync()
if (res?.environment === 'wxwork') {
uni?.qy?.login({
success: function (loginRes) {
console.log(loginRes)
getWorkToken(app, loginRes.code)
}
})
} else {
uni?.login({
provider: 'weixin', //使用微信登录
success: function (loginRes) {
console.log(loginRes)
getWXToken(app, loginRes.code)
}
})
}
}
const getWorkToken = (app: any, code: string) => {
Apis.Login.Auth.WorkLogin({ code: code, app_id: 'ww8e14f3d20774c997' })
.then(res => {
console.log('登录', res?.data)
data.value.user = res.data?.user
loading.value = false
uni.setStorageSync(import.meta.env.VITE_ACCESS_TOKEN_KEY, res?.data?.token?.token)
app?.appContext.config.globalProperties.$isResolve()
if (!res.data?.user) {
//未绑定账号
uni.redirectTo({
url: '/pages/login'
2025-07-08 16:49:39 +08:00
})
2025-07-09 00:34:00 +08:00
return false
}
})
.catch(() => {
loading.value = false
hasError.value = true
})
}
const getWXToken = (app: any, code: string) => {
Apis.Login.Auth.Login({ code: code, app_id: 'wx09b52ea34b5e8855' })
.then(res => {
console.log('登录', res?.data)
data.value.user = res.data?.user
loading.value = false
uni.setStorageSync(import.meta.env.VITE_ACCESS_TOKEN_KEY, res?.data?.token?.token)
app?.appContext.config.globalProperties.$isResolve()
if (!res.data?.user) {
//未绑定账号
uni.redirectTo({
url: '/pages/login'
})
return false
}
})
.catch(() => {
loading.value = false
hasError.value = true
})
2025-07-08 16:49:39 +08:00
}
function me() {
2025-07-09 00:34:00 +08:00
getApiLoading(Apis.Login.Auth.Me, {}).then(res => {
data.value.user = res.data.user
console.log('me', res.data)
loading.value = false
hasError.value = false
})
2025-07-08 16:49:39 +08:00
}
const handleUserLogin = async (from_data: {
2025-07-09 00:34:00 +08:00
code?: string
phone?: string
phone_validate_code?: string
2025-07-08 16:49:39 +08:00
}) => {
2025-07-09 00:34:00 +08:00
getApiLoading(Apis.Login.Auth.BindPhoneNumber, {
app_id: 'wx09b52ea34b5e8855',
...from_data
}).then(res => {
me()
})
}
2025-07-08 16:49:39 +08:00
return {
loading,
hasError,
data,
login,
me,
2025-07-09 00:34:00 +08:00
handleUserLogin
}
})