base.js 3.5 KB

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