123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import { defineStore } from 'pinia'
- import { setToken, removeToken, setRefreshToken, getToken } from '@/utils/auth'
- import {
- smsLogin,
- passwordLogin,
- getBaseInfo,
- switchLoginOfEnterprise,
- getEnterprisingUserInfo,
- logoutToken,
- logout
- } from '@/api/common'
- import { getUserInfo } from '@/api/personal/user'
- import { getEntUpdatePasswordCheck } from '@/api/recruit/enterprise/information'
- import { getEnterpriseUserAccount, getAccountBalance, getUserAccount, userRegister } from '@/api/common'
- import { getEnterpriseBaseInfo } from '@/api/enterprise'
- import Snackbar from '@/plugins/snackbar'
- import { timesTampChange } from '@/utils/date'
- import { updateEventList } from '@/utils/eventList'
- import { getBaseInfoDictOfName } from '@/utils/getText'
- import { checkPersonBaseInfo } from '@/utils/check'
- // import Confirm from '@/plugins/confirm'
- // import { useIMStore } from './im'
- // const useIM = useIMStore()
- export const useUserStore = defineStore('user',
- {
- state: () => ({
- isAdmin: localStorage.getItem('isAdmin') === '1',
- token: getToken(),
- accountInfo: localStorage.getItem('accountInfo') ? JSON.parse(localStorage.getItem('accountInfo')) : {}, // 登录返回的信息
- userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {}, // 当前登录账号信息
- baseInfo: localStorage.getItem('baseInfo') ? JSON.parse(localStorage.getItem('baseInfo')) : {}, // 人才信息
- entBaseInfo: localStorage.getItem('entBaseInfo') ? JSON.parse(localStorage.getItem('entBaseInfo')) : {}, // 企业个人信息
- userAccount: {}, // 用户账户信息
- enterpriseUserAccount: {} // 企业账户信息
- }),
- actions: {
- // 个人用户注册并登录
- handleUserRegister (data) {
- return new Promise((resolve, reject) => {
- userRegister(data).then(async res => {
- setToken(res.accessToken)
- setRefreshToken(res.refreshToken)
- this.accountInfo = res
- localStorage.setItem('accountInfo', JSON.stringify(res))
- localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
- updateEventList(true) // 获取规则配置跟踪列表
- await this.getUserInfos()
- await this.getUserBaseInfos()
- resolve()
- }).catch(err => { reject(err) })
- })
- },
- // 短信登录
- handleSmsLogin (data) {
- return new Promise((resolve, reject) => {
- smsLogin(data).then(async res => {
- this.token = res.accessToken
- setToken(res.accessToken)
- setRefreshToken(res.refreshToken)
- this.accountInfo = res
- localStorage.setItem('accountInfo', JSON.stringify(res))
- localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
- updateEventList(true) // 获取规则配置跟踪列表
- await this.getUserInfos()
- await this.getUserBaseInfos()
- resolve(res)
- }).catch(err => { reject(err) })
- })
- },
- // 密码登录
- async handlePasswordLogin(data) {
- return new Promise((resolve, reject) => {
- data.account = data.phone
- passwordLogin(data).then(async res => {
- if (data.isEnterprise) { // 企业邮箱登录
- localStorage.setItem('emailLoginInfo', JSON.stringify(res))
- window.location.href = '/enterpriseVerification'
- } else {
- // 个人手机号登录
- setToken(res.accessToken)
- setRefreshToken(res.refreshToken)
- this.accountInfo = res
- localStorage.setItem('accountInfo', JSON.stringify(res))
- localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
- updateEventList(true) // 获取规则配置跟踪列表
- await this.getUserInfos()
- await this.getUserBaseInfos()
- }
- resolve()
- }).catch(err => {
- reject(err)
- })
- })
- },
- // 获取当前登录账户信息
- async getUserInfos () {
- try {
- // const api = getIsEnterprise() ? getEnterprisingUserInfo : getUserInfo
- const data = await getUserInfo({ id: this.accountInfo.userId })
- this.userInfo = data
- localStorage.setItem('userInfo', JSON.stringify(data))
- this.getUserAccountInfo()
- } catch (error) {
- Snackbar.error(error.msg)
- }
- },
- // 获取当前登录账户的基本信息(人才信息)
- async getUserBaseInfos (userId = null) {
- try {
- // const api = getIsEnterprise() ? null : getBaseInfo
- let data = await getBaseInfo({ userId: userId || this.accountInfo.userId })
- data = data || {}
- // if (!data) return localStorage.setItem('baseInfo', '{}')
- this.baseInfo = await this.getFieldText(data)
- localStorage.setItem('baseInfo', JSON.stringify(this.baseInfo))
- localStorage.setItem('necessaryInfoReady', checkPersonBaseInfo(this.baseInfo) ? 'ready' : 'fddeaddc47868b') // 校验是否完善人才必填信息
- } catch (error) {
- Snackbar.error(error)
- }
- },
- // 字典对应中文
- async getFieldText (data) {
- if (!data || !Object.keys(data).length) return {}
- if (data.birthday && data.birthday !== 0) data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
- if (data.firstWorkTime && data.firstWorkTime !== 0) data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
- if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
- if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
- if (data.eduType && data.eduType !== 0) await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
- if (data.expType && data.expType !== 0) await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
- if (data.sex && data.sex !== 0) await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
- if (data.jobType && data.jobType !== 0) await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
- if (data.jobStatus && data.jobStatus !== 0) await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
- if (data.maritalStatus && data.maritalStatus !== 0) await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
- return data
- },
- // 退出登录
- async userLogout (type) {
- // type: 1求职端 2招聘端
- if (type === 1) {
- await logout()
- } else await logoutToken(getToken(1))
- this.handleClearStorage()
- },
- // 清除缓存
- handleClearStorage () {
- removeToken()
- this.token = ''
- this.userInfo = {}
- this.baseInfo = {}
- this.accountInfo = {}
- // 商城模版数据不清楚缓存
- const mallTemplate = localStorage.getItem('mallTemplate')
- localStorage.clear()
- localStorage.setItem('mallTemplate', mallTemplate)
- },
- // 切换为招聘者
- async changeRole (res) {
- let data
- if (res?.type === 'emailLogin') {
- data = res
- } else {
- const enterpriseId = localStorage.getItem('enterpriseId') || ''
- if (!enterpriseId) return Snackbar.error('切换失败,请重新登录!')
- data = await switchLoginOfEnterprise({ enterpriseId })
- }
- setToken(data.accessToken, 1) // 个人切换企业->存放企业token
- setRefreshToken(data.refreshToken, 1) // 个人切换企业->存放企业refreshToken
- localStorage.setItem('accountInfo', JSON.stringify(data))
- localStorage.setItem('expiresTime', data.expiresTime)
- updateEventList(false)
- await this.updatePasswordCheck() // 检查密码是否需要修改
- await this.getEnterpriseInfo()
- await this.getEnterpriseUserAccountInfo()
-
- Snackbar.success(res?.type === 'emailLogin' ? '登录成功' : '切换成功')
- let href = '/enterprise'
- // 是否存在重定向
- if (localStorage.getItem('enterpriseRedirect')) {
- href = localStorage.getItem('enterpriseRedirect')
- localStorage.setItem('enterpriseRedirect', '')
- }
- // 人才推荐不需要跳转
- if (!res.noJump) {
- setTimeout(() => { window.location.href = href }, 1000)
- }
- },
- // 获取当前登录的企业用户信息
- async getEnterpriseInfo (check) {
- const result = await getEnterprisingUserInfo()
- this.entBaseInfo = result
-
- // 是否为企业账号管理员
- this.isAdmin = result.userType === '1'
- localStorage.setItem('isAdmin', result.userType)
- if (this.isAdmin && !check) await this.checkEnterpriseBaseInfo() // 校验企业必填信息
- localStorage.setItem('entBaseInfo', JSON.stringify(result))
- },
- // 获取企业账户信息
- async getEnterpriseUserAccountInfo () {
- const data = await getEnterpriseUserAccount()
- if (!data) return
- this.enterpriseUserAccount = data
- // this.getUserAccountBalance()
- localStorage.setItem('enterpriseUserAccount', JSON.stringify(data))
- return data // 方便直接获取
- },
- // 获取《企业基本信息》
- async checkEnterpriseBaseInfo () {
- try {
- const data = await getEnterpriseBaseInfo()
- if (data?.first === null || data?.first === false) { // null或者为false才弹
- localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '/recruit/enterprise/entInfoSetting')
- }
- if (!data?.bizFlag) { // 企业登录免费职位广告提示,除了true都弹窗
- localStorage.setItem('positionAd', 'showPositionAd')
- }
- // // 检验必填信息
- // const keyArr = ['industryId', 'financingStatus', 'scale', 'introduce', 'logoUrl'] // 必填信息列表
- // let href = '/recruit/enterprise/entInfoSetting'
- // const valid = Object.keys(data).length && keyArr.every(e => {
- // const bool = data[e] && data[e] !== 0
- // if (!bool && e === 'logoUrl') href = '/recruit/enterprise/entInfoSetting?tabKey=2'
- // return bool
- // })
- // if (!valid) {
- // localStorage.setItem('checkEnterpriseBaseInfoFalseHref', href)
- // }
- } catch (error) {
- // console.log(error)
- }
- },
- // 获取用户账户信息
- async getUserAccountInfo () {
- const data = await getUserAccount()
- if (!data) return
- this.userAccount = data
- this.getUserAccountBalance()
- // localStorage.setItem('userAccount', JSON.stringify(data))
- },
- // 获取账户余额
- async getUserAccountBalance () {
- const data = await getAccountBalance()
- const obj = Object.assign(this.userAccount, data)
- localStorage.setItem('userAccount', JSON.stringify(obj))
- },
- // 检查密码是否需要修改
- async updatePasswordCheck () {
- const bool = await getEntUpdatePasswordCheck()
- if (bool) {
- // 强制修改密码
- localStorage.setItem('entUpdatePassword', bool ? 'needChange' : 'doNotNeedChange')
- }
- }
- }
- },
- {
- persist: true,
- devtools: true
- }
- )
|