123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import BaseStore from '@src/stores/base';
- // import * as querystring from 'querystring';
- export default class UserStore extends BaseStore {
- initState() {
- // const { code } = querystring.parse(window.location.search.replace('?', ''));
- // if (code) {
- // this.loginWechat(code);
- // }
- return { login: false };
- }
- initAfter() {
- if (this.state.login) {
- this.refreshToken().then(() => {
- if (this.adminLogin) {
- window.location.href = window.location.href.replace(`token=${this.adminLogin}`, '').replace('&&', '&');
- }
- })
- .catch(() => {
- this.setState({ login: false });
- });
- }
- }
- infoHandle(result) {
- if (result.token) this.setToken(result.token);
- this.setState({ login: result.id, needLogin: false, info: result, username: result.username });
- }
- originInviteCode(inviteCode) {
- this.setState({
- inviteCode,
- });
- }
- /**
- * 验证token
- */
- refreshToken() {
- return this.apiPost('/auth/token')
- .then(result => {
- this.infoHandle(result);
- })
- .catch(() => {
- this.logout(false);
- });
- }
- /**
- * 登陆
- * @param {*} area 区域码
- * @param {*} mobile 手机号
- * @param {*} mobileVerifyCode 手机验证码
- * @param {*} inviteCode 邀请人手机/邀请码
- * @param {*} email 绑定邮箱
- */
- login(area, mobile, mobileVerifyCode, inviteCode, email) {
- if (!inviteCode) {
- ({ inviteCode } = this.state);
- }
- return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
- this.infoHandle(result);
- return result;
- });
- }
- loginWechat(code, userInfo) {
- return this.apiGet('/auth/wechat', { code, userInfo }).then((result) => {
- this.infoHandle(result);
- return result;
- });
- }
- /**
- * 登出
- */
- logout(login = true) {
- return Promise.resolve()
- .then(() => {
- if (login) {
- return this.apiPost('/auth/logout', {});
- }
- return true;
- })
- .then(() => {
- this.setState({ login: false, info: {}, username: '' });
- })
- .then(() => {
- linkTo(this.project.loginPath);
- });
- }
- /**
- * 绑定手机
- * @param {*} area 区域码
- * @param {*} mobile 手机号
- * @param {*} mobileVerifyCode 手机验证码
- * @param {*} email 绑定邮箱
- * @param {*} inviteCode 邀请人手机/邀请码
- */
- bind(area, mobile, mobileVerifyCode, email, inviteCode) {
- if (!inviteCode) {
- ({ inviteCode } = this.state);
- }
- return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
- this.infoHandle(result);
- return result;
- });
- }
- /**
- * 查询邀请码对应账号
- * @param {*} code 邀请码
- */
- validInviteCode(code) {
- return this.apiGet('/auth/valid/invite_code', { code });
- }
- /**
- * 查询手机对应账号
- */
- validMobile(area, mobile) {
- return this.apiGet('/auth/valid/mobile', { area, mobile });
- }
- /**
- * 查询邮箱对应账号
- */
- validEmail(email) {
- return this.apiGet('/auth/valid/email', { email });
- }
- /**
- * 查询手机是否绑定微信
- */
- validWechat(area, mobile) {
- return this.apiGet('/auth/valid/wechat', { area, mobile });
- }
- }
- export const User = new UserStore({ key: 'user', local: true });
|