Skip to content

Commit

Permalink
Merge branch 'master' of github.com:szuprefix/vue-django
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Nov 18, 2019
2 parents e8c3871 + c96a94e commit 813b73c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
20 changes: 10 additions & 10 deletions src/components/layout/ViewTabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
</el-tabs>
</template>
<script>
import server_response from '../../mixins/server_response'
import serverResponse from '../../mixins/server_response'
export default{
mixins: [server_response],
mixins: [serverResponse],
data () {
return {
tabs: [],
curTab: '/'
}
},
created (){
created () {
this.curTab = this.$route.path
this.changeRoute(this.$route)
this.$store.state.bus.$on("tab-destroy", this.tabRemove)
this.$store.state.bus.$on('tab-destroy', this.tabRemove)
},
components: {},
methods: {
ellipsis(s){
ellipsis (s) {
return s.length > 19 ? `${s.substr(0, 16)}...` : s
},
clearTabs(){
clearTabs () {
this.curTab = '/'
this.tabs = [this.tabs.find((tab) => tab.name == this.curTab)]
},
tabRemove(name){
tabRemove (name) {
let targetName = name
if (this.curTab === targetName) {
this.tabs.forEach((tab, index) => {
Expand All @@ -53,7 +53,7 @@
}
this.curTab = tab.name
},
changeRoute(newVal, oldVal){
changeRoute (newVal, oldVal) {
// console.log(newVal)
// console.log(oldVal)
let to = newVal
Expand All @@ -78,12 +78,12 @@
}
},
watch: {
curTab(){
curTab () {
if (this.$route.path !== this.curTab) {
this.$router.push(this.resolveRoutePath(this.curTab))
}
},
$route(newVal, oldVal){
$route (newVal, oldVal) {
this.changeRoute(newVal, oldVal)
}
},
Expand Down
40 changes: 20 additions & 20 deletions src/components/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import {Register} from '../../utils/app_model'
import axios from '../../configs/axios'
// import store from '../store'
export function joinErrors(errors) {
export function joinErrors (errors) {
let es = {}
for (let n in errors) {
es[n] = errors[n].join("")
es[n] = errors[n].join('')
}
return es
}
Expand Down Expand Up @@ -43,7 +43,7 @@ export default function (appModel, defaults, eventor) {
})
}
},
loadOptions(){
loadOptions () {
if (this.config.rest_options) {
this.cacheOptions(this.config.rest_options)
return Promise.resolve(this.config.rest_options)
Expand All @@ -54,14 +54,14 @@ export default function (appModel, defaults, eventor) {
return data
})
},
cacheOptions(options){
cacheOptions (options) {
this.options = Object.assign({}, this.options, options)
this.fieldConfigs = Object.assign({}, this.fieldConfigs, options.actions.LIST, options.actions.POST)
Object.keys(this.fieldConfigs).forEach((a) => {
this.fieldConfigs[a].name = a
})
},
emptyDataFromOptions(m){
emptyDataFromOptions (m) {
let dvs = {}
Object.assign(dvs, this.defaults)
let r = {}
Expand All @@ -72,16 +72,16 @@ export default function (appModel, defaults, eventor) {
})
return r
},
load() {
return axios.all([this.loadData(), this.loadOptions()]).then(axios.spread((data, rest_options) => {
load () {
return axios.all([this.loadData(), this.loadOptions()]).then(axios.spread((data, restOptions) => {
if (!this.id) {
data = this.emptyDataFromOptions(rest_options.actions.POST)
data = this.emptyDataFromOptions(restOptions.actions.POST)
}
this.data = Object.assign({}, this.data, data)
return [data, rest_options]
return [data, restOptions]
}))
},
save(data){
save (data) {
let d = Object.assign({}, this.defaults, this.data, data)
let promise
if (!this.id) {
Expand All @@ -94,44 +94,44 @@ export default function (appModel, defaults, eventor) {
this.data = Object.assign({}, this.data, data)
this.emitPosted(this.id)
return data
})//.catch((error) => this.onErrors(error))
}) // .catch((error) => this.onErrors(error))
},
removeRelateObject (rel, id) {
this.data[rel] = this.data[rel].filter(a => a !== id)
return this.save()
},
destroy(id){
destroy (id) {
id = id || this.id
return axios.delete(this.getDetailUrl(id)).then(() => {
this.eventor.$emit('model-deleted', {appModel: this.appModel, id})
})
},
emitPosted(id){
emitPosted (id) {
this.eventor.$emit('model-posted', {appModel: this.appModel, id})
},
onErrors(error){
onErrors (error) {
if (error.code === 400) {
this.errors = joinErrors(error.msg)
}
return Promise.reject(error)
},
checkPermission(p, ps){
checkPermission (p, ps) {
if (!ps) {
return false
}
let pn = this.appModel.replace('.', `.${p}_`)
return ps.includes(pn)
},
getListUrl(){
return `/${this.appModel.replace(".", "/")}/`
getListUrl () {
return `/${this.appModel.replace('.', '/')}/`
},
getDetailUrl(id){
getDetailUrl (id) {
let mid = id || this.id
return `${this.getListUrl()}${mid}/`
},
title(){
title () {
return !this.id && `新增${this.config.verbose_name}` || this.data['__str__']
},
}
}
m.init()
return m
Expand Down
23 changes: 11 additions & 12 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,38 @@ const state = {
user: {},
apps,
system_name: '',
logo:require('../assets/logo.png'),
logo: require('../assets/logo.png')
}


var store = new Vuex.Store({
state: state,
mutations: {
setUser(state, payload){
setUser (state, payload) {
// console.log(payload)
state.user = payload
state.bus.$emit("get-user-info", payload)
state.bus.$emit('get-user-info', payload)
},
clearUser(state){
clearUser (state) {
state.user = {}
state.bus.$emit("user-logout")
state.bus.$emit('user-logout')
}
},
actions: {
getUserInfo (context){
return Vue.http.get("/auth/user/current/").then(({data}) => {
context.commit("setUser", data)
getUserInfo (context) {
return Vue.http.get('/auth/user/current/').then(({data}) => {
context.commit('setUser', data)
return data
})
},
logout (context){
logout (context) {
return logout().then((data) => {
context.commit("clearUser")
context.commit('clearUser')
return data
})
}
},
plugins: []
})
// Vue.prototype.$store = Vue.store =store
// store.dispatch("getUserInfo").catch((e) => {console.error(e)})
// store.dispatch('getUserInfo').catch((e) => {console.error(e)})
export default store
16 changes: 8 additions & 8 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import Cookies from 'js-cookie'

const TokenKey = 'access_token'

import axios from 'axios'

export function getToken() {
const TokenKey = 'access_token'

export function getToken () {
return Cookies.get(TokenKey)
}

export function setToken(token) {
export function setToken (token) {
axios.defaults.headers.common['Authorization'] = `Bearer ${token}`
return Cookies.set(TokenKey, token)
}

export function removeToken() {
export function removeToken () {
delete axios.defaults.headers.common['Authorization']
return Cookies.remove(TokenKey)
}

export function login(username, password) {
export function login (username, password) {
axios.post('/auth/user/login/', {username, password}).then(({data}) => {
let token = data.token.access
setToken(token)
})
}

export function logout() {
export function logout () {
return axios.get('/auth/user/logout/').then(({data}) => {
removeToken()
return data
Expand All @@ -37,4 +37,4 @@ export default {
removeToken,
login,
logout
}
}

0 comments on commit 813b73c

Please sign in to comment.