load.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //import { Empty } from "google-protobuf/google/protobuf/empty_pb";
  2. //import { Duration } from "google-protobuf/google/protobuf/duration_pb";
  3. import {
  4. get as getToken,
  5. guest,
  6. type IUser,
  7. signIn,
  8. studioSignIn,
  9. } from "./reducers/current-user";
  10. //import { DURATION } from "./reducers/current-user";
  11. import { type ISite, refresh as refreshLayout } from "./reducers/layout";
  12. import {
  13. type ISettingItem,
  14. refresh as refreshSetting,
  15. } from "./reducers/setting";
  16. import { themeChange, type TThemeMode } from "./reducers/theme";
  17. import { get } from "./request";
  18. import { get as getLang } from "./locales";
  19. import store from "./store";
  20. import { grammar, type ITerm, update } from "./reducers/term-vocabulary";
  21. import { push as nissayaEndingPush } from "./reducers/nissaya-ending-vocabulary";
  22. import { pushRelation } from "./reducers/relation";
  23. import type { IGroupMemberListResponse } from "./api/group";
  24. import type { IAiModel } from "./api/ai";
  25. import type { IStudio } from "./api/Auth";
  26. import type { IRelation, IRelationListResponse } from "./api/relation";
  27. export interface ISettingModels {
  28. wbw?: IAiModel[];
  29. chat?: IAiModel[];
  30. }
  31. export interface ISiteInfoResponse {
  32. logo: string;
  33. title: string;
  34. subhead: string;
  35. models?: ISettingModels;
  36. }
  37. interface IUserData {
  38. id: string;
  39. nickName: string;
  40. realName: string;
  41. avatar: string;
  42. roles: string[];
  43. token: string;
  44. }
  45. export interface ITokenRefreshResponse {
  46. ok: boolean;
  47. message: string;
  48. data: IUserData;
  49. }
  50. interface ITermResponse {
  51. ok: boolean;
  52. message: string;
  53. data: {
  54. rows: ITerm[];
  55. count: number;
  56. };
  57. }
  58. interface INissayaEnding {
  59. ending: string;
  60. }
  61. interface INissayaEndingResponse {
  62. ok: boolean;
  63. message: string;
  64. data: {
  65. rows: INissayaEnding[];
  66. count: number;
  67. };
  68. }
  69. export const grammarTermFetch = () => {
  70. //获取语法术语表
  71. get<ITermResponse>(
  72. `/api/v2/term-vocabulary?view=grammar&lang=` + getLang()
  73. ).then((json) => {
  74. if (json.ok) {
  75. console.debug("grammar dispatch", json.data.rows);
  76. store.dispatch(grammar(json.data.rows));
  77. }
  78. });
  79. };
  80. const init = () => {
  81. get<ISite>("/api/v2/site-info/en").then((response) => {
  82. if ("title" in response) {
  83. const it: ISite = response;
  84. store.dispatch(refreshLayout(it));
  85. }
  86. });
  87. //获取用户登录信息
  88. const token = getToken();
  89. if (token) {
  90. get<ITokenRefreshResponse>("/api/v2/auth/current").then((response) => {
  91. console.log("auth", response);
  92. if (response.ok) {
  93. const it: IUser = {
  94. id: response.data.id,
  95. nickName: response.data.nickName,
  96. realName: response.data.realName,
  97. avatar: response.data.avatar,
  98. roles: response.data.roles,
  99. };
  100. store.dispatch(signIn([it, response.data.token]));
  101. } else {
  102. localStorage.removeItem("token");
  103. store.dispatch(guest(true));
  104. }
  105. });
  106. get<IGroupMemberListResponse>("/api/v2/group-member?view=user").then(
  107. (response) => {
  108. console.log("auth", response);
  109. if (response.ok) {
  110. const it: IStudio[] = response.data.rows.map((item) => {
  111. return item.group;
  112. });
  113. store.dispatch(studioSignIn(it));
  114. }
  115. }
  116. );
  117. } else {
  118. console.log("no token");
  119. store.dispatch(guest(true));
  120. }
  121. //获取用户设置
  122. const setting = localStorage.getItem("user-settings");
  123. if (setting !== null) {
  124. const json: ISettingItem[] = JSON.parse(setting);
  125. store.dispatch(refreshSetting(json));
  126. }
  127. //获取语法术语表
  128. grammarTermFetch();
  129. //获取术语表
  130. get<ITermResponse>(
  131. `/api/v2/term-vocabulary?view=community&lang=` + getLang()
  132. ).then((json) => {
  133. if (json.ok) {
  134. store.dispatch(update(json.data.rows));
  135. }
  136. });
  137. //获取nissaya ending 表
  138. get<INissayaEndingResponse>(`/api/v2/nissaya-ending-vocabulary?lang=my`).then(
  139. (json) => {
  140. if (json.ok) {
  141. const nissayaEnding = json.data.rows.map((item) => item.ending);
  142. store.dispatch(nissayaEndingPush(nissayaEnding));
  143. }
  144. }
  145. );
  146. //获取 relation 表
  147. const urlRelation = `/api/v2/relation?vocabulary=true&limit=1000`;
  148. console.debug("relations api request", urlRelation);
  149. get<IRelationListResponse>(urlRelation).then((json) => {
  150. console.debug("relations api response", json);
  151. if (json.ok) {
  152. const items: IRelation[] = json.data.rows.map((item) => {
  153. return {
  154. id: item.id,
  155. name: item.name,
  156. case: item.case,
  157. from: item.from,
  158. to: item.to,
  159. };
  160. });
  161. store.dispatch(pushRelation(items));
  162. }
  163. });
  164. //获取用户选择的主题
  165. const theme = localStorage.getItem("theme");
  166. if (theme) {
  167. store.dispatch(themeChange(theme as TThemeMode));
  168. } else {
  169. store.dispatch(themeChange("system"));
  170. }
  171. //设置时区到cookie
  172. function setCookie(c_name: string, value: string, expiredays: number) {
  173. const exdate = new Date();
  174. exdate.setDate(exdate.getDate() + expiredays);
  175. document.cookie =
  176. c_name +
  177. "=" +
  178. escape(value) +
  179. (expiredays == null
  180. ? ""
  181. : "; expires=" + exdate.toUTCString() + ";path=/");
  182. }
  183. const date = new Date();
  184. const timezone = date.getTimezoneOffset();
  185. setCookie("timezone", timezone.toString(), 10);
  186. };
  187. export default init;