|
| 1 | +import { defineStore } from 'pinia' |
| 2 | +import applicationApi from '@/api/application/application' |
| 3 | +import applicationXpackApi from '@/api/application/application-xpack' |
| 4 | +import { type Ref } from 'vue' |
| 5 | +import { getBrowserLang } from '@/locales/index' |
| 6 | +import useUserStore from './user' |
| 7 | +const useApplicationStore = defineStore('application', { |
| 8 | + state: () => ({ |
| 9 | + location: `${window.location.origin}/ui/chat/`, |
| 10 | + }), |
| 11 | + actions: { |
| 12 | + async asyncGetAllApplication() { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + applicationApi |
| 15 | + .getAllAppilcation() |
| 16 | + .then((data) => { |
| 17 | + resolve(data) |
| 18 | + }) |
| 19 | + .catch((error) => { |
| 20 | + reject(error) |
| 21 | + }) |
| 22 | + }) |
| 23 | + }, |
| 24 | + |
| 25 | + async asyncGetApplicationDetail(id: string, loading?: Ref<boolean>) { |
| 26 | + return new Promise((resolve, reject) => { |
| 27 | + applicationApi |
| 28 | + .getApplicationDetail(id, loading) |
| 29 | + .then((data) => { |
| 30 | + resolve(data) |
| 31 | + }) |
| 32 | + .catch((error) => { |
| 33 | + reject(error) |
| 34 | + }) |
| 35 | + }) |
| 36 | + }, |
| 37 | + |
| 38 | + async asyncGetApplicationDataset(id: string, loading?: Ref<boolean>) { |
| 39 | + return new Promise((resolve, reject) => { |
| 40 | + applicationApi |
| 41 | + .getApplicationDataset(id, loading) |
| 42 | + .then((data) => { |
| 43 | + resolve(data) |
| 44 | + }) |
| 45 | + .catch((error) => { |
| 46 | + reject(error) |
| 47 | + }) |
| 48 | + }) |
| 49 | + }, |
| 50 | + |
| 51 | + async asyncGetAccessToken(id: string, loading?: Ref<boolean>) { |
| 52 | + return new Promise((resolve, reject) => { |
| 53 | + const user = useUserStore() |
| 54 | + if (user.isEnterprise()) { |
| 55 | + applicationXpackApi |
| 56 | + .getAccessToken(id, loading) |
| 57 | + .then((data) => { |
| 58 | + resolve(data) |
| 59 | + }) |
| 60 | + .catch((error) => { |
| 61 | + reject(error) |
| 62 | + }) |
| 63 | + } else { |
| 64 | + applicationApi |
| 65 | + .getAccessToken(id, loading) |
| 66 | + .then((data) => { |
| 67 | + resolve(data) |
| 68 | + }) |
| 69 | + .catch((error) => { |
| 70 | + reject(error) |
| 71 | + }) |
| 72 | + } |
| 73 | + }) |
| 74 | + }, |
| 75 | + |
| 76 | + async asyncGetAppProfile(loading?: Ref<boolean>) { |
| 77 | + return new Promise((resolve, reject) => { |
| 78 | + applicationApi |
| 79 | + .getAppProfile(loading) |
| 80 | + .then((res) => { |
| 81 | + sessionStorage.setItem('language', res.data?.language || getBrowserLang()) |
| 82 | + resolve(res) |
| 83 | + }) |
| 84 | + .catch((error) => { |
| 85 | + reject(error) |
| 86 | + }) |
| 87 | + }) |
| 88 | + }, |
| 89 | + |
| 90 | + async asyncAppAuthentication( |
| 91 | + token: string, |
| 92 | + loading?: Ref<boolean>, |
| 93 | + authentication_value?: any, |
| 94 | + ) { |
| 95 | + return new Promise((resolve, reject) => { |
| 96 | + applicationApi |
| 97 | + .postAppAuthentication(token, loading, authentication_value) |
| 98 | + .then((res) => { |
| 99 | + localStorage.setItem(`${token}-accessToken`, res.data) |
| 100 | + sessionStorage.setItem(`${token}-accessToken`, res.data) |
| 101 | + resolve(res) |
| 102 | + }) |
| 103 | + .catch((error) => { |
| 104 | + reject(error) |
| 105 | + }) |
| 106 | + }) |
| 107 | + }, |
| 108 | + async refreshAccessToken(token: string) { |
| 109 | + this.asyncAppAuthentication(token) |
| 110 | + }, |
| 111 | + // 修改应用 |
| 112 | + async asyncPutApplication(id: string, data: any, loading?: Ref<boolean>) { |
| 113 | + return new Promise((resolve, reject) => { |
| 114 | + applicationApi |
| 115 | + .putApplication(id, data, loading) |
| 116 | + .then((data) => { |
| 117 | + resolve(data) |
| 118 | + }) |
| 119 | + .catch((error) => { |
| 120 | + reject(error) |
| 121 | + }) |
| 122 | + }) |
| 123 | + }, |
| 124 | + async validatePassword(id: string, password: string, loading?: Ref<boolean>) { |
| 125 | + return new Promise((resolve, reject) => { |
| 126 | + applicationApi |
| 127 | + .validatePassword(id, password, loading) |
| 128 | + .then((data) => { |
| 129 | + resolve(data) |
| 130 | + }) |
| 131 | + .catch((error) => { |
| 132 | + reject(error) |
| 133 | + }) |
| 134 | + }) |
| 135 | + }, |
| 136 | + }, |
| 137 | +}) |
| 138 | + |
| 139 | +export default useApplicationStore |
0 commit comments