Authored by htoooth

add request login

... ... @@ -14,6 +14,20 @@ export default {
name: 'App',
computed: {
...mapState(['yoho'])
},
mounted() {
if (this.yoho.context.needLogin) {
this.$yoho.ready(() => {
this.$sdk.goLogin();
});
}
},
watch: {
'yoho.context.needLogin': function(newVal) {
if (newVal) {
this.$sdk.goLogin();
}
},
}
};
</script>
... ...
... ... @@ -14,25 +14,31 @@ const errHandle = (error) => {
message: '服务器开小差了~'
});
};
const request = (options) => {
return axios(options).then(res => res.data, errHandle);
const request = (options, store) => {
return axios(options).then((res) => {
if (res.data.code === 401) {
store && store.commit('needLogin', {needLogin: true});
return Promise.reject(res.data);
}
return res.data;
}, errHandle);
};
export const createApi = () => {
export const createApi = (context, store) => {
return {
get(url, params, options) {
return request(Object.assign({
url,
params,
method: 'get',
}), options);
}), options, store);
},
post(url, data, options) {
return request(Object.assign({
url,
data,
method: 'post',
}, options));
}, options), store);
}
};
};
... ...
... ... @@ -8,6 +8,7 @@ import {Style, Toast, Dialog, DatePicker} from 'cube-ui'; //eslint-disable-line
import {get} from 'lodash';
import Lazy from 'vue-lazyload';
import yoho from 'common/yoho';
import sdk from 'yoho-activity-sdk';
import 'statics/scss/common.scss';
import 'statics/font/iconfont.css';
import 'statics/font/ufofont.css';
... ... @@ -25,6 +26,7 @@ if (window.__INITIAL_STATE__) {
window._router = get(store, 'state.yoho.context.route');
Vue.prop('yoho', yoho);
Vue.prop('sdk', sdk);
Vue.use(Toast);
Vue.use(DatePicker);
Vue.use(Dialog);
... ...
... ... @@ -40,6 +40,10 @@ export default context => {
return resolve(app);
}).catch(e => {
reportError(e);
if (e.code === 401) {
store.commit('needLogin', {needLogin: true});
}
context.state = store.state;
return resolve(app);
});
});
... ...
... ... @@ -131,7 +131,7 @@ export default {
methods: {
async onSubmit() {
if (!this.$yoho.isLogin()) {
this.$yoho.goLogin();
this.$sdk.goLogin();
return;
}
... ...
... ... @@ -18,7 +18,7 @@ export function createStore(context) {
strict: process.env.NODE_ENV !== 'production'
});
const api = createApi(context);
const api = createApi(context, store);
store.$api = api;
store.$context = context;
... ...
... ... @@ -39,16 +39,7 @@ export default {
},
async licenseStatus({ commit }) {
let result = {};
try {
result = await this.$api.get('/api/ufo/license/status');
} catch (e) {
if (e.code === 401) {
commit(Types.FETCH_UFO_NEED_LOGIN, true);
return;
}
}
let result = await this.$api.get('/api/ufo/license/status');
if (result.code === 200) {
commit(Types.FETCH_UFO_STATUS_SUCCESS, { status: result.data, message: result.message });
... ...
... ... @@ -15,7 +15,8 @@ export default function() {
fs: true,
},
route: '',
path: ''
path: '',
needLogin: false
},
historys: [],
visible: true,
... ... @@ -51,6 +52,9 @@ export default function() {
});
state.direction = 'forword';
}
},
[Types.NEED_LOGIN](state, {needLogin}) {
state.context.needLogin = needLogin;
}
},
actions: {
... ...
... ... @@ -4,3 +4,4 @@ export const ROUTE_CHANGE = 'ROUTE_CHANGE';
export const PAGE_INIT_VISIBLE = 'PAGE_INIT_VISIBLE';
export const YOHO_PAGE_VISIBLE = 'YOHO_PAGE_VISIBLE';
export const REPORT_YAS = 'REPORT_YAS';
export const NEED_LOGIN = 'needLogin';
... ...
... ... @@ -62,6 +62,7 @@
"vue-virtual-scroll-list": "^1.2.8",
"vuex": "^3.0.1",
"winston": "^3.1.0",
"yoho-activity-sdk": "^1.1.3",
"yoho-cookie": "^1.2.0",
"yoho-express-session": "^2.0.0",
"yoho-md5": "^2.1.0",
... ...
... ... @@ -9357,6 +9357,11 @@ yargs@^7.0.0:
y18n "^3.2.1"
yargs-parser "^5.0.0"
yoho-activity-sdk@^1.1.3:
version "1.1.3"
resolved "http://npm.yohops.com/yoho-activity-sdk/-/yoho-activity-sdk-1.1.3.tgz#14a8a952e1fd68cc7c167f1e6336d67262d55ba6"
integrity sha512-XbE6K/Ioid0Uguyf+2zdmFPgVALB9Ozsei1JLpp7MZhZNlVALQ0sER/L5sOQdZqIrtBmTiplur7u2pgaXl374g==
yoho-cookie@^1.2.0:
version "1.2.0"
resolved "http://npm.yohops.com/yoho-cookie/-/yoho-cookie-1.2.0.tgz#e8600ff0fcf316e8a9f88d32cd263396dc7e5f22"
... ...