chenlong пре 4 година
родитељ
комит
4e209268da

+ 4 - 7
.env.development

@@ -1,15 +1,12 @@
+#由于每个人开发环境可能不一样 本地开发时需将此文件添加至.gitignore
 #请求地址api
-VUE_APP_API_DEV=''
+VUE_APP_API_DEV='http://localhost'
+#多代理api 开发环境下使用
+VUE_APP_API_MOCK='http://localhost'
 
 #是否mock数据
 VUE_APP_BUILD_MODE=!NOMOCK
 
-VUE_APP_PUBLIC_PATH=/
-VUE_APP_Version=1.0.0
-
-#多代理api 开发环境下使用
-VUE_APP_API_MOCK=''
-
 #开发
 VUE_APP_DEV_HOST=0.0.0.0
 VUE_APP_DEV_PORT=8080

+ 0 - 4
babel.config.js

@@ -1,9 +1,5 @@
 module.exports = {
   presets: [
     '@vue/cli-plugin-babel/preset'
-  ],
-  //添加忽略项
-  ignore: [
-
   ]
 }

+ 1 - 0
package.json

@@ -11,6 +11,7 @@
   },
   "dependencies": {
     "core-js": "^3.6.5",
+    "mockjs": "^1.1.0",
     "vue": "^2.6.11",
     "vue-router": "^3.4.7",
     "vuex": "^3.5.1"

+ 31 - 0
src/mock/api/login.js

@@ -0,0 +1,31 @@
+const userDB = [
+    { username: 'admin', password: 'admin', uuid: 'admin-uuid', name: '管理员' },
+    { username: 'editor', password: 'editor', uuid: 'editor-uuid', name: '编辑' },
+    { username: 'user1', password: 'user1', uuid: 'user1-uuid', name: '用户1' }
+]
+
+export default [
+    {
+        path: '/api/login',
+        method: 'post',
+        handle ({ body }) {console.log('=======');
+            const user = userDB.find(e => e.username === body.username && e.password === body.password)
+            if (user) {
+                return {
+                    code: 0,
+                    msg: '登录成功',
+                    data: {
+                        ...user,
+                        token: '8dfhassad0asdjwoeiruty'
+                    }
+                }
+            } else {
+                return {
+                    code: 401,
+                    msg: '用户名或密码错误',
+                    data: {}
+                }
+            }
+        }
+    }
+]

+ 73 - 0
src/mock/cmock/index.js

@@ -0,0 +1,73 @@
+import Mock from 'mockjs'
+import qs from 'qs'
+import withCredentials from './patch/withCredentials'
+
+/* 补丁 */
+withCredentials(Mock)
+
+/* Mock 默认配置 */
+Mock.setup({ timeout: '200-300' })
+
+/* 扩展 [生成器] */
+const Generator = (prop, template) => {
+  const obj = {}
+  obj[prop] = [template]
+  return Mock.mock(obj)
+}
+
+/* 扩展 [循环] */
+const Repeat = (num, itemTemplate) => Generator(`data|${num}`, itemTemplate).data
+
+const CustomExtends = {
+  Generator,
+  Repeat,
+  Mock,
+  Random: Mock.Random
+}
+
+const extend = (prop, value) => {
+  CustomExtends[prop] = value
+}
+
+/* 装配配置组 */
+const wired = ({ url, type, body }) => ({
+  method: type,
+  params: qs.parse(url.split('?').length > 1 ? url.split('?')[1] : ''),
+  body: JSON.parse(body),
+  url: qs.parse(url.split('?')[0]),
+  ...CustomExtends
+})
+
+const setup = (path, method, handle) => {
+  Mock.mock(
+    RegExp(path),
+    method,
+    typeof handle === 'function' ? o => handle(wired(o)) : handle
+  )
+}
+
+const load = (collection) => {
+  collection.map(({ path, method, handle }) => {
+    if (method === '*') {
+      method = [
+        'get',
+        'head',
+        'post',
+        'put',
+        'delete',
+        'connect',
+        'options',
+        'trace',
+        'patch'
+      ]
+    }
+    if (typeof method === 'string' && method.indexOf('|') > -1) method = method.split('|')
+    if (method instanceof Array) {
+      method.map(item => setup(path, item, handle))
+    } else {
+      setup(path, method, handle)
+    }
+  })
+}
+
+export default { setup, load, extend }

+ 8 - 0
src/mock/cmock/patch/withCredentials.js

@@ -0,0 +1,8 @@
+export default function (Mock) {
+  // http://cnine.me/note/FrontEnd/mock-lose-cookies-dbg.html
+  Mock.XHR.prototype.__send = Mock.XHR.prototype.send
+  Mock.XHR.prototype.send = function () {
+    if (this.custom.xhr) this.custom.xhr.withCredentials = this.withCredentials || false
+    this.__send.apply(this, arguments)
+  }
+}

+ 10 - 0
src/mock/index.js

@@ -0,0 +1,10 @@
+import cMock from './cmock'
+
+const req = context => context.keys().map(context)
+const options = req(require.context('./api/', true, /\.js$/))
+    .filter(e => e.default)
+    .map(e => e.default)
+
+options.forEach(option => {
+    cMock.load(option)
+})

+ 1 - 14
src/modules/func/pages/Login.vue

@@ -3,8 +3,6 @@
 </template>
 
 <script>
-    import { GetUserInfo } from '@/api/login'
-    import {Toast} from "vant";
     export default {
         name: "login",
         data(){
@@ -13,20 +11,9 @@
           }
         },
         mounted() {
-          //this.getUserInfo()
-          console.log(this.$route.fullPath)
         },
         methods:{
-          getUserInfo(userInfo={}){
-            let that = this;
-            GetUserInfo().then(async res => {
-              if(res.status == 200){
-                Toast.clear();
-              }else{
-                Toast(res.message);
-              }
-            })
-          },
+
         }
     }
 </script>

+ 1 - 1
src/routers/index.js

@@ -54,7 +54,7 @@ index.beforeEach(async (to, from, next) => {
     }
 })
 
-index.afterEach(to => {
+index.afterEach(() => {
 
 })