load.ts 4.7 KB

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