base.js 3.3 KB

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