load.ts 5.3 KB

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