Bladeren bron

登录跳转

gzb
guozhongbo 9 maanden geleden
bovenliggende
commit
4a4688b991
5 gewijzigde bestanden met toevoegingen van 463 en 9 verwijderingen
  1. 1
    1
      src/api/admin.js
  2. 34
    3
      src/store/index.js
  3. 99
    0
      src/utils/store.js
  4. 304
    0
      src/utils/validate.js
  5. 25
    5
      src/views/login/login.vue

+ 1
- 1
src/api/admin.js Bestand weergeven

@@ -3,7 +3,7 @@ import { admin_url } from "@/utils/httpRequest";
3 3
 /**
4 4
  * 1.0.1管理员--账号登录
5 5
  */
6
-export const admin_login = (data) => admin_url("admin/login", data);
6
+export const login = (data) => admin_url("admin/login", data);
7 7
 /**
8 8
  * 1.0.4管理员--生成图片验证码
9 9
  */

+ 34
- 3
src/store/index.js Bestand weergeven

@@ -1,9 +1,40 @@
1 1
 import Vue from "vue";
2 2
 import Vuex from "vuex";
3
+import { login } from "../api/admin";
4
+import { clearStore, setStore } from "../utils/store";
3 5
 Vue.use(Vuex);
4 6
 
5 7
 export default new Vuex.Store({
6
-  state: {},
7
-  mutations: {},
8
-  actions: {}
8
+  state: {
9
+    userInfo: {}
10
+  },
11
+  mutations: {
12
+    // 设置个人信息
13
+    SET_USERIFNO: (state, userInfo) => {
14
+      state.userInfo = userInfo;
15
+      setStore({ name: "xh-control-userInfo", content: userInfo });
16
+    }
17
+  },
18
+  actions: {
19
+    // 登陆
20
+    login_username({ commit }, userInfo) {
21
+      return new Promise((resolve, reject) => {
22
+        login(userInfo).then((res) => {
23
+          if (res.code == 0) {
24
+            const userInfo = res.obj;
25
+            commit("SET_USERIFNO", userInfo);
26
+          }
27
+          resolve(res);
28
+        });
29
+      });
30
+    },
31
+    // 登出
32
+    loginOut({ commit }) {
33
+      return new Promise((resolve, reject) => {
34
+        commit("SET_USERIFNO", []);
35
+        clearStore();
36
+        resolve("请重新登录!");
37
+      });
38
+    }
39
+  }
9 40
 });

+ 99
- 0
src/utils/store.js Bestand weergeven

@@ -0,0 +1,99 @@
1
+import { validatenull } from "@/utils/validate";
2
+
3
+/**
4
+ * 存储localStorage
5
+ */
6
+export const setStore = (params = {}) => {
7
+  let { name, content, type } = params;
8
+  let obj = {
9
+    dataType: typeof content,
10
+    content: content,
11
+    type: type,
12
+    datetime: new Date().getTime()
13
+  };
14
+  if (type) window.sessionStorage.setItem(name, JSON.stringify(obj));
15
+  else window.localStorage.setItem(name, JSON.stringify(obj));
16
+};
17
+
18
+/**
19
+ * 获取localStorage
20
+ */
21
+export const getStore = (params = {}) => {
22
+  let { name, debug } = params;
23
+  let obj = {};
24
+  let content;
25
+  obj = window.sessionStorage.getItem(name);
26
+  if (validatenull(obj)) obj = window.localStorage.getItem(name);
27
+  if (validatenull(obj)) return;
28
+  try {
29
+    obj = JSON.parse(obj);
30
+  } catch {
31
+    return obj;
32
+  }
33
+  if (debug) {
34
+    return obj;
35
+  }
36
+  if (obj.dataType === "string") {
37
+    content = obj.content;
38
+  } else if (obj.dataType === "number") {
39
+    content = Number(obj.content);
40
+  } else if (obj.dataType === "boolean") {
41
+    content = this.eval(obj.content);
42
+  } else if (obj.dataType === "object") {
43
+    content = obj.content;
44
+  }
45
+  return content;
46
+};
47
+
48
+/**
49
+ * 删除localStorage
50
+ */
51
+export const removeStore = (params = {}) => {
52
+  let { name, type } = params;
53
+  if (type) {
54
+    window.sessionStorage.removeItem(name);
55
+  } else {
56
+    window.localStorage.removeItem(name);
57
+  }
58
+};
59
+
60
+/**
61
+ * 获取全部localStorage
62
+ */
63
+export const getAllStore = (params = {}) => {
64
+  let list = [];
65
+  let { type } = params;
66
+  if (type) {
67
+    for (let i = 0; i <= window.sessionStorage.length; i++) {
68
+      list.push({
69
+        name: window.sessionStorage.key(i),
70
+        content: getStore({
71
+          name: window.sessionStorage.key(i),
72
+          type: "session"
73
+        })
74
+      });
75
+    }
76
+  } else {
77
+    for (let i = 0; i <= window.localStorage.length; i++) {
78
+      list.push({
79
+        name: window.localStorage.key(i),
80
+        content: getStore({
81
+          name: window.localStorage.key(i)
82
+        })
83
+      });
84
+    }
85
+  }
86
+  return list;
87
+};
88
+
89
+/**
90
+ * 清空全部localStorage
91
+ */
92
+export const clearStore = (params = {}) => {
93
+  let { type } = params;
94
+  if (type) {
95
+    window.sessionStorage.clear();
96
+  } else {
97
+    window.localStorage.clear();
98
+  }
99
+};

+ 304
- 0
src/utils/validate.js Bestand weergeven

@@ -0,0 +1,304 @@
1
+/**
2
+ * Created by jiachenpan on 16/11/18.
3
+ */
4
+
5
+export function isvalidUsername(str) {
6
+  const validMap = ["admin", "editor"];
7
+  return validMap.indexOf(str.trim()) >= 0;
8
+}
9
+
10
+/* 合法uri */
11
+export function validateURL(textval) {
12
+  const urlregex =
13
+    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
14
+  return urlregex.test(textval);
15
+}
16
+/**
17
+ * 邮箱
18
+ * @param {*} s
19
+ */
20
+export function isEmail(s) {
21
+  return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(
22
+    s
23
+  );
24
+}
25
+
26
+/**
27
+ * 手机号码
28
+ * @param {*} s
29
+ */
30
+export function isMobile(s) {
31
+  return /^1[0-9]{10}$/.test(s);
32
+}
33
+
34
+/**
35
+ * 电话号码
36
+ * @param {*} s
37
+ */
38
+export function isPhone(s) {
39
+  return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s);
40
+}
41
+
42
+/**
43
+ * URL地址
44
+ * @param {*} s
45
+ */
46
+export function isURL(s) {
47
+  return /^http[s]?:\/\/.*/.test(s);
48
+}
49
+
50
+/* 小写字母 */
51
+export function validateLowerCase(str) {
52
+  const reg = /^[a-z]+$/;
53
+  return reg.test(str);
54
+}
55
+
56
+/* 大写字母 */
57
+export function validateUpperCase(str) {
58
+  const reg = /^[A-Z]+$/;
59
+  return reg.test(str);
60
+}
61
+
62
+/* 大小写字母 */
63
+export function validatAlphabets(str) {
64
+  const reg = /^[A-Za-z]+$/;
65
+  return reg.test(str);
66
+}
67
+/* 验证pad还是pc */
68
+export const vaildatePc = function () {
69
+  const userAgentInfo = navigator.userAgent;
70
+  const Agents = [
71
+    "Android",
72
+    "iPhone",
73
+    "SymbianOS",
74
+    "Windows Phone",
75
+    "iPad",
76
+    "iPod"
77
+  ];
78
+  let flag = true;
79
+  for (var v = 0; v < Agents.length; v++) {
80
+    if (userAgentInfo.indexOf(Agents[v]) > 0) {
81
+      flag = false;
82
+      break;
83
+    }
84
+  }
85
+  return flag;
86
+};
87
+/**
88
+ * validate email
89
+ * @param email
90
+ * @returns {boolean}
91
+ */
92
+export function validateEmail(email) {
93
+  const re =
94
+    /^(([^<>()\\[\]\\.,;:\s@"]+(\.[^<>()\\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
95
+  return re.test(email);
96
+}
97
+
98
+/**
99
+ * 判断身份证号码
100
+ */
101
+export function cardid(code) {
102
+  let list = [];
103
+  let result = true;
104
+  let msg = "";
105
+  var city = {
106
+    11: "北京",
107
+    12: "天津",
108
+    13: "河北",
109
+    14: "山西",
110
+    15: "内蒙古",
111
+    21: "辽宁",
112
+    22: "吉林",
113
+    23: "黑龙江 ",
114
+    31: "上海",
115
+    32: "江苏",
116
+    33: "浙江",
117
+    34: "安徽",
118
+    35: "福建",
119
+    36: "江西",
120
+    37: "山东",
121
+    41: "河南",
122
+    42: "湖北 ",
123
+    43: "湖南",
124
+    44: "广东",
125
+    45: "广西",
126
+    46: "海南",
127
+    50: "重庆",
128
+    51: "四川",
129
+    52: "贵州",
130
+    53: "云南",
131
+    54: "西藏 ",
132
+    61: "陕西",
133
+    62: "甘肃",
134
+    63: "青海",
135
+    64: "宁夏",
136
+    65: "新疆",
137
+    71: "台湾",
138
+    81: "香港",
139
+    82: "澳门",
140
+    91: "国外 "
141
+  };
142
+  if (!validatenull(code)) {
143
+    if (code.length === 18) {
144
+      if (!code || !/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(code)) {
145
+        msg = "证件号码格式错误";
146
+      } else if (!city[code.substr(0, 2)]) {
147
+        msg = "地址编码错误";
148
+      } else {
149
+        // 18位身份证需要验证最后一位校验位
150
+        code = code.split("");
151
+        // ∑(ai×Wi)(mod 11)
152
+        // 加权因子
153
+        var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
154
+        // 校验位
155
+        var parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2, "x"];
156
+        var sum = 0;
157
+        var ai = 0;
158
+        var wi = 0;
159
+        for (var i = 0; i < 17; i++) {
160
+          ai = code[i];
161
+          wi = factor[i];
162
+          sum += ai * wi;
163
+        }
164
+        if (parity[sum % 11] !== code[17]) {
165
+          msg = "证件号码校验位错误";
166
+        } else {
167
+          result = false;
168
+        }
169
+      }
170
+    } else {
171
+      msg = "证件号码长度不为18位";
172
+    }
173
+  } else {
174
+    msg = "证件号码不能为空";
175
+  }
176
+  list.push(result);
177
+  list.push(msg);
178
+  return list;
179
+}
180
+/**
181
+ * 判断手机号码是否正确
182
+ */
183
+export function isvalidatemobile(phone) {
184
+  let list = [];
185
+  let result = true;
186
+  let msg = "";
187
+  var isPhone = /^0\d{2,3}-?\d{7,8}$/;
188
+  // 增加134 减少|1349[0-9]{7},增加181,增加145,增加17[678]
189
+  if (!validatenull(phone)) {
190
+    if (phone.length === 11) {
191
+      if (isPhone.test(phone)) {
192
+        msg = "手机号码格式不正确";
193
+      } else {
194
+        result = false;
195
+      }
196
+    } else {
197
+      msg = "手机号码格式不正确";
198
+    }
199
+  } else {
200
+    msg = "手机号码不能为空";
201
+  }
202
+  list.push(result);
203
+  list.push(msg);
204
+  return list;
205
+}
206
+/**
207
+ * 判断手机号码是否正确 不是必填
208
+ */
209
+export function check_phone(phone) {
210
+  let list = [];
211
+  let result = true;
212
+  let msg = "";
213
+  var isPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
214
+  if (!validatenull(phone)) {
215
+    if (phone.length === 11) {
216
+      if (!isPhone.test(phone)) {
217
+        msg = "手机号码格式不正确";
218
+      } else {
219
+        result = false;
220
+      }
221
+    } else {
222
+      msg = "手机号码格式不正确";
223
+    }
224
+  } else {
225
+    result = false;
226
+  }
227
+  list.push(result);
228
+  list.push(msg);
229
+  return list;
230
+}
231
+/**
232
+ * 判断姓名是否正确
233
+ */
234
+export function validatename(name) {
235
+  var regName = /^[\u4e00-\u9fa5]{2,4}$/;
236
+  if (!regName.test(name)) return false;
237
+  return true;
238
+}
239
+/**
240
+ * 判断是否为整数
241
+ */
242
+export function validatenum(num, type) {
243
+  let regName = /[^\d.]/g;
244
+  if (type === 1) {
245
+    if (!regName.test(num)) return false;
246
+  } else if (type === 2) {
247
+    regName = /[^\d]/g;
248
+    if (!regName.test(num)) return false;
249
+  }
250
+  return true;
251
+}
252
+/**
253
+ * 判断是否为小数
254
+ */
255
+export function validatenumord(num, type) {
256
+  let regName = /[^\d.]/g;
257
+  if (type === 1) {
258
+    if (!regName.test(num)) return false;
259
+  } else if (type === 2) {
260
+    regName = /[^\d.]/g;
261
+    if (!regName.test(num)) return false;
262
+  }
263
+  return true;
264
+}
265
+/**
266
+ * 判断是否为空
267
+ */
268
+export function validatenull(val) {
269
+  if (typeof val === "boolean") {
270
+    return false;
271
+  }
272
+  if (typeof val === "number") {
273
+    return false;
274
+  }
275
+  if (val instanceof Array) {
276
+    if (val.length === 0) return true;
277
+  } else if (val instanceof Object) {
278
+    if (JSON.stringify(val) === "{}") return true;
279
+  } else {
280
+    if (
281
+      val === "null" ||
282
+      val === null ||
283
+      val === "undefined" ||
284
+      val === undefined ||
285
+      val === ""
286
+    )
287
+      return true;
288
+    return false;
289
+  }
290
+  return false;
291
+}
292
+// 密码必须是6-16位的英文字母、数字组合
293
+export function isLoginPwd(val) {
294
+  const passRegex = new RegExp("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
295
+  return passRegex.test(val);
296
+}
297
+// 修改密码功能改为: 密码必须是8-16位大小写英文字母、数字组合 。
298
+export function isValidPassword(password) {
299
+  const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,16}$/;
300
+  return {
301
+    is: reg.test(password),
302
+    msg: "密码必须是8-16位大小写英文字母、数字组合"
303
+  };
304
+}

+ 25
- 5
src/views/login/login.vue Bestand weergeven

@@ -107,7 +107,7 @@ export default {
107 107
     return {
108 108
       code_info: {
109 109
         base64Str: "",
110
-        msgcode: ""
110
+        code: ""
111 111
       },
112 112
       usertype: 1,
113 113
       time1: null,
@@ -205,7 +205,6 @@ export default {
205 205
       img_code({
206 206
         loginname: this.loginForm.loginname
207 207
       }).then((data) => {
208
-        console.log(data);
209 208
         if (data.code === 0) {
210 209
           this.code_info = data.obj;
211 210
         } else {
@@ -238,16 +237,37 @@ export default {
238 237
     //登录
239 238
     login_by_username(name) {
240 239
       let that = this;
240
+      console.log(name);
241 241
       this.$refs[name].validate((valid) => {
242 242
         if (valid) {
243 243
           if (
244
-            this.loginForm.msgcode.toLowerCase() !==
245
-            this.code_info.msgcode.toLowerCase()
244
+            that.loginForm.msgcode.toLowerCase() !==
245
+            that.code_info.code.toLowerCase()
246 246
           ) {
247 247
             this.$Message.error("验证码不正确");
248 248
             return;
249 249
           }
250
-          this.wait_flag = true;
250
+          that.wait_flag = true;
251
+          let loginForm = that.loginForm;
252
+          that.$store.dispatch("login_username", loginForm).then((res) => {
253
+            that.wait_flag = false;
254
+            if (res.code == 0) {
255
+              that.$Message.success(res.msg);
256
+              //1平台管理员2区域管理员3学校管理员
257
+              console.log(res.obj);
258
+              let atype = res.obj.atype;
259
+              atype = 2;
260
+              if (atype === 1) {
261
+              } else if (atype === 2) {
262
+                this.$router.replace({ name: "region" });
263
+              } else if (atype === 3) {
264
+              }
265
+              that.set_userInfo();
266
+            } else {
267
+              that.$Message.error(res.msg);
268
+              that.wait_flag = false;
269
+            }
270
+          });
251 271
         }
252 272
       });
253 273
     },

Laden…
Annuleren
Opslaan