123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import { GET, POST, DELETE, FORM, PUT, STORE_UPDATE, STORE_LOADING, STORE_LOADED } from '../services/Constant';
- const API_TOKEN = 'API_TOKEN';
- export default class BaseStore {
- constructor({ key, local }) {
- this.key = key;
- this.local = local;
- }
- init(project, { Api, Cache, History }) {
- this.History = History;
- this.Api = Api;
- this.Cache = Cache;
- this.project = project;
- this.state = this.local ? Object.assign(this.initState(), this.getLocal(this.key) || {}) : this.initState();
- }
- initState() {
- return {};
- }
- setStore(store) {
- this.store = store;
- if (this.initAfter) this.initAfter();
- }
- setState(state) {
- if (!this.store) throw new Error('store init error');
- console.log('setState', state);
- this.store.dispatch({
- key: this.key,
- type: STORE_UPDATE,
- state,
- });
- }
- setToken(token) {
- this.saveLocal(API_TOKEN, token);
- }
- getToken() {
- return this.getLocal(API_TOKEN);
- }
- apiGet(url, data, loadOption) {
- return this.request({ url, method: GET }, data, loadOption);
- }
- apiPost(url, data, loadOption) {
- return this.request({ url, method: POST }, data, loadOption);
- }
- apiPut(url, data, loadOption) {
- return this.request({ url, method: PUT }, data, loadOption);
- }
- apiDel(url, data, loadOption) {
- return this.request({ url, method: DELETE }, data, loadOption);
- }
- apiForm(url, data, loadOption) {
- return this.request({ url, method: POST, type: FORM }, data, loadOption);
- }
- getRequestHeader() {
- const headers = [];
- if (this.project.apiToken) {
- headers.push({ key: this.project.apiToken, value: this.getToken() });
- }
- return headers.length === 0 ? null : headers;
- }
- request(api, data = {}) {
- this.requestBeforeHook();
- return this.Api.request(api.url, {
- method: api.method,
- data,
- type: api.type,
- headers: this.getRequestHeader(),
- })
- .then(body => {
- try {
- return this.requestBodyHandle(body);
- } catch (err) {
- throw err;
- }
- })
- .finally(() => {
- this.requestAfterHook();
- })
- .end(result => {
- if (__DEBUG__) console.log(result);
- });
- }
- loading() {
- if (!this.store) throw new Error('store init error');
- this.store.dispatch({
- type: STORE_LOADING,
- });
- }
- loaded() {
- if (!this.store) throw new Error('store init error');
- this.store.dispatch({
- type: STORE_LOADED,
- });
- }
- requestBeforeHook() {
- this.loading();
- }
- requestBodyHandle(body) {
- if (body.status === 200) return body.result;
- throw new Error(body.message);
- }
- requestAfterHook() {
- this.loaded();
- }
- getCacheKey(key) {
- return `store.${key}`;
- }
- getLocal(key) {
- return this.Cache.getCache(this.getCacheKey(key), true);
- }
- saveLocal(key, value) {
- this.Cache.setCache(this.getCacheKey(key), value, 86400 * 30, true);
- }
- removeLocal(key) {
- this.Cache.removeCache(this.getCacheKey(key), true);
- }
- merge(state, action) {
- const _ = Object.assign({}, state, action.state);
- if (this.local) {
- setImmediate(() => {
- this.saveLocal(this.key, _);
- });
- }
- return _;
- }
- handle(state = this.state, action) {
- this.state = action.key === this.key ? this.merge(state, action) : state;
- return this.state;
- }
- }
|