base.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { GET, POST, DELETE, FORM, PUT, STORE_UPDATE, STORE_LOADING, STORE_LOADED } from '../services/Constant';
  2. const API_TOKEN = 'API_TOKEN';
  3. export default class BaseStore {
  4. constructor({ key, local }) {
  5. this.key = key;
  6. this.local = local;
  7. }
  8. init(project, { Api, Cache, History }) {
  9. this.History = History;
  10. this.Api = Api;
  11. this.Cache = Cache;
  12. this.project = project;
  13. this.state = this.local ? Object.assign(this.initState(), this.getLocal(this.key) || {}, this.fixState()) : this.initState();
  14. }
  15. initState() {
  16. return {};
  17. }
  18. fixState() {
  19. return {};
  20. }
  21. setStore(store) {
  22. this.store = store;
  23. if (this.initAfter) this.initAfter();
  24. }
  25. setState(state) {
  26. if (!this.store) throw new Error('store init error');
  27. this.store.dispatch({
  28. key: this.key,
  29. type: STORE_UPDATE,
  30. state,
  31. });
  32. }
  33. setToken(token) {
  34. this.saveLocal(API_TOKEN, token);
  35. }
  36. getToken() {
  37. return this.getLocal(API_TOKEN);
  38. }
  39. apiGet(url, data, loadOption) {
  40. return this.request({ url, method: GET }, data, loadOption);
  41. }
  42. apiPost(url, data, loadOption) {
  43. return this.request({ url, method: POST }, data, loadOption);
  44. }
  45. apiPut(url, data, loadOption) {
  46. return this.request({ url, method: PUT }, data, loadOption);
  47. }
  48. apiDel(url, data, loadOption) {
  49. return this.request({ url, method: DELETE }, data, loadOption);
  50. }
  51. apiForm(url, data, loadOption) {
  52. return this.request({ url, method: POST, type: FORM }, data, loadOption);
  53. }
  54. getRequestHeader() {
  55. const headers = [];
  56. if (this.project.apiToken) {
  57. headers.push({ key: this.project.apiToken, value: this.getToken() });
  58. }
  59. return headers.length === 0 ? null : headers;
  60. }
  61. request(api, data = {}) {
  62. this.requestBeforeHook();
  63. return this.Api.request(api.url, {
  64. method: api.method,
  65. data,
  66. type: api.type,
  67. headers: this.getRequestHeader(),
  68. })
  69. .then(body => {
  70. try {
  71. return this.requestBodyHandle(body);
  72. } catch (err) {
  73. throw err;
  74. }
  75. })
  76. .finally(() => {
  77. this.requestAfterHook();
  78. })
  79. .end(result => {
  80. if (__DEBUG__) console.log(result);
  81. });
  82. }
  83. loading() {
  84. if (!this.store) throw new Error('store init error');
  85. this.store.dispatch({
  86. type: STORE_LOADING,
  87. });
  88. }
  89. loaded() {
  90. if (!this.store) throw new Error('store init error');
  91. this.store.dispatch({
  92. type: STORE_LOADED,
  93. });
  94. }
  95. requestBeforeHook() {
  96. this.loading();
  97. }
  98. requestBodyHandle(body) {
  99. if (body.status === 200) return body.result;
  100. throw new Error(body.message);
  101. }
  102. requestAfterHook() {
  103. this.loaded();
  104. }
  105. getCacheKey(key) {
  106. return `store.${key}`;
  107. }
  108. getLocal(key) {
  109. return this.Cache.getCache(this.getCacheKey(key), true);
  110. }
  111. saveLocal(key, value) {
  112. this.Cache.setCache(this.getCacheKey(key), value, 86400 * 30, true);
  113. }
  114. removeLocal(key) {
  115. this.Cache.removeCache(this.getCacheKey(key), true);
  116. }
  117. getApiCache(key, api, expireTime = 0) {
  118. const result = this.Cache.getCache(key);
  119. if (result) return Promise.resolve(result);
  120. return api().then(data => {
  121. this.Cache.setCache(key, data, expireTime);
  122. return data;
  123. });
  124. }
  125. merge(state, action) {
  126. const _ = Object.assign({}, state, action.state);
  127. if (this.local) {
  128. setImmediate(() => {
  129. this.saveLocal(this.key, _);
  130. });
  131. }
  132. return _;
  133. }
  134. handle(state = this.state, action) {
  135. this.state = action.key === this.key ? this.merge(state, action) : state;
  136. return this.state;
  137. }
  138. }