修复windows下无法导入csv问题;

生成和导入用户逻辑调整
This commit is contained in:
zema1 2017-11-26 09:07:25 +08:00
parent 339dca2160
commit 9cc9c02fb6
5 changed files with 48 additions and 85 deletions

View File

@ -25,7 +25,7 @@ module.exports = {
},
plugins: [
new webpack.DefinePlugin({
'process.env': config.dev.env
'process.env': config.build.env
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/),

View File

@ -29,7 +29,7 @@ Vue.prototype.$error = (msg) => {
Vue.prototype.$message({'message': msg, 'type': 'error'})
}
Vue.prototype.$alert = (msg) => {
Vue.prototype.$warning = (msg) => {
Vue.prototype.$message({'message': msg, 'type': 'warning'})
}

View File

@ -112,7 +112,7 @@
this.smtp = res.data.data
} else {
this.init = true
this.$alert('Please setup SMTP config at first')
this.$warning('Please setup SMTP config at first')
}
})
api.getWebsiteConfig().then(res => {

View File

@ -97,10 +97,12 @@
<div class="panel-options">
<el-button type="primary" size="small"
icon="el-icon-fa-upload"
@click="handleUsersUpload">Import All</el-button>
@click="handleUsersUpload">Import All
</el-button>
<el-button type="warning" size="small"
icon="el-icon-fa-undo"
@click="handleResetData">Reset Data</el-button>
@click="handleResetData">Reset Data
</el-button>
<el-pagination
class="page"
layout="prev, pager, next"
@ -122,9 +124,9 @@
<el-form-item label="Start Number" prop="number_from" required>
<el-input-number v-model="formGenerateUser.number_from" style="width: 100%"></el-input-number>
</el-form-item>
<el-form-item label="Default Email" prop="default_email" required>
<el-input v-model="formGenerateUser.default_email"
placeholder="Default Email"></el-input>
<el-form-item label="Password Length" prop="password_length" required>
<el-input v-model="formGenerateUser.password_length"
placeholder="Password Length"></el-input>
</el-form-item>
</el-col>
@ -135,21 +137,14 @@
<el-form-item label="End Number" prop="number_to" required>
<el-input-number v-model="formGenerateUser.number_to" style="width: 100%"></el-input-number>
</el-form-item>
<el-form-item label="Password Length" prop="password_length" required>
<el-input v-model="formGenerateUser.password_length"
placeholder="Password Length"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-form-item>
<el-button type="primary" @click="generateUser" icon="el-icon-fa-users" :loading="loadingGenerate">Generate
</el-button>
<el-button v-if="file_id" @click="downloadUserExcel" icon="el-icon-fa-download">Download The Excel</el-button>
<span class="userPreview" v-if="!file_id &&
formGenerateUser.number_from &&
formGenerateUser.number_to &&
formGenerateUser.number_from <= formGenerateUser.number_to">
<span class="userPreview" v-if="formGenerateUser.number_from && formGenerateUser.number_to &&
formGenerateUser.number_from <= formGenerateUser.number_to">
The usernames will be {{formGenerateUser.prefix + formGenerateUser.number_from + formGenerateUser.suffix}},
<span v-if="formGenerateUser.number_from + 1 < formGenerateUser.number_to">
{{formGenerateUser.prefix + (formGenerateUser.number_from + 1) + formGenerateUser.suffix + '...'}}
@ -270,13 +265,11 @@
//
currentPage: 0,
selectedUsers: [],
file_id: '',
formGenerateUser: {
prefix: '',
suffix: '',
number_from: 0,
number_to: 0,
default_email: '',
password_length: 8
}
}
@ -344,39 +337,17 @@
let data = Object.assign({}, this.formGenerateUser)
api.generateUser(data).then(res => {
this.loadingGenerate = false
let data = res.data.data
this.file_id = data.file_id
this.$alert(data.created_count + ' users have been created')
if (data.get_count > 0) {
this.$notify({
title: 'Warning',
type: 'warning',
message: data.get_count + ' users were previously created whose password has been set to random string',
duration: 0
})
}
let url = '/admin/generate_user?file_id=' + res.data.data.fileID
utils.downloadFile(url).then(() => {
this.$alert('All users created successfully, the users sheets have downloaded to your disk', 'Notice')
})
this.getUserList(1)
}).catch(() => {
this.loadingGenerate = false
})
})
},
downloadUserExcel () {
this.$msgbox({
title: 'Download The File',
message: 'The users sheets can only be downloaded once, please save it carefully.',
type: 'warning'
}).then(() => {
let url = '/admin/generate_user?file_id=' + this.file_id
utils.downloadFile(url)
this.file_id = ''
})
},
handleUsersCSV (file) {
if (file.type !== 'text/csv') {
this.$error('Only support csv file!')
return
}
papa.parse(file, {
complete: (results) => {
let data = results.data.filter(user => {
@ -384,7 +355,7 @@
})
let delta = results.data.length - data.length
if (delta > 0) {
this.$alert(delta + ' users have been filtered due to empty value')
this.$warning(delta + ' users have been filtered due to empty value')
}
this.uploadUsersCurrentPage = 1
this.uploadUsers = data
@ -397,23 +368,9 @@
},
handleUsersUpload () {
api.importUsers(this.uploadUsers).then(res => {
let data = res.data.data
this.getUserList(1)
let h = this.$createElement
let message = [h('p', {}, data.created_count + ' users created successfully,\n\n')]
if (data.get_count > 0) {
message.push(h('p', {}, data.get_count + ' users were previously created whose password has been updated'))
}
if (data.omitted_count > 0) {
message.push(h('p', {}, data.omitted_count + ' users were omitted'))
}
this.$notify({
type: 'info',
title: 'Import Result',
duration: 0,
customClass: 'notification',
message: h('div', {}, message)
})
this.handleResetData()
}).catch(() => {
})
},
handleResetData () {
@ -452,9 +409,11 @@
color: #555555;
margin-left: 4px;
}
.userPreview {
padding-left: 10px;
}
.notification {
p {
margin: 0;

View File

@ -42,33 +42,37 @@ function breakLongWords (value, length = 16) {
}
function downloadFile (url) {
Vue.prototype.$http.get(url, {responseType: 'blob'}).then(resp => {
let headers = resp.headers
if (headers['content-type'].indexOf('json') !== -1) {
let fr = new window.FileReader()
if (resp.data.error) {
Vue.prototype.$error(resp.data.error)
} else {
Vue.prototype.$error('Invalid file format')
}
fr.onload = (event) => {
let data = JSON.parse(event.target.result)
if (data.error) {
Vue.prototype.$error(data.data)
return new Promise((resolve, reject) => {
Vue.prototype.$http.get(url, {responseType: 'blob'}).then(resp => {
let headers = resp.headers
if (headers['content-type'].indexOf('json') !== -1) {
console.log('2')
let fr = new window.FileReader()
if (resp.data.error) {
Vue.prototype.$error(resp.data.error)
} else {
Vue.prototype.$error('Invalid file format')
}
fr.onload = (event) => {
let data = JSON.parse(event.target.result)
if (data.error) {
Vue.prototype.$error(data.data)
} else {
Vue.prototype.$error('Invalid file format')
}
}
let b = new window.Blob([resp.data], {type: 'application/json'})
fr.readAsText(b)
return
}
let b = new window.Blob([resp.data], {type: 'application/json'})
fr.readAsText(b)
return
}
let link = document.createElement('a')
link.href = window.URL.createObjectURL(new window.Blob([resp.data], {type: headers['content-type']}))
link.download = (headers['content-disposition'] || '').split('filename=')[1]
document.body.appendChild(link)
link.click()
link.remove()
let link = document.createElement('a')
link.href = window.URL.createObjectURL(new window.Blob([resp.data], {type: headers['content-type']}))
link.download = (headers['content-disposition'] || '').split('filename=')[1]
document.body.appendChild(link)
link.click()
link.remove()
resolve()
})
})
}