2
0

load.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. //获取用户登录信息
  79. const token = getToken();
  80. if (token) {
  81. get<ITokenRefreshResponse>("/v2/auth/current").then((response) => {
  82. console.log("auth", response);
  83. if (response.ok) {
  84. const it: IUser = {
  85. id: response.data.id,
  86. nickName: response.data.nickName,
  87. realName: response.data.realName,
  88. avatar: response.data.avatar,
  89. roles: response.data.roles,
  90. };
  91. store.dispatch(signIn([it, response.data.token]));
  92. } else {
  93. localStorage.removeItem("token");
  94. store.dispatch(guest(true));
  95. }
  96. });
  97. } else {
  98. console.log("no token");
  99. store.dispatch(guest(true));
  100. }
  101. //获取用户设置
  102. const setting = localStorage.getItem("user-settings");
  103. if (setting !== null) {
  104. const json: ISettingItem[] = JSON.parse(setting);
  105. store.dispatch(refreshSetting(json));
  106. }
  107. //获取语法术语表
  108. grammarTermFetch();
  109. //获取术语表
  110. get<ITermResponse>(
  111. `/v2/term-vocabulary?view=community&lang=` + getLang()
  112. ).then((json) => {
  113. if (json.ok) {
  114. store.dispatch(update(json.data.rows));
  115. }
  116. });
  117. //获取nissaya ending 表
  118. get<INissayaEndingResponse>(`/v2/nissaya-ending-vocabulary?lang=my`).then(
  119. (json) => {
  120. if (json.ok) {
  121. const nissayaEnding = json.data.rows.map((item) => item.ending);
  122. store.dispatch(nissayaEndingPush(nissayaEnding));
  123. }
  124. }
  125. );
  126. //获取 relation 表
  127. get<IRelationListResponse>(`/v2/relation?vocabulary=true&limit=1000`).then(
  128. (json) => {
  129. if (json.ok) {
  130. const items: IRelation[] = json.data.rows.map((item, id) => {
  131. return {
  132. id: item.id,
  133. name: item.name,
  134. case: item.case,
  135. from: item.from,
  136. to: item.to,
  137. };
  138. });
  139. store.dispatch(pushRelation(items));
  140. }
  141. }
  142. );
  143. //获取用户选择的主题
  144. const theme = localStorage.getItem("theme");
  145. if (theme === "dark") {
  146. store.dispatch(refreshTheme("dark"));
  147. } else {
  148. store.dispatch(refreshTheme("ant"));
  149. }
  150. //设置时区到cookie
  151. function setCookie(c_name: string, value: string, expiredays: number) {
  152. var exdate = new Date();
  153. exdate.setDate(exdate.getDate() + expiredays);
  154. document.cookie =
  155. c_name +
  156. "=" +
  157. escape(value) +
  158. (expiredays == null
  159. ? ""
  160. : "; expires=" + exdate.toUTCString() + ";path=/");
  161. }
  162. const date = new Date();
  163. const timezone = date.getTimezoneOffset();
  164. setCookie("timezone", timezone.toString(), 10);
  165. };
  166. export default init;