模型 Model
global.js
import { routerRedux } from 'dva/router';
import { notification } from 'antd';
import Request from '@/utils/request';
import Storage from '@/utils/storage';
import ENV from '@/config/env'
export default {
namespace: 'global',
state: {
loading: true,
isAuth: false,
status: undefined,
lastTel: Storage.get(ENV.storageLastTel) || '',
currentUser: '',
roleList: [],
signModalVisible: false,
signTabKey: '',
theme: Storage.get(ENV.storageTheme) || {},
},
subscriptions: {
setup({ dispatch, history }) {
},
},
effects: {
*init({ payload }, { call, put }) {
const res = yield call(
(params) => {return Request('/api/init', {method: 'POST', body: params})},
payload
);
if(res.status === 1){
yield put({
type: 'changeAppInfo',
payload: res.data,
});
}
},
*register({ payload, callback }, { call, put }) {
const res = yield call(
(params) => {return Request('/api/register', {method: 'POST', body: params})},
payload
);
if(res.status === 1){
yield put({
type: 'changeLoginStatus',
payload: {
loading: false,
isAuth: true,
currentUser: res.data.currentUser,
}
});
Storage.set(ENV.storageLastTel, payload.tel);
Storage.set(ENV.storageToken, res.data.token);
}
yield callback(res);
},
*login({ payload, callback }, { call, put }) {
const res = yield call(
(params) => {return Request('/api/login', {method: 'POST', body: params})},
payload
);
if(res.status === 1){
yield put({
type: 'changeLoginStatus',
payload: {
loading: false,
isAuth: true,
currentUser: res.data.currentUser,
}
});
Storage.set(ENV.storageLastTel, payload.tel);
Storage.set(ENV.storageToken, res.data.token);
}
yield callback(res);
},
*token({ payload, callback }, { call, put }) {
if(payload.refreshToken) {
const res = yield call(
(params) => {return Request('/api/token', {method: 'POST', body: params})},
payload
);
yield callback(res);
if(res.status === 1){
Storage.set(ENV.storageToken, res.data.token);
yield put({
type: 'changeLoginStatus',
payload: {
loading: false,
isAuth: true,
currentUser: res.data,
}
});
}else{
yield put({
type: 'changeLoginStatus',
payload: {
loading: false,
isAuth: false,
currentUser: '',
}
});
}
}else{
yield put({ type: 'changeLoading', payload: false });
}
},
*logout({ payload, callback }, { call, put }) {
const res = yield call(
(params) => {return Request('/api/logout', {method: 'POST', body: params})},
payload
);
if(res.status === 1){
yield put({
type: 'changeLoginStatus',
payload: {
isAuth: false,
currentUser: '',
},
});
Storage.remove(ENV.storageToken);
}
},
*post({ url, payload, callback }, { call, put }) {
let res, exp = payload.exp, storage = Storage.get(url);
if(exp && storage){
res = storage;
}else{
res = yield call(
(params) => {return Request(url, {method: 'POST', body: params})},
payload
);
if(res.status === 1 && exp) Storage.set(url, res);
}
if(res.status === 9){
Storage.remove(ENV.storageToken);
notification.error({
message: '提示',
description: res.message
});
yield put({
type: 'changeLoginStatus',
payload: {
isAuth: false,
userInfo: '',
}
});
yield put(routerRedux.push({ pathname: '/' }));
}else{
yield callback(res);
}
},
*get({ url, payload, callback }, { call, put }) {
const res = yield call(
(params) => {return Request(url, {method: 'GET', body: params})},
payload
);
yield callback(res);
},
},
reducers: {
changeAppInfo(state, { payload }) {
return {
...state,
loading: false,
isAuth: !!payload.currentUser,
roleList: payload.roleList,
category: payload.category,
currentUser: payload.currentUser,
};
},
changeLoginStatus(state, { payload }) {
return {
...state,
loading: payload.loading,
isAuth: payload.isAuth,
currentUser: payload.currentUser,
token: payload.token,
};
},
changeSignModal(state, { payload }) {
return {
...state,
signModalVisible: payload.signModalVisible,
signTabKey: payload.signTabKey,
};
},
},
};
oss.js
import request, {FetchGet} from "@/utils/request"
import {notification} from 'antd'
const getClient = function (data) {
return new window.OSS.Wrapper({
region: data.region,
bucket: data.bucket,
accessKeyId: data.credentials.AccessKeyId,
accessKeySecret: data.credentials.AccessKeySecret,
stsToken: data.credentials.SecurityToken
})
}
const toBlob = function (urlData, fileType) {
let bytes = window.atob(urlData),
n = bytes.length,
u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bytes.charCodeAt(n)
}
return new Blob([u8arr], {type: fileType})
}
export default {
namespace: 'oss',
state: {
loading: false,
submitting: false,
exif: ''
},
effects: {
*list({payload, callback}, {call, put}) {
yield put({
type: 'changeLoading',
payload: true,
})
const res = yield call(
(params) => {
return request('/api/PhotoList', {method: 'POST', body: params,})
},
{payload}
)
yield callback(res)
yield put({
type: 'changeList',
payload: res,
})
yield put({
type: 'changeLoading',
payload: false,
})
},
*detail({payload, callback}, {call, put}) {
yield put({
type: 'changeLoading',
payload: true,
})
const res = yield call(
(params) => {
return request('/api/FileOssEdit', {method: 'POST', body: params,})
},
{}
)
yield callback(res)
yield put({
type: 'changeDetail',
payload: res,
})
yield put({
type: 'changeLoading',
payload: false,
})
},
*exif({payload, callback}, {call, put}) {
let url = payload.url + '?x-oss-process=image/info';
yield FetchGet(url)
.then((exif) => {
if (exif.FNumber) {
let FNumber = exif.FNumber.value
exif.FNumber.value = (FNumber.split('/')[0] / FNumber.split('/')[1]).toString()
}
return callback(exif)
})
.catch(function (err) {
notification.error({
message: 'exif查询失败!',
description: err,
})
})
},
*upload({payload, callback}, {call, put}) {
const res = yield call(
(params) => {
return request('/api/FileOssEdit', {method: 'POST', body: params,})
},
{}
)
if (res.status === 1) {
let client = getClient(res.data)
client.multipartUpload(payload.key, payload.file, {})
.then(function (r) {
let url = r.res.requestUrls[0].split('?')[0]
url = url.replace('metuwang.oss-cn-qingdao.aliyuncs.com', 'photo.metuwang.com')
return callback(url)
})
.catch(function (err) {
notification.error({
message: '上传失败!',
})
})
}
},
*uploadBase64({payload, callback}, {call, put}) {
const res = yield call(
(params) => {
return request('/api/FileOssEdit', {method: 'POST', body: params,})
},
{}
)
if (res.status === 1) {
let client = getClient(res.data)
let dataUrl = payload.base64
let base64 = dataUrl.split(',')[1]
let fileType = dataUrl.split(';')[0].split(':')[1]
let blob = toBlob(base64, fileType)
let reader = new FileReader()
reader.readAsArrayBuffer(blob)
reader.onload = function (event) {
let buffer = new window.OSS.Buffer(event.target.result)
client.put(payload.key, buffer).then(function (r) {
let url = r.res.requestUrls[0].split('?')[0]
url = url.replace('metuwang.oss-cn-qingdao.aliyuncs.com', 'photo.metuwang.com')
return callback(url)
}).catch(function (err) {
notification.error({
message: '上传失败!',
description: err,
})
})
}
}
},
*del({payload, callback}, {call, put}) {
yield put({
type: 'changeSubmitting',
payload: true,
})
const res = yield call(
(params) => {
return request('/api/FileOssEdit', {method: 'POST', body: params,})
},
{}
)
if (res.status === 1) {
let client = getClient(res.data)
client.deleteMulti(payload.keys)
.catch(function (err) {
notification.error({
message: '删除失败!',
description: err,
})
})
}
yield put({
type: 'changeSubmitting',
payload: false,
})
},
},
reducers: {
changeLoading(state, {payload}) {
return {
...state,
loading: payload,
uploadAvatar: '',
}
},
changeSubmitting(state, {payload}) {
return {
...state,
submitting: payload,
}
},
}
}
publish.js
import Request from '@/utils/request';
export default {
namespace: 'publish',
state: {
submitting: false,
publishType: '',
content: '',
thumb: '',
},
effects: {
*init({ payload }, { call, put }) {
const res = yield call(
(params) => {return Request('api/init', {method: 'POST', body: params})},
payload
);
if(res.status === 1){
yield put({
type: 'changeAppInfo',
payload: res,
});
}
},
},
reducers: {
changePublishType(state, {payload}) {
return {
...state,
publishType: payload.publishType
};
},
saveArticleContent(state, {payload}) {
return {
...state,
content: payload.content,
thumb: payload.thumb,
};
},
savePhotoContent(state, {payload}) {
return {
...state,
content: payload.content,
thumb: payload.thumb,
};
},
}
}