Skip to content

Commit da6e177

Browse files
committed
通过 FileReader读取本地 json数据
1 parent a5bbd2a commit da6e177

File tree

1 file changed

+156
-1
lines changed

1 file changed

+156
-1
lines changed

handsonDoc/DSW015-Element-Admin-编辑登录界面-添加CA认证.md

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,159 @@ lin-jinwei
88

99
Code: [../code/S10-vue-element-admin-edit](../code/S10-vue-element-admin-edit/)
1010

11-
##
11+
### 添加一个字符串输入栏
12+
13+
代码:src\views\login\index.vue
14+
15+
添加组件代码:
16+
17+
```html
18+
<!-- 在此添加 -->
19+
<el-form-item prop="cacert">
20+
<span class="svg-container">
21+
<!-- 在此修改图标 -->
22+
<svg-icon icon-class="guide" />
23+
</span>
24+
<!-- tabindex="1" 表示元素可以被聚焦,即光标点击响应 -->
25+
<el-input ref="cacert" v-model="loginForm.cacertplaceholder="CA-Certification" name="cacert" type="text"
26+
tabindex="1" autocomplete="on" />
27+
</el-form-item>
28+
```
29+
![alt text](image-96.png)
30+
31+
### 设置export default的默认值
32+
设置界面初始化时候输入框内的默认值或者提示语言:
33+
34+
```js
35+
export default {
36+
name: 'Login',
37+
components: { SocialSign },
38+
data() {
39+
const validateUsername = (rule, value, callback) => {
40+
if (!validUsername(value)) {
41+
callback(new Error('Please enter the correct user name'))
42+
} else {
43+
callback()
44+
}
45+
}
46+
47+
// 添加-认证CA证书-初步认证
48+
const validateCacert = (rule, value, callback) => {
49+
if (!validateCacert(value)) {
50+
// 在此处添加-初步认证CA证书的代码
51+
callback(new Error('Please enter the correct CA certification'))
52+
} else {
53+
callback()
54+
}
55+
}
56+
57+
const validatePassword = (rule, value, callback) => {
58+
if (value.length < 6) {
59+
callback(new Error('The password can not be less than 6 digits'))
60+
} else {
61+
callback()
62+
}
63+
}
64+
return {
65+
loginForm: {
66+
username: '用户名-默认值',
67+
cacert: 'CA证书-默认值',
68+
password: '登录密码-默认值',
69+
},
70+
loginRules: {
71+
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
72+
cacert: [{ required: true, trigger: 'blur', validator: validateCacert }],
73+
password: [{ required: true, trigger: 'blur', validator: validatePassword }]
74+
},
75+
passwordType: 'password',
76+
capsTooltip: false,
77+
loading: false,
78+
showDialog: false,
79+
redirect: undefined,
80+
otherQuery: {}
81+
}
82+
},
83+
```
84+
85+
![alt text](image-97.png)
86+
87+
88+
### 编辑认证js
89+
90+
![alt text](image-99.png)
91+
92+
代码:src\utils\validate.js
93+
94+
新增:
95+
```js
96+
export function validCacert(str) {
97+
const valid_map = ['ca']
98+
return valid_map.indexOf(str.trim()) >= 0
99+
}
100+
```
101+
102+
### 编辑登录 index.vue
103+
104+
代码:src\views\login\index.vue
105+
```js
106+
import { validUsername, validCacert} from '@/utils/validate'
107+
```
108+
109+
### 修改handleLogin()函数
110+
111+
```js
112+
handleLogin() {
113+
this.$refs.loginForm.validate(valid => {
114+
if (valid) {
115+
this.loading = true
116+
// 使用store存储数据
117+
this.$store.dispatch('user/login', this.loginForm)
118+
.then(() => {
119+
// 正式启动后的运行代码
120+
this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
121+
this.loading = false
122+
})
123+
.catch(() => {
124+
this.loading = false
125+
})
126+
} else {
127+
console.log('error submit!!')
128+
return false
129+
}
130+
})
131+
},
132+
```
133+
134+
### 通过 FileReader读取本地 json数据
135+
136+
```js
137+
readerData(rawFile) {
138+
this.loading = true
139+
return new Promise((resolve, reject) => {
140+
const reader = new FileReader()
141+
reader.onload = e => {
142+
console.log('reader.onload = e => ========================')
143+
144+
// 获取读取到的文件内容+解析JSON文件
145+
const contents = reader.result
146+
console.log('contents = ' + contents)
147+
console.log('contents.byteLength = ' + contents.byteLength)
148+
const jsonData = String.fromCharCode.apply(null, new Uint8Array(contents))
149+
console.log('jsonData = ' + jsonData)
150+
console.log('typeof jsonData = ' + typeof jsonData);
151+
152+
// ========================
153+
// 继续处理读取数据
154+
// ========================
155+
156+
this.loading = false
157+
resolve()
158+
}
159+
reader.readAsArrayBuffer(rawFile)
160+
})
161+
},
162+
```
163+
164+
165+
166+

0 commit comments

Comments
 (0)