load.ts 5.2 KB

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