@ -3,7 +3,7 @@ import { devtools } from 'zustand/middleware'
import { fetchJSON } from '@/utils/request'
import { fetchJSON } from '@/utils/request'
import { isEmpty , isNotEmpty } from '@/utils/commons'
import { isEmpty , isNotEmpty } from '@/utils/commons'
import { API _HOST , BUILD _VERSION } from '@/config'
import { API _HOST , BUILD _VERSION } from '@/config'
import { usingStorage } from '@/utils/usingStorage' ;
import { usingStorage } from '@/utils/usingStorage'
export const PERM _MERGE _CONVERSATION = 'merge-conversation'
export const PERM _MERGE _CONVERSATION = 'merge-conversation'
export const PERM _ASSIGN _NEW _CONVERSATION = 'assign-new-conversation'
export const PERM _ASSIGN _NEW _CONVERSATION = 'assign-new-conversation'
@ -13,133 +13,9 @@ export const PERM_IMPORT_EMAIL = 'import-email'
const WAI _SERVER _KEY = 'G-STR:WAI_SERVER'
const WAI _SERVER _KEY = 'G-STR:WAI_SERVER'
const useAuthStore = create ( devtools ( ( set , get ) => ( {
const useAuthStore = create (
loginUser : {
devtools (
userId : - 1 ,
( set , get ) => ( {
userIdStr : '-1' ,
username : '' ,
avatarUrl : '' ,
mobile : '' ,
email : '' ,
openId : '' ,
accountList : [ ] ,
emailList : [ ] ,
whatsAppBusiness : '' ,
accountName : '' ,
} ,
loginStatus : 0 ,
isPermitted : ( perm ) => {
const { waiServer } = usingStorage ( WAI _SERVER _KEY )
const { loginUser } = get ( )
if ( perm === PERM _USE _WHATSAPP ) {
return isNotEmpty ( waiServer ) // ['370', '143', '495', '404', '383', '227'].includes(loginUser.userId)
}
if ( perm === PERM _USE _EMAL ) {
return true //['501', '466', '599', '495', '143', '370', '639', '513', '654', '404', '383', '227'].includes(loginUser.userId)
}
// 导入邮件消息,需要配置才能使用
if ( perm === PERM _IMPORT _EMAIL && window . localStorage . getItem ( 'PERM_IMPORT_EMAIL' ) ) {
return true
}
if ( perm === PERM _MERGE _CONVERSATION ) {
return [ '404' , '383' , '227' ] . includes ( loginUser . userId )
}
if ( perm === PERM _ASSIGN _NEW _CONVERSATION ) {
return [ '79' , '383' , '404' , '227' ] . includes ( loginUser . userId )
}
// 以上是 Hardcode 判断
// 动态权限列表参考海外供应商平台实现
} ,
login : async ( authCode ) => {
const { setStorage } = usingStorage ( )
const { saveUserSession , setLoginStatus } = get ( )
setLoginStatus ( 200 )
const json = await fetchJSON (
'https://p9axztuwd7x8a7.mycht.cn/dingtalk/dingtalkwork/WhatsAppAuth' ,
{ authCode } ,
)
if ( json . errcode === 0 && isNotEmpty ( json . result . opisn ) ) {
// TODO: 保存个人 WhatsApp 服务器地址
const waiServer = json . result . whatsappinfo . length > 0 ? json . result . whatsappinfo [ 0 ] . wai _server : ''
setStorage ( 'G-STR:WAI_SERVER' , waiServer )
set ( ( ) => ( {
loginUser : {
userId : json . result . opisn ,
userIdStr : json . result ? . accountlist
. map ( ( acc ) => {
return acc . OPI _SN
} )
. join ( ',' ) ,
emailList : json . result ? . emaillist . map ( item => {
return {
opi _sn : item . opi _sn ,
mat _sn : item . mat _sn ,
email : item . email ,
default : item . Isdefaultemail == 1 ,
backup : item . Isbakemail == 1 ,
}
} ) ,
whatsAppBusiness : json . result . whatsappinfo . length > 0 ? json . result . whatsappinfo [ 0 ] . whatsapp _waba : '' ,
whatsAppNo : json . result . whatsappinfo . length > 0 ? json . result . whatsappinfo [ 0 ] . whatsapp _wa : '' ,
accountName : json . result . opicode ,
username : json . result . nick ,
avatarUrl : json . result . avatarUrl ,
mobile : '+' + json . result . stateCode + '-' + json . result . mobile ,
email : json . result . email ,
openId : json . result . openId ,
accountList : json . result . accountlist ,
} ,
} ) )
saveUserSession ( )
setLoginStatus ( 302 )
} else {
setLoginStatus ( 403 )
}
} ,
getPrimaryEmail : ( ) => {
const { loginUser } = get ( )
const defaultList = loginUser . emailList . filter ( email => {
return email . default
} )
if ( defaultList . length > 0 ) {
return defaultList [ 0 ] . email
} else {
const backupList = loginUser . emailList . filter ( email => {
return email . backup
} )
if ( backupList . length > 0 ) {
return backupList [ 0 ] . email
} else if ( loginUser . emailList . length > 0 ) {
return loginUser . emailList [ 0 ] . email
}
}
} ,
setLoginStatus : ( code ) => {
set ( ( ) => ( {
loginStatus : code ,
} ) )
} ,
logout : ( ) => {
window . sessionStorage . clear ( )
set ( ( ) => ( {
loginStatus : 0 ,
loginUser : {
loginUser : {
userId : - 1 ,
userId : - 1 ,
userIdStr : '-1' ,
userIdStr : '-1' ,
@ -153,80 +29,203 @@ const useAuthStore = create(devtools((set, get) => ({
whatsAppBusiness : '' ,
whatsAppBusiness : '' ,
accountName : '' ,
accountName : '' ,
} ,
} ,
} ) )
} ,
loginStatus : 0 ,
loadUserSession : ( ) => {
isPermitted : ( perm ) => {
let sessionData = window . sessionStorage . getItem ( 'GLOBAL_SALES_LOGIN_USER' )
const { waiServer } = usingStorage ( WAI _SERVER _KEY )
// let sessionData ='{"userId":"155","userIdStr":"155","emailList":[],"whatsAppBusiness":"+8617607730395","whatsAppNo":null,"username":"尹诚诚","avatarUrl":"https://static-legacy.dingtalk.com/media/lADPBE1XYG_HAcDNAgDNAgA_512_512.jpg","mobile":"+86-18507832160","email":"ycc@hainatravel.com","openId":"K8BNXMf8ESSr1DzLVUrX7wiEiE","accountList":[{"OPI_SN":155,"OPI_Code":"YCC","OPI_NameCN":"尹诚诚","OPI_DEI_SN":1,"OPI_NameEN":"Yin Chengcheng"}]}'
const { loginUser } = get ( )
// if (window.location.hostname === '202.103.68.93' && window.location.port === '4173' && isEmpty(sessionData)) {
if ( perm === PERM _USE _WHATSAPP ) {
// sessionData = `{"userId":"383","userIdStr":"383,609","emailList":[{"opi_sn":383,"mat_sn":760,"email":"lyj@asiahighlights.com","default":false,"backup":false},{"opi_sn":383,"mat_sn":759,"email":"lyj@chinahighlights.com","default":true,"backup":false},{"opi_sn":383,"mat_sn":758,"email":"lyj@hainatravel.com","default":false,"backup":false}],"username":"廖一军","avatarUrl":"https://static-legacy.dingtalk.com/media/lALPBDDrhXr716HNAoDNAoA_640_640.png","mobile":"+86-18777396951","email":"lyj@hainatravel.com","whatsAppBusiness":"8617458471254","openId":"iioljiPmZ4RPoOYpkFiSn7IKAiEiE","accountList":[{"OPI_SN":383,"OPI_Code":"LYJ","OPI_NameCN":"廖一军","OPI_DEI_SN":7,"OPI_NameEN":"Jimmy Liow"},{"OPI_SN":609,"OPI_Code":"LYJAH","OPI_NameCN":"廖一军( ah) ","OPI_DEI_SN":28,"OPI_NameEN":"Jimmy Liow"}]}`
return isNotEmpty ( waiServer ) // ['370', '143', '495', '404', '383', '227'].includes(loginUser.userId)
}
// window.localStorage.setItem('GLOBAL_SALES_LOGIN_USER', sessionData)
// }
if ( perm === PERM _USE _EMAL ) {
if ( import . meta . env . DEV && isEmpty ( sessionData ) ) {
return true //['501', '466', '599', '495', '143', '370', '639', '513', '654', '404', '383', '227'].includes(loginUser.userId)
sessionData = window . localStorage . getItem ( 'GLOBAL_SALES_LOGIN_USER' )
}
}
// 导入邮件消息,需要配置才能使用
if ( sessionData !== null ) {
if ( perm === PERM _IMPORT _EMAIL && window . localStorage . getItem ( 'PERM_IMPORT_EMAIL' ) ) {
const sesstionObj = JSON . parse ( sessionData )
return true
set ( ( ) => ( {
}
loginUser : sesstionObj ,
} ) )
if ( perm === PERM _MERGE _CONVERSATION ) {
}
return [ '404' , '383' , '227' ] . includes ( loginUser . userId )
} ,
}
saveUserSession : ( ) => {
if ( perm === PERM _ASSIGN _NEW _CONVERSATION ) {
const { loginUser } = get ( )
return [ '79' , '383' , '404' , '227' ] . includes ( loginUser . userId )
window . sessionStorage . setItem (
}
'GLOBAL_SALES_LOGIN_USER' ,
// 以上是 Hardcode 判断
JSON . stringify ( loginUser ) ,
// 动态权限列表参考海外供应商平台实现
)
} ,
} ,
login : async ( authCode ) => {
setWhatsAppProfile : async ( userId , whatsAppBusiness , whatsAppNo ) => {
const { setStorage } = usingStorage ( )
const { loginUser , saveUserSession } = get ( )
const { saveUserSession , setLoginStatus } = get ( )
const postWABAUrl = ` ${ API _HOST } /v2/set_whatsapp_info `
setLoginStatus ( 200 )
const params = { opi _sn : userId , whatsapp _waba : whatsAppBusiness , whatsapp _wa : whatsAppNo . replace ( /\D/g , '' ) } ;
const json = await fetchJSON ( 'https://p9axztuwd7x8a7.mycht.cn/dingtalk/dingtalkwork/WhatsAppAuth' , { authCode } )
return fetchJSON ( postWABAUrl , params )
. then ( json => {
if ( json . errcode === 0 && isNotEmpty ( json . result . opisn ) ) {
if ( json . errcode === 0 ) {
// TODO: 保存个人 WhatsApp 服务器地址
const waiServer = json . result . whatsappinfo . length > 0 ? json . result . whatsappinfo [ 0 ] . wai _server : ''
setStorage ( 'G-STR:WAI_SERVER' , waiServer )
set ( ( ) => ( {
set ( ( ) => ( {
loginUser : {
loginUser : {
... loginUser ,
userId : json . result . opisn ,
whatsAppNo : whatsAppNo ,
userIdStr : json . result ? . accountlist
whatsAppBusiness : whatsAppBusiness ,
. map ( ( acc ) => {
}
return acc . OPI _SN
} )
. join ( ',' ) ,
emailList : json . result ? . emaillist . map ( ( item ) => {
return {
opi _sn : item . opi _sn ,
mat _sn : item . mat _sn ,
email : item . email ,
default : item . Isdefaultemail == 1 ,
backup : item . Isbakemail == 1 ,
}
} ) ,
whatsAppBusiness : json . result . whatsappinfo . length > 0 ? json . result . whatsappinfo [ 0 ] . whatsapp _waba : '' ,
whatsAppNo : json . result . whatsappinfo . length > 0 ? json . result . whatsappinfo [ 0 ] . whatsapp _wa : '' ,
accountName : json . result . opicode ,
username : json . result . nick ,
avatarUrl : json . result . avatarUrl ,
mobile : '+' + json . result . stateCode + '-' + json . result . mobile ,
email : json . result . email ,
openId : json . result . openId ,
accountList : json . result . accountlist ,
} ,
} ) )
} ) )
saveUserSession ( )
saveUserSession ( )
setLoginStatus ( 302 )
} else {
} else {
throw new Error ( json ? . errmsg + ': ' + json . errcode )
setLoginStatus ( 403 )
}
}
} )
} ,
} ,
getPrimaryEmail : ( ) => {
sendNotify : async ( ) => {
const { loginUser } = get ( )
const { loginUser } = get ( )
const notifyUrl = 'https://p9axztuwd7x8a7.mycht.cn/dingtalk/dingtalkwork/SendMDMsgByDingRobotToGroup'
const defaultList = loginUser . emailList . filter ( ( email ) => {
return email . default
const params = {
} )
groupid : 'cidFtzcIzNwNoiaGU9Q795CIg==' ,
msgTitle : '有人求助' ,
if ( defaultList . length > 0 ) {
msgText : loginUser . username + '上传了销售平台' + BUILD _VERSION + '的日志'
return defaultList [ 0 ] . email
} ;
return fetchJSON ( notifyUrl , params )
. then ( json => {
if ( json . errcode === 0 ) {
console . info ( '发送通知成功' )
} else {
} else {
throw new Error ( json ? . errmsg + ': ' + json . errcode )
const backupList = loginUser . emailList . filter ( ( email ) => {
return email . backup
} )
if ( backupList . length > 0 ) {
return backupList [ 0 ] . email
} else if ( loginUser . emailList . length > 0 ) {
return loginUser . emailList [ 0 ] . email
}
}
}
} )
} ,
} ,
} ) , { name : 'authStore' } ) )
setLoginStatus : ( code ) => {
set ( ( ) => ( {
loginStatus : code ,
} ) )
} ,
logout : ( ) => {
window . sessionStorage . clear ( )
set ( ( ) => ( {
loginStatus : 0 ,
loginUser : {
userId : - 1 ,
userIdStr : '-1' ,
username : '' ,
avatarUrl : '' ,
mobile : '' ,
email : '' ,
openId : '' ,
accountList : [ ] ,
emailList : [ ] ,
whatsAppBusiness : '' ,
accountName : '' ,
} ,
} ) )
} ,
loadUserSession : ( ) => {
let sessionData =
import . meta . env . PROD === 'production'
? window . sessionStorage . getItem ( 'GLOBAL_SALES_LOGIN_USER' )
: '{"userId":"155","userIdStr":"155","emailList":[],"whatsAppBusiness":"+8617607730395","whatsAppNo":null,"username":"尹诚诚","avatarUrl":"https://static-legacy.dingtalk.com/media/lADPBE1XYG_HAcDNAgDNAgA_512_512.jpg","mobile":"+86-18507832160","email":"ycc@hainatravel.com","openId":"K8BNXMf8ESSr1DzLVUrX7wiEiE","accountList":[{"OPI_SN":155,"OPI_Code":"YCC","OPI_NameCN":"尹诚诚","OPI_DEI_SN":1,"OPI_NameEN":"Yin Chengcheng"}]}'
// if (window.location.hostname === '202.103.68.93' && window.location.port === '4173' && isEmpty(sessionData)) {
// sessionData = `{"userId":"383","userIdStr":"383,609","emailList":[{"opi_sn":383,"mat_sn":760,"email":"lyj@asiahighlights.com","default":false,"backup":false},{"opi_sn":383,"mat_sn":759,"email":"lyj@chinahighlights.com","default":true,"backup":false},{"opi_sn":383,"mat_sn":758,"email":"lyj@hainatravel.com","default":false,"backup":false}],"username":"廖一军","avatarUrl":"https://static-legacy.dingtalk.com/media/lALPBDDrhXr716HNAoDNAoA_640_640.png","mobile":"+86-18777396951","email":"lyj@hainatravel.com","whatsAppBusiness":"8617458471254","openId":"iioljiPmZ4RPoOYpkFiSn7IKAiEiE","accountList":[{"OPI_SN":383,"OPI_Code":"LYJ","OPI_NameCN":"廖一军","OPI_DEI_SN":7,"OPI_NameEN":"Jimmy Liow"},{"OPI_SN":609,"OPI_Code":"LYJAH","OPI_NameCN":"廖一军( ah) ","OPI_DEI_SN":28,"OPI_NameEN":"Jimmy Liow"}]}`
// window.localStorage.setItem('GLOBAL_SALES_LOGIN_USER', sessionData)
// }
if ( import . meta . env . DEV && isEmpty ( sessionData ) ) {
sessionData = window . localStorage . getItem ( 'GLOBAL_SALES_LOGIN_USER' )
}
if ( sessionData !== null ) {
const sesstionObj = JSON . parse ( sessionData )
set ( ( ) => ( {
loginUser : sesstionObj ,
} ) )
}
} ,
saveUserSession : ( ) => {
const { loginUser } = get ( )
window . sessionStorage . setItem ( 'GLOBAL_SALES_LOGIN_USER' , JSON . stringify ( loginUser ) )
} ,
setWhatsAppProfile : async ( userId , whatsAppBusiness , whatsAppNo ) => {
const { loginUser , saveUserSession } = get ( )
const postWABAUrl = ` ${ API _HOST } /v2/set_whatsapp_info `
const params = { opi _sn : userId , whatsapp _waba : whatsAppBusiness , whatsapp _wa : whatsAppNo . replace ( /\D/g , '' ) }
return fetchJSON ( postWABAUrl , params ) . then ( ( json ) => {
if ( json . errcode === 0 ) {
set ( ( ) => ( {
loginUser : {
... loginUser ,
whatsAppNo : whatsAppNo ,
whatsAppBusiness : whatsAppBusiness ,
} ,
} ) )
saveUserSession ( )
} else {
throw new Error ( json ? . errmsg + ': ' + json . errcode )
}
} )
} ,
sendNotify : async ( ) => {
const { loginUser } = get ( )
const notifyUrl = 'https://p9axztuwd7x8a7.mycht.cn/dingtalk/dingtalkwork/SendMDMsgByDingRobotToGroup'
const params = {
groupid : 'cidFtzcIzNwNoiaGU9Q795CIg==' ,
msgTitle : '有人求助' ,
msgText : loginUser . username + '上传了销售平台' + BUILD _VERSION + '的日志' ,
}
return fetchJSON ( notifyUrl , params ) . then ( ( json ) => {
if ( json . errcode === 0 ) {
console . info ( '发送通知成功' )
} else {
throw new Error ( json ? . errmsg + ': ' + json . errcode )
}
} )
} ,
} ) ,
{ name : 'authStore' } ,
) ,
)
export default useAuthStore
export default useAuthStore