Newer
Older
intelligentRobot / src / store / modules / settings.ts
wangxitong on 3 Sep 3 KB first commit
import { defaultsDeep } from 'lodash-es'
import type { RecursiveRequired, Settings } from '@/global'
import settings from '@/settings'
import settingsDefault from '@/settings.default'

interface SettingsStore extends RecursiveRequired<Settings.all> {
  subMenuCollapseLastStatus: boolean
  mode: 'pc' | 'mobile'
  title: string
  robot: {
    "alarmThreshold": 0,
    "confV": 0,
    "confW": 0,
    "createTime": "",
    "ptzInfo": {
      "deviceIp": "",
      "deviceUser": 0,
      "devicePassword": 0,
    },
    "currRoute": {
      "createTime": "",
      "id": 0,
      "robotId": 0,
      "routeName": "",
      "targetId": "",
      "taskType": 0,
      "updateTime": ""
    },
    "defaultTopic": "",
    "id": 0,
    "pcdDownload": "",
    "robotAdmin": "",
    "robotIp": "",
    "robotName": "",
    "robotPassword": "",
    "robotPort": 0,
    "robotUrl": "",
    "statusInfo": {
      "appNavPause": 0,
      "appNavPauseName": "",
      "appStopNavtrack": 0,
      "appStopNavtrackName": "",
      "chargeMsg": 0,
      "chargeMsgName": "",
      "connStatus": 0,
      "connStatusName": "",
      "createTime": "",
      "currRouteId": 0,
      "currTaskId": 0,
      "dataRecord": 0,
      "dataRecordName": "",
      "discernType": 0,
      "discernTypeName": "",
      "electricityQuantity": 0,
      "gpsMsg": 0,
      "gpsMsgName": "",
      "id": 0,
      "imuMsg": 0,
      "imuMsgName": "",
      "insStatus": 0,
      "insStatusName": "",
      "lidarMsg": 0,
      "lidarMsgName": "",
      "maxSpeed": 0,
      "navigationStatus": 0,
      "navigationStatusName": "",
      "postionX": 0,
      "postionY": 0,
      "postionZ": 0,
      "processMessage": 0,
      "robotId": 0,
      "robotV": 0,
      "robotW": 0,
      "track": 0,
      "trackName": "",
      "trackRecord": 0,
      "trackRecordName": "",
      "trackStatus": 0,
      "trackStatusName": "",
      "updateTime": "",
      "voltage": 0
    },
    "updateTime": ""
  }
}

const useSettingsStore = defineStore(
  // 唯一ID
  'settings',
  {
    state: (): SettingsStore => ({
      // 合并配置
      ...defaultsDeep(settings, settingsDefault),
      // 侧边栏是否收起(用于记录 pc 模式下最后的状态)
      subMenuCollapseLastStatus: settingsDefault.menu.subMenuCollapse,
      // 显示模式,支持:mobile、pc
      mode: 'pc',
      // 页面标题
      title: '',
      robot: {} as any
    }),
    actions: {
      // 设置网页标题
      setRobot(robot: any) {
        this.robot = robot
        localStorage.setItem('robot_id', robot.id)
      },
      // 设置网页标题
      setTitle(title: string) {
        this.title = title
      },
      // 设置访问模式
      setMode(width: number) {
        if (this.layout.enableMobileAdaptation) {
          // 先判断 UA 是否为移动端设备(手机&平板)
          if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
            this.mode = 'mobile'
          }
          else {
            // 如果为桌面设备,再根据页面宽度判断是否需要切换为移动端展示
            this.mode = width < 992 ? 'mobile' : 'pc'
          }
        }
        else {
          this.mode = 'pc'
        }
      },
      // 切换侧边栏导航展开/收起
      toggleSidebarCollapse() {
        this.menu.subMenuCollapse = !this.menu.subMenuCollapse
        if (this.mode === 'pc') {
          this.subMenuCollapseLastStatus = !this.subMenuCollapseLastStatus
        }
      },
      // 设置主题颜色模式
      setColorScheme(color: Required<Settings.app>['colorScheme']) {
        this.app.colorScheme = color
      },
      // 更新应用配置
      updateSettings(data: Settings.all) {
        Object.keys(data).forEach((key) => {
          this[key as keyof Settings.all] = defaultsDeep(data[key as keyof Settings.all], this[key as keyof Settings.all])
        })
      },
    },
  },
)

export default useSettingsStore