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

114 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-07-15 16:18:34 +08:00
import { Apis } from '@/gen/Apis'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getApiLoading } from '../libraries/apiLoading'
2025-08-29 09:51:02 +08:00
const wxAppId = 'wx31500e871924b903' //小程序id
2025-07-15 16:18:34 +08:00
type DataType = {
user?: {
id: number
2025-08-29 09:51:02 +08:00
phone?: string
name?: string
2025-07-15 16:18:34 +08:00
}
orgs?: { id?: number; name?: string }[]
selectedOrg?: { id?: number; name?: string; is_show_procedure?: number }
2025-08-29 09:51:02 +08:00
work_info?: { session_key: string; openid: string }
2025-12-15 18:19:04 +08:00
selected_house?: { id?: string; full_name?: string; asset_projects_id?: string }
2025-08-29 09:51:02 +08:00
environment?: string
house_register?: boolean
house_occupant?: boolean
2025-12-15 18:19:04 +08:00
is_house_exist?: number
2025-07-15 16:18:34 +08:00
}
export const useWeAppAuthStore = defineStore('we_app_auth', () => {
const loading = ref<boolean>(true)
const hasError = ref<boolean>(false)
2025-12-15 18:19:04 +08:00
const is_house_exist = ref(0)
2025-07-15 16:18:34 +08:00
const data = ref<DataType>({
2025-08-29 09:51:02 +08:00
user: { id: 0, name: '' },
work_info: { session_key: '', openid: '' },
selected_house: {},
environment: '',
house_occupant: false,
2025-12-15 18:19:04 +08:00
house_register: false,
is_house_exist: 0
2025-07-15 16:18:34 +08:00
})
function login(app: any) {
uni.login({
provider: 'weixin', //使用微信登录
success: function (loginRes) {
console.log(loginRes)
getWXToken(app, loginRes.code)
}
})
}
const getWXToken = (app: any, code: string) => {
Apis.Login.Auth.Login({ code: code, app_id: wxAppId })
.then(res => {
console.log('登录', res?.data)
data.value.user = res.data?.user
2025-08-29 09:51:02 +08:00
data.value.selected_house = res.data?.selected_house
2025-07-15 16:18:34 +08:00
loading.value = false
uni.setStorageSync(import.meta.env.VITE_ACCESS_TOKEN_KEY, res?.data?.token?.token)
2025-08-29 09:51:02 +08:00
if (data.value.user) {
getIsHouseExist()
}
2025-07-15 16:18:34 +08:00
app?.appContext.config.globalProperties.$isResolve()
})
.catch(() => {
loading.value = false
hasError.value = true
})
}
2025-08-29 09:51:02 +08:00
const getIsHouseExist = () => {
Apis.Archive.HouseOccupants.GetCustomerHouseIsExist().then(res => {
data.value.house_occupant = res.data?.house_occupant || false
data.value.house_register = res.data?.house_register || false
2025-12-15 18:19:04 +08:00
data.value.is_house_exist = is_house_exist.value + 1
2025-08-29 09:51:02 +08:00
})
}
function me(fun?: () => void) {
2025-07-15 16:18:34 +08:00
getApiLoading(Apis.Login.Auth.Me, {}).then(res => {
data.value.user = res.data.user
2025-08-29 09:51:02 +08:00
data.value.selected_house = res.data?.selected_house
2025-07-15 16:18:34 +08:00
console.log('me', res.data)
loading.value = false
hasError.value = false
2025-08-29 09:51:02 +08:00
if (data.value.user) {
getIsHouseExist()
}
return fun?.()
2025-07-15 16:18:34 +08:00
})
}
2025-08-29 09:51:02 +08:00
//小程序快捷绑定
2025-07-15 16:18:34 +08:00
const handleUserLogin = async (from_data: {
code?: string
phone?: string
phone_validate_code?: string
}) => {
getApiLoading(Apis.Login.Auth.BindPhoneNumber, {
app_id: wxAppId,
...from_data
}).then(res => {
2025-08-29 09:51:02 +08:00
me(() => {
uni.navigateBack({ delta: 1 })
})
2025-07-15 16:18:34 +08:00
})
}
return {
loading,
hasError,
data,
login,
me,
2025-12-15 18:19:04 +08:00
handleUserLogin,
getIsHouseExist
2025-07-15 16:18:34 +08:00
}
})