diff --git a/src/api/index.js b/src/api/index.js index 46306c9..8b34a73 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -1,5 +1,5 @@ import axios from 'axios' -import qs from 'qs' +// import qs from 'qs' import { MessageBox, Message } from 'element-ui' import router from '@/router/index' import store from '@/store/index' @@ -15,12 +15,12 @@ const toLogin = () => { } const api = axios.create({ - baseURL: process.env.VUE_APP_API_ROOT, + baseURL: process.env.NODE_ENV !== 'development' && process.env.VUE_APP_API_ROOT, timeout: 10000, responseType: 'json', withCredentials: false, headers: { - 'Content-Type': 'application/json; charset=utf-8' + 'Content-Type': 'application/json' } }) @@ -30,37 +30,32 @@ api.interceptors.request.use( * 全局拦截请求发送前提交的参数 * 以下代码为示例,在登录状态下,分别对 post 和 get 请求加上 token 参数 */ + if (store.getters['token/isLogin']) { + request.headers.token = store.state.user.token + } if (request.method == 'post') { if (request.data instanceof FormData) { - if (store.getters['token/isLogin']) { + if (store.getters['user/isLogin']) { // 如果是 FormData 类型(上传图片) - request.data.append('token', store.state.token.token) + request.data.append('token', store.state.user.token) } } else { // 带上 token if (request.data == undefined) { request.data = {} } - if (store.getters['token/isLogin']) { - request.data.token = store.state.token.token - } // 参数验签 let timestamp = new Date().getTime() request.data.timestamp = '' + timestamp let sign = signMd5Utils.getSign(request.url, request.data) request.data.sign = sign - request.data = qs.stringify(request.data) } - } else { + } else if (request.method === 'get') { // 带上 token if (request.params == undefined) { request.params = {} } - if (store.getters['token/isLogin']) { - request.params.token = store.state.token.token - } let timestamp = new Date().getTime() - console.log(request.params) request.params.timestamp = '' + timestamp let sign = signMd5Utils.getSign(request.url, request.params) request.params.sign = sign diff --git a/src/assets/icons/like.svg b/src/assets/icons/like.svg new file mode 100644 index 0000000..f1b517b --- /dev/null +++ b/src/assets/icons/like.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/router/index.js b/src/router/index.js index 724f079..2c4bc52 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -46,7 +46,7 @@ Router.prototype.replace = function replace(location) { router.beforeEach((to, from, next) => { NProgress.start() if (to.meta.requireLogin) { - if (store.getters['token/isLogin']) { + if (store.getters['user/isLogin']) { next() NProgress.done() } else { diff --git a/src/router/modules/root.js b/src/router/modules/root.js index 7dfc8ed..960c719 100644 --- a/src/router/modules/root.js +++ b/src/router/modules/root.js @@ -1,34 +1,56 @@ export default [ { path: '/', - meta: { requireLogin: false }, - component: () => import(/* webpackChunkName: 'root' */ '@/views/Home.vue'), + meta: {requireLogin: false}, + component: () => import(/* webpackChunkName: 'root' */ '@/views/welcome.vue'), children: [ { path: '/login', - meta: { requireLogin: false }, + meta: {requireLogin: false}, component: () => import(/* webpackChunkName: 'root' */ '@/views/account/login.vue') }, { path: '/enterprise', - meta: { requireLogin: false }, + meta: {requireLogin: false}, component: () => import(/* webpackChunkName: 'root' */ '@/views/common/enterprise/index.vue') }, { path: '/sources', - meta: { requireLogin: false }, + meta: {requireLogin: false}, component: () => import(/* webpackChunkName: 'root' */ '@/views/common/sources/index.vue') }, { path: '/proposal', - meta: { requireLogin: false }, + meta: {requireLogin: false}, component: () => import(/* webpackChunkName: 'root' */ '@/views/common/proposal/index.vue') }, { path: '/console', - meta: { requireLogin: true }, + meta: {requireLogin: true}, component: () => import(/* webpackChunkName: 'root' */ '@/views/account/console/index.vue') } ] + }, { + path: '/home', + meta: {requireLogin: true}, + component: () => import(/* webpackChunkName: 'root' */ '@/views/home/index.vue'), + children: [ + { + path: '/', + meta: {requireLogin: true}, + component: () => import(/* webpackChunkName: 'root' */ '@/views/home/home-view') + } + ] + }, { + path: '/project', + meta: {requireLogin: true}, + component: () => import(/* webpackChunkName: 'root' */ '@/views/home/index.vue'), + children: [ + { + path: 'create', + meta: {requireLogin: true}, + component: () => import(/* webpackChunkName: 'root' */ '@/views/project/create.vue') + } + ] } ] diff --git a/src/store/modules/token.js b/src/store/modules/token.js index a5e5209..b58f8b4 100644 --- a/src/store/modules/token.js +++ b/src/store/modules/token.js @@ -7,14 +7,7 @@ const state = { const getters = { isLogin: state => { - let retn = false - if (state.token != null) { - let unix = Date.parse(new Date()) - if (unix < state.failuretime * 1000) { - retn = true - } - } - return retn + return state.token } } diff --git a/src/store/modules/user.js b/src/store/modules/user.js new file mode 100644 index 0000000..0994007 --- /dev/null +++ b/src/store/modules/user.js @@ -0,0 +1,45 @@ +// import api from '@/api' + +const state = { + token: localStorage.token, + userInfo: localStorage.userInfo +} + +const getters = { + isLogin: state => { + return state.token + }, + userInfo: state => { + return state.userInfo + } +} + +const actions = { + login(context, payload) { + return new Promise(resolve => { + // 模拟登录成功,写入 token 信息 + context.commit('setData', { + token: payload.token, + userInfo: payload + }) + resolve() + }) + } +} + +const mutations = { + setData(state, data) { + localStorage.setItem('token', data.token) + localStorage.setItem('userInfo', JSON.stringify(data.userInfo)) + state.token = data.token + state.userInfo = data.userInfo + } +} + +export default { + namespaced: true, + state, + actions, + getters, + mutations +} diff --git a/src/views/account/login.vue b/src/views/account/login.vue index 4fe1a48..316fabf 100644 --- a/src/views/account/login.vue +++ b/src/views/account/login.vue @@ -35,14 +35,16 @@ - - - + + - - + + @@ -53,7 +55,7 @@ - 登录 + 登录 使用第三方登录 或 @@ -75,15 +77,19 @@ @tab-click="registerHandleClick" > - + - + - + - + 发送验证码 @@ -92,22 +98,25 @@ - - + - - - + - + {{ validateCodeBtnText }} @@ -141,11 +150,21 @@ export default { callback() } } + const validateAccount = (rule, value, callback) => { + // /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{10,20}$/ + const reg1 = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/ + const reg2 = /^(?:0|86|\+86)?1[3456789]\d{9}$/ + if (reg1.test(value) || reg2.test(value)) { + callback() + } else { + callback(new Error('请输入正确的账号')) + } + } return { loginType: 'wx', validateCodeBtn: false, validateCodeBtnText: '发送验证码', - formType: 'reg', + formType: 'login', regType: 'regPhone', agreeProtocol: '', emailRegRules: { @@ -159,7 +178,12 @@ export default { password: [{required: true, trigger: 'blur', validator: validatePassword}], code: {required: true, trigger: 'blur', message: '请输入验证码'} }, - account: { + accountLoginRules: { + account: [ + {required: true, trigger: 'blur', message: '请输入账号'}, {trigger: 'blur', validator: validateAccount}], + password: [{required: true, trigger: 'blur', validator: validatePassword}] + }, + accountForm: { email: '', password: '' }, @@ -174,13 +198,14 @@ export default { sendEmailCodeHandle() { this.$refs.emailRegForm.validateField('email', err => { if (!err) { + this.validateCodeBtn = true this.$api.get(`/user/send-email-code?email=${this.account.email}`).then(() => { this.msgSuccess('验证码发送成功,5分钟内有效') this.validateCodeBtn = true let count = 60 let timer = setInterval(() => { count-- - this.validateCodeBtnText = count + 's' + this.validateCodeBtnText = count + 's后重新发送' if (count == 0) { this.validateCodeBtnText = '发送验证码' clearInterval(timer) @@ -193,27 +218,42 @@ export default { emailRegHandle() { this.$refs['emailRegForm'].validate(valid => { if (valid) { - this.$api.post('/user/email-register', this.account).then(() => { + this.$api.post('/user/email-register', this.accountForm).then(() => { this.msgSuccess('注册成功,快去登录吧') + setTimeout(() => { + this.formType = 'login' + this.loginType = 'account' + }, 1000) }) } else { return false } }) }, - login() { - this.$store.dispatch('token/login').then(() => { - // 登录成功后路由跳回 - if (this.$route.query.redirect) { - this.$router.replace({ - path: this.$route.query.redirect + loginHandle() { + this.$refs['accountLoginForm'].validate(valid => { + if (valid) { + this.$api.post('/user/account-login', this.accountForm).then(res => { + this.msgSuccess('登录成功') + this.$store.dispatch('user/login', res.data).then(() => { + // 登录成功后路由跳回 + // eslint-disable-next-line no-debugger + debugger + if (this.$route.query.redirect) { + this.$router.replace({ + path: this.$route.query.redirect + }) + } else { + if (window.history.length <= 1) { + this.$router.push({path: '/home'}) + } else { + this.$router.push({path: '/home'}) + } + } + }) }) } else { - if (window.history.length <= 1) { - this.$router.push({path: '/'}) - } else { - this.$router.go(-1) - } + return false } }) } @@ -222,7 +262,6 @@ export default { diff --git a/src/views/home/home-button.vue b/src/views/home/home-button.vue new file mode 100644 index 0000000..3fbad47 --- /dev/null +++ b/src/views/home/home-button.vue @@ -0,0 +1,39 @@ + + + + + diff --git a/src/views/home/home-view.vue b/src/views/home/home-view.vue new file mode 100644 index 0000000..d5fed63 --- /dev/null +++ b/src/views/home/home-view.vue @@ -0,0 +1,16 @@ + + + diff --git a/src/views/home/index.vue b/src/views/home/index.vue new file mode 100644 index 0000000..6b0451f --- /dev/null +++ b/src/views/home/index.vue @@ -0,0 +1,119 @@ + + + diff --git a/src/views/index.vue b/src/views/index.vue deleted file mode 100644 index d84423c..0000000 --- a/src/views/index.vue +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/src/views/login.vue b/src/views/login.vue deleted file mode 100644 index f1c02c4..0000000 --- a/src/views/login.vue +++ /dev/null @@ -1,28 +0,0 @@ - - - diff --git a/src/views/project/create.vue b/src/views/project/create.vue new file mode 100644 index 0000000..2084639 --- /dev/null +++ b/src/views/project/create.vue @@ -0,0 +1,232 @@ + + + + + diff --git a/src/views/Home.vue b/src/views/welcome.vue similarity index 94% rename from src/views/Home.vue rename to src/views/welcome.vue index a0d780b..7af6871 100644 --- a/src/views/Home.vue +++ b/src/views/welcome.vue @@ -6,9 +6,9 @@
- +
- 控 制 台 + 控 制 台 登 录 {{ item.title }}
@@ -37,9 +37,7 @@
-
- -
+
diff --git a/vue.config.js b/vue.config.js index 6b11618..a89dc02 100644 --- a/vue.config.js +++ b/vue.config.js @@ -65,6 +65,9 @@ module.exports = { }, configureWebpack: config => { config.resolve.modules = ['node_modules', 'assets/sprites'] + config.resolve.alias = { + '@': path.resolve(__dirname, 'src') + } config.plugins.push(...spritesmithTasks) if (isCDN) { config.externals = externals