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

143 lines
3.7 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'
2025-07-16 09:34:46 +08:00
const wxworkId='ww8e14f3d20774c997' //企微id
const wxAppId='wx2d8208625006f74e' //小程序id
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-16 09:34:46 +08:00
work_info?:{session_key:string,openid:string},
environment?:string
2025-07-09 00:34:00 +08:00
}
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-16 09:34:46 +08:00
user: { id: 0, username: '' },
work_info:{session_key:'',openid:''},
environment:""
2025-07-09 00:34:00 +08:00
})
2025-07-08 16:49:39 +08:00
function login(app: any) {
2025-07-16 09:34:46 +08:00
const res:any = uni.getSystemInfoSync()
data.value.environment = res?.environment
console.log(res?.environment,'res?.environment')
2025-07-09 00:34:00 +08:00
if (res?.environment === 'wxwork') {
2025-07-16 09:34:46 +08:00
uni.qy.login({
success: function (res:any) {
console.log(res)
getWorkToken(app, res.code)
2025-07-09 00:34:00 +08:00
}
})
} else {
2025-07-16 09:34:46 +08:00
uni.login({
2025-07-09 00:34:00 +08:00
provider: 'weixin', //使用微信登录
success: function (loginRes) {
console.log(loginRes)
getWXToken(app, loginRes.code)
}
})
}
}
const getWorkToken = (app: any, code: string) => {
2025-07-16 09:34:46 +08:00
Apis.Login.Auth.WorkLogin({ code: code, app_id:wxworkId })
2025-07-09 00:34:00 +08:00
.then(res => {
console.log('登录', res?.data)
data.value.user = res.data?.user
2025-07-16 09:34:46 +08:00
data.value.work_info = res.data?.work_info
2025-07-09 00:34:00 +08:00
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) {
//未绑定账号
2025-07-16 09:34:46 +08:00
// uni.redirectTo({
// url: '/pages/login'
// })
2025-07-09 00:34:00 +08:00
return false
}
})
.catch(() => {
loading.value = false
hasError.value = true
})
}
const getWXToken = (app: any, code: string) => {
2025-07-16 09:34:46 +08:00
Apis.Login.Auth.Login({ code: code, app_id: wxAppId })
2025-07-09 00:34:00 +08:00
.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) {
//未绑定账号
2025-07-16 09:34:46 +08:00
// uni.redirectTo({
// url: '/pages/login'
// })
2025-07-09 00:34:00 +08:00
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
}
2025-07-16 09:34:46 +08:00
//企微绑定
const handleUserLoginWxwork = async (from_data: {
iv?: string
encryptedData?: string
}) => {
getApiLoading(Apis.Login.Auth.BindWork, {
app_id: wxAppId,
session_key: data?.value?.work_info?.session_key,
iv: from_data?.iv,
encrypted_data: from_data?.encryptedData
}).then(res => {
me()
uni.navigateBack({delta:1})
})
}
//小程序快捷绑定
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, {
2025-07-16 09:34:46 +08:00
app_id: wxAppId,
2025-07-09 00:34:00 +08:00
...from_data
}).then(res => {
me()
2025-07-16 09:34:46 +08:00
uni.navigateBack({delta:1})
2025-07-09 00:34:00 +08:00
})
}
2025-07-08 16:49:39 +08:00
return {
loading,
hasError,
data,
login,
me,
2025-07-16 09:34:46 +08:00
handleUserLogin,
handleUserLoginWxwork
2025-07-09 00:34:00 +08:00
}
})