load.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, 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 { ITerm, push } 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. const init = () => {
  51. get<ISiteInfoResponse | IErrorResponse>("/v2/siteinfo/en").then(
  52. (response) => {
  53. if ("title" in response) {
  54. const it: ISite = {
  55. title: response.title,
  56. subhead: "",
  57. keywords: [],
  58. description: "",
  59. copyright: "",
  60. logo: "",
  61. author: { name: "", email: "" },
  62. };
  63. store.dispatch(refreshLayout(it));
  64. }
  65. }
  66. );
  67. const token = getToken();
  68. if (token) {
  69. get<ITokenRefreshResponse | IErrorResponse>("/v2/auth/current").then(
  70. (response) => {
  71. console.log(response);
  72. if ("data" in response) {
  73. const it: IUser = {
  74. id: response.data.id,
  75. nickName: response.data.nickName,
  76. realName: response.data.realName,
  77. avatar: response.data.avatar,
  78. roles: response.data.roles,
  79. };
  80. store.dispatch(signIn([it, response.data.token]));
  81. }
  82. }
  83. );
  84. } else {
  85. console.log("no token");
  86. }
  87. //获取用户设置
  88. const setting = localStorage.getItem("user-settings");
  89. if (setting !== null) {
  90. const json: ISettingItem[] = JSON.parse(setting);
  91. store.dispatch(refreshSetting(json));
  92. }
  93. //获取术语表
  94. get<ITermResponse>(`/v2/term-vocabulary?view=grammar&lang=` + getLang()).then(
  95. (json) => {
  96. if (json.ok) {
  97. store.dispatch(push(json.data.rows));
  98. }
  99. }
  100. );
  101. get<ITermResponse>(
  102. `/v2/term-vocabulary?view=community&lang=` + getLang()
  103. ).then((json) => {
  104. if (json.ok) {
  105. store.dispatch(push(json.data.rows));
  106. }
  107. });
  108. //获取nissaya ending 表
  109. get<INissayaEndingResponse>(`/v2/nissaya-ending-vocabulary?lang=my`).then(
  110. (json) => {
  111. if (json.ok) {
  112. const nissayaEnding = json.data.rows.map((item) => item.ending);
  113. store.dispatch(nissayaEndingPush(nissayaEnding));
  114. }
  115. }
  116. );
  117. //获取 relation 表
  118. get<IRelationListResponse>(`/v2/relation?limit=1000`).then((json) => {
  119. if (json.ok) {
  120. const items: IRelation[] = json.data.rows.map((item, id) => {
  121. return {
  122. id: item.id,
  123. name: item.name,
  124. case: item.case,
  125. to: item.to,
  126. };
  127. });
  128. store.dispatch(pushRelation(items));
  129. }
  130. });
  131. //获取用户选择的主题
  132. const theme = localStorage.getItem("theme");
  133. if (theme === "dark") {
  134. store.dispatch(refreshTheme("dark"));
  135. } else {
  136. store.dispatch(refreshTheme("ant"));
  137. }
  138. //设置时区到cookie
  139. function setCookie(c_name: string, value: string, expiredays: number) {
  140. var exdate = new Date();
  141. exdate.setDate(exdate.getDate() + expiredays);
  142. document.cookie =
  143. c_name +
  144. "=" +
  145. escape(value) +
  146. (expiredays == null
  147. ? ""
  148. : "; expires=" + exdate.toUTCString() + ";path=/");
  149. }
  150. const date = new Date();
  151. const timezone = date.getTimezoneOffset();
  152. setCookie("timezone", timezone.toString(), 10);
  153. };
  154. export default init;