index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { getRouter } from "@/utils/utils";
  2. import { observable, runInAction } from "mobx";
  3. import { getMenu, loginUserInfo, outLogin, selectCompanyApi } from "../API";
  4. import { exitFullScreen, isFull, requestFullScreen } from "../utils/fullScreen";
  5. export interface DataProps {
  6. /**用户初始信息*/
  7. userInfo: any;
  8. /**菜单*/
  9. menus: any;
  10. /**按钮权限*/
  11. btnAuthority: any[];
  12. /**公司列表*/
  13. companyList: any[];
  14. /**左侧菜单是否收起*/
  15. collapsed: boolean,
  16. /**是否是移动端*/
  17. isMobile: boolean,
  18. /**是否是全屏*/
  19. isFull: boolean,
  20. /**是否隐藏搜索框 */
  21. isFullscreen: boolean
  22. routerLoading: boolean
  23. }
  24. /**
  25. * 全局需要当前登录的状态
  26. * */
  27. const initStore: {
  28. data: DataProps;
  29. /**初始化操作*/
  30. userInit(): Promise<void>;
  31. /**获取用户信息*/
  32. getUserInfo(): Promise<void>;
  33. /**获取菜单*/
  34. menuInfo(): Promise<void>;
  35. /**退出登录*/
  36. loginOut(): Promise<void>;
  37. /**切换公司*/
  38. selectCompany(companyId: number): Promise<void>;
  39. /**菜单折叠事件*/
  40. menuCollapsed(collapsed: boolean): Promise<void>;
  41. /**全屏操作*/
  42. handleFullScreen(): Promise<void>;
  43. /**是否是移动端*/
  44. handleMobile(): Promise<void>;
  45. /**修改data的值 */
  46. updata(data: Partial<DataProps>): Promise<void>;
  47. } = {
  48. data: {
  49. userInfo: { userId: null },//用户初始信息
  50. menus: {},//菜单
  51. btnAuthority: [],//按钮权限
  52. companyList: [],//公司列表
  53. collapsed: false,//左侧菜单是否收起
  54. isMobile: false,//是否是移动端
  55. isFull: false,//是否是全屏
  56. isFullscreen: false,
  57. routerLoading: true,
  58. },
  59. /**初始化操作*/
  60. async userInit() {
  61. this.getUserInfo()
  62. this.menuInfo()
  63. this.handleMobile()
  64. },
  65. async updata(data) {
  66. this.data = { ...this.data, ...data }
  67. },
  68. /**获取用户信息*/
  69. async getUserInfo() {
  70. await loginUserInfo().then(res => {
  71. runInAction(() => {
  72. this.data.userInfo = { ...res?.data?.userInfo, onlineCompanyId: res?.data?.onlineCompanyId }
  73. this.data.companyList = res?.data?.companyRelationInfo
  74. let userName = res?.data?.userInfo?.nickname
  75. let userId = res?.data?.userInfo?.userId
  76. sessionStorage.setItem('userName', userName)
  77. sessionStorage.setItem('userId', userId)
  78. sessionStorage.setItem('powerLevel', res?.data?.userInfo?.powerLevel)
  79. })
  80. })
  81. },
  82. /**获取菜单*/
  83. async menuInfo() {
  84. this.data.routerLoading = true
  85. await getMenu().then(res => {
  86. // res.data = menu//=================临时虚拟数据==================
  87. let menu = getRouter(res?.data)
  88. let btn: any = []
  89. //返回按钮路由
  90. function btnFun(items: any) {
  91. items?.childrenBtn?.forEach((b: any,) => {
  92. btn.push(b.title)
  93. })
  94. }
  95. //返回子路由
  96. function routeFun(items: any) {
  97. return items.children.map((route: any) => {//循环重组
  98. if (route?.children?.length > 0) {
  99. routeFun(route)
  100. }
  101. if (route?.childrenBtn?.length > 0) {
  102. btnFun(route)
  103. }
  104. })
  105. }
  106. if (menu) {//添加按钮权限
  107. Object.values(menu).forEach((item: any) => {
  108. if (item?.length > 0) {
  109. routeFun(item[0])
  110. }
  111. })
  112. }
  113. runInAction(() => {
  114. // 过滤掉空的
  115. let arr = Object.keys(menu)?.filter(key => {
  116. return menu[key]?.length > 0
  117. })
  118. let obj: any = {}
  119. arr.forEach(a => {
  120. menu[a][0].children = menu[a][0].children?.filter((item: { title: any; }) => {
  121. let noArr = ["企微信息", "企微客户联系成员", "企微客服", "企微客服消息", "投诉信息"]
  122. return noArr.every(i => i !== item.title)
  123. })//过滤不要的菜单
  124. obj[a] = menu[a]
  125. })
  126. // 赋值
  127. this.data.menus = obj
  128. this.data.btnAuthority = btn
  129. })
  130. })
  131. runInAction(() => {
  132. this.data.routerLoading = false
  133. })
  134. },
  135. /**退出登录*/
  136. async loginOut() {
  137. outLogin().then(res => {
  138. localStorage.removeItem('Admin-Token')
  139. sessionStorage.removeItem('userId')
  140. window.location.href = window.location.origin + '/#/login'
  141. })
  142. },
  143. /**切换公司*/
  144. async selectCompany(companyId: number) {
  145. selectCompanyApi(companyId).then(res => {
  146. localStorage.setItem('Admin-Token', res?.data?.token)
  147. window.location.reload()
  148. })
  149. },
  150. /**菜单折叠事件*/
  151. async menuCollapsed(collapsed: any) {
  152. runInAction(() => {
  153. this.data.collapsed = collapsed
  154. })
  155. },
  156. /**是否是移动端*/
  157. async handleMobile() {
  158. let u = navigator.userAgent
  159. let isPhone: any = !!u.match(/AppleWebKit.*Mobile.*/) || u.indexOf('iPad') > -1
  160. if (isPhone) {
  161. runInAction(() => {
  162. this.data.isMobile = isPhone
  163. })
  164. }
  165. },
  166. /**全屏操作*/
  167. async handleFullScreen() {
  168. let isf: any = isFull()
  169. let f: any = !isf
  170. if (isf) {
  171. exitFullScreen()
  172. } else {
  173. requestFullScreen()
  174. }
  175. runInAction(() => {
  176. this.data.isMobile = f
  177. })
  178. },
  179. }
  180. export default observable(initStore)