ソースを参照

增加了一些公共的东西

somebody2012 4 年 前
コミット
6df2af5bb6

+ 5 - 1
config/index.js

@@ -10,7 +10,11 @@ module.exports = {
     // Paths
     assetsSubDirectory: 'static',
     assetsPublicPath: '/',
-    proxyTable: {},
+    proxyTable: {
+      "/": {
+        target: "http://localhost:8080"
+      }
+    },
 
     // Various Dev Server settings
     host: 'localhost', // can be overwritten by process.env.HOST

+ 19 - 0
src/common/Result.js

@@ -0,0 +1,19 @@
+
+
+export class Result{
+  status = 0;
+  data = {};
+  msg = "";
+  constructor(options = {}){
+    this.status = options.status || 0;
+    this.data = options.data || {};
+    this.msg = options.msg || "";
+  }
+  static success = options => new Result(Object.assign(options,{status:1}));
+  static fail = options => new Result(Object.assign(options,{status:0}));
+  static build = res => new Result(res);
+  get isSuccess(){
+    return this.status === 1;
+  }
+}
+

+ 18 - 0
src/common/globalMixins.js

@@ -0,0 +1,18 @@
+import {sendRequest} from "./service-utils"
+export const globalPlugin = {
+  install(Vue){
+    Vue.config.productionTip = false;
+    Vue.prototype.sendRequest = sendRequest.bind(Vue.prototype,Vue.prototype);
+  }
+}
+export const globalMixins = {
+  data(){
+    return{
+
+    }
+  },
+  methods:{},
+  mounted(){
+    (window.comps || (window.comps = {}))[this.$options.name || "anonymous"] = this;
+  }
+}

+ 53 - 0
src/common/service-utils.js

@@ -0,0 +1,53 @@
+import $ from 'jquery'
+import {Result} from "./Result.js"
+
+export const sendRequest = (ins,url,params,showLoading=true,showError=true) => {
+  return new Promise((resolve,reject) => {
+    let loadingIns = null;
+    if(showLoading){
+      loadingIns = ins.$loading();
+    }
+    let o = {
+      type: "get",
+      data:params,
+      dataType:"JSON",
+      xhrFields: {withCredentials: true},
+      contentType:"application/json; charset=utf-8",
+      crossDomain: true,
+      url:url,
+      success:function(resp){
+        if(resp.result === 1){
+          resolve(Result.success({data:resp}))
+        }else{
+          if(showError) ins.$notify.error({title: '系统出错',message:resp.msg,offset: 50});
+          resolve(Result.fail({data:resp}))
+        }
+      },
+      error: function(resp){
+        if(showError) ins.$notify.error({title: '系统出错',offset: 50});
+        resolve(Result.fail({msg:"系统出错",data:resp}))
+      },
+      complete:function(resp){
+        if(showLoading){
+          setTimeout(() => {
+            loadingIns.close();
+          },1000)
+        }
+      }
+    };
+    try{
+      $.ajax(o);
+    }catch(e){
+      if(showLoading){
+        setTimeout(() => {
+          loadingIns.close();
+        },1000)
+      }
+      if(showError) ins.$notify.error({title: '系统出错',offset: 50});
+      resolve(Result.fail({msg:"程序出错"}))
+    }finally{
+
+    }
+
+  });
+}

+ 3 - 0
src/common/serviceNames.js

@@ -0,0 +1,3 @@
+export default {
+  "xxx":"/api/a/c",//接口说明
+}

+ 4 - 1
src/main.js

@@ -7,9 +7,12 @@ import ElementUI from 'element-ui'
 import 'element-ui/lib/theme-chalk/index.css'
 import 'bootstrap3/dist/css/bootstrap.min.css'
 import 'font-awesome/css/font-awesome.css'
+import {globalMixins,globalPlugin} from "./common/globalMixins.js"
+
 
-Vue.config.productionTip = false;
 Vue.use(ElementUI);
+Vue.use(globalPlugin);
+Vue.mixin(globalMixins);
 
 /* eslint-disable no-new */
 new Vue({

+ 13 - 1
src/router/index.js

@@ -14,7 +14,7 @@ import ReportDesign from '@/view/bireport/ReportDesign'
 
 Vue.use(Router)
 
-export default new Router({
+let router = new Router({
   routes: [
     {
       path: '/',
@@ -76,3 +76,15 @@ export default new Router({
     }
   ]
 })
+
+router.beforeEach((to, from, next) => {
+  if(to.path != "/"){
+    // if(!checkIsLogin()){
+    //   next("/")
+    //   return;
+    // }
+  }
+  next();
+})
+
+export default router;

+ 4 - 0
static/test.json

@@ -0,0 +1,4 @@
+{
+  "name":"jack",
+  "age":22
+}