331 lines
14 KiB
JavaScript
331 lines
14 KiB
JavaScript
import Vue from 'vue'
|
|
const mode = 'dev'
|
|
var paths = [
|
|
{name: 'operation', url: 'https://oprtapi.bigdatatech.vn/' },
|
|
{name: 'local', url: 'http://127.0.0.1:8000/' },
|
|
{name: 'dev', url: 'https://utopiaapi.dev247.net/' },
|
|
{name: 'prod', url: 'https://utopiaapi.dev247.net/' }
|
|
]
|
|
const path = paths.find(v=>v.name===mode).url
|
|
const apis = [
|
|
{name: 'upload' , url: 'upload/', params: {}},
|
|
{name: 'image' , url: 'data/Image/', url_detail: 'data-detail/Image/', params: {}},
|
|
{name: 'file' , url: 'data/File/', url_detail: 'data-detail/File/', params: {}},
|
|
{name: 'user', url: 'data/User/', url_detail: 'data-detail/User/', params: {values: 'id,phone_verified,email_verified,create_time__date,username,password,avatar,fullname,display_name,type,blocked,block_reason,blocked_by,last_login,auth_method__code,auth_method__name,auth_method,auth_status,auth_status__code,auth_status__name,register_method,phone,create_time,update_time'}},
|
|
{name: 'blockreason', commit: 'updateBlockReason', url: 'data/Block_Reason/', url_detail: 'data-detail/Block_Reason/', params: {page: -1}},
|
|
{name: 'authstatus', commit: 'updateAuthStatus', url: 'data/Auth_Status/', url_detail: 'data-detail/Auth_Status/', params: {page: -1}},
|
|
{name: 'authmethod', commit: 'updateAuthMethod', url: 'data/Auth_Method/', url_detail: 'data-detail/Auth_Method/', params: {page: -1}},
|
|
{name: 'usertype', commit: 'updateUserType', url: 'data/User_Type/', url_detail: 'data-detail/User_Type/', params: {}},
|
|
{name: 'registermethod', commit: 'updateRegisterMethod', url: 'data/Register_Method/', url_detail: 'data-detail/Register_Method/', params: {page: -1}},
|
|
{name: 'langchoice', commit: 'updateLangChoice', url: 'data/Lang_Choice/', url_detail: 'data-detail/Lang_Choice/', params: {}},
|
|
{name: 'userauth', url: 'data/User_Auth/', url_detail: 'data-detail/User_Auth/', params: {}},
|
|
{name: 'accountrecovery', url: 'data/Account_Recovery/', url_detail: 'data-detail/Account_Recovery/', params: {}},
|
|
{name: 'login', url: 'login/', params: {values: 'id,username,password,avatar,fullname,display_name,type,type__code,type__name,blocked,block_reason,block_reason__code,block_reason__name,blocked_by,last_login,auth_method,auth_method__code,auth_method__name,auth_status,auth_status__code,auth_status__name,register_method,register_method__code,register_method__name,create_time,update_time'}},
|
|
{name: 'authtoken', url: 'auth-token/', params: {}},
|
|
|
|
{name: 'emailsetup', url: 'data/Email_Setup/', url_detail: 'data-detail/Email_Setup/', params: {sort: '-id'}},
|
|
{name: 'emailsent', url: 'data/Email_Sent/', url_detail: 'data-detail/Email_Sent/', params: {values: 'id,receiver,content,content__sender__email,content__subject,content__content,status__code,status,status__name,create_time', sort: '-id'}},
|
|
{name: 'sendemail', url: 'send-email/', path: 'operation', params: {}},
|
|
{name: 'token', url: 'data/Token/', url_detail: 'data-detail/Token', params: {filter: {expiry: 0}}},
|
|
{name: 'common', commit: 'updateCommon', url: 'data/Common/', url_detail: 'data-detail/Common/', params: {sort: 'index'}},
|
|
{name: 'sex', url: 'data/Sex/', url_detail: 'data-detail/Sex/', params: {}},
|
|
{name: 'downloadfile', url: 'download-file/', params: {}},
|
|
{name: 'download', url: 'download/', params: {}},
|
|
{name: 'gethash', url: 'get-hash/', params: {}},
|
|
{name: 'userapps', url: 'data/User_Apps/', url_detail: 'data-detail/User_Apps/', params: {}}
|
|
]
|
|
|
|
Vue.use( {
|
|
install(Vue){
|
|
Vue.prototype.$path = function(name) {
|
|
return name? paths.find(v=>v.name===name).url : path
|
|
}
|
|
|
|
Vue.prototype.$findapi = function(name) {
|
|
const result = Array.isArray(name)? apis.filter(v=>name.findIndex(x=>v.name===x)>=0) : apis.find(v=>v.name===name)
|
|
return this.$copy(result)
|
|
}
|
|
|
|
Vue.prototype.$readyapi = function(list) {
|
|
var array = []
|
|
list.forEach(element => {
|
|
let found = apis.find(v=>v.name===element)
|
|
if(found) {
|
|
let ele = JSON.parse(JSON.stringify(found))
|
|
ele.ready = this.$store.state[element]? true : false
|
|
array.push(ele)
|
|
}
|
|
})
|
|
return array
|
|
}
|
|
|
|
// get data
|
|
Vue.prototype.$getapi = async function(list) {
|
|
try {
|
|
let arr = list.map(v => {
|
|
let found = apis.find(v=>v.name===v.name)
|
|
let url = (v.path? paths.find(x=>x.name===v.path).url : path) + (v.url? v.url : found.url)
|
|
let params = v.params? v.params : (found.params===undefined? {} : found.params)
|
|
params.login = this.$store.state.login? this.$store.state.login.id : undefined
|
|
return {url: url, params: params}
|
|
})
|
|
let data = await Promise.all(arr.map(v=>this.$axios.get(v.url, {params: v.params})))
|
|
data.map((v,i) => {
|
|
list[i].data = v.data
|
|
if(list[i].commit) {
|
|
let payload = {}
|
|
payload[list[i].name] = v.data.rows? v.data.rows : v.data
|
|
this.$store.commit(list[i].commit, payload)
|
|
}
|
|
})
|
|
return list
|
|
} catch(err) {
|
|
console.log(err)
|
|
return 'error'
|
|
}
|
|
}
|
|
|
|
// insert data
|
|
Vue.prototype.$insertapi = async function(name, data, values) {
|
|
try {
|
|
let found = this.$findapi(name)
|
|
let curpath = found.path? paths.find(x=>x.name===found.path).url : path
|
|
var rs
|
|
if(!Array.isArray(data))
|
|
rs = await this.$axios.post(`${curpath}${found.url}`, data, {params: {values: values}})
|
|
else {
|
|
let params = {action: 'import', values: values}
|
|
rs = await this.$axios.post(`${curpath}import-data/${found.url.substring(5, found.url.length-1)}/`, data, {params: params})
|
|
}
|
|
|
|
// update store
|
|
if(found.commit) {
|
|
if(this.$store.state[found.name]) {
|
|
let copy = JSON.parse(JSON.stringify(this.$store.state[found.name]))
|
|
let rows = Array.isArray(rs.data)? rs.data : [rs.data]
|
|
rows.map(v=>{
|
|
if(v.id && !v.error) {
|
|
let idx = copy.findIndex(x=>x.id===v.id)
|
|
if(idx>=0) copy[idx] = v
|
|
else copy.push(v)
|
|
}
|
|
})
|
|
this.$store.commit('updateStore', {name: found.name, data: copy})
|
|
}
|
|
}
|
|
return rs.data
|
|
} catch(err) {
|
|
console.log(err)
|
|
return 'error'
|
|
}
|
|
}
|
|
|
|
// update api
|
|
Vue.prototype.$updateapi = async function(name, data, values) {
|
|
try {
|
|
let found = this.$findapi(name)
|
|
let rs = await this.$axios.put(`${path}${found.url_detail}${data.id}/`, data, {params: {values: values? values : found.params.values}})
|
|
if(found.commit) {
|
|
let index = this.$store.state[found.name]? this.$store.state[found.name].findIndex(v=>v.id===rs.data.id) : -1
|
|
if(index>=0) {
|
|
var copy = JSON.parse(JSON.stringify(this.$store.state[found.name]))
|
|
if(Array.isArray(rs.data)===false) Vue.set(copy, index, rs.data)
|
|
else {
|
|
rs.data.forEach(v => {
|
|
let index = copy.findIndex(v=>v.id===v.id)
|
|
if(index>=0) Vue.set(copy, index, v)
|
|
})
|
|
}
|
|
this.$store.commit('updateStore', {name: found.name, data: copy})
|
|
}
|
|
}
|
|
return rs.data
|
|
} catch(err) {
|
|
console.log(err)
|
|
return 'error'
|
|
}
|
|
}
|
|
|
|
// delete data
|
|
Vue.prototype.$deleteapi = async function(name, id) {
|
|
try {
|
|
let found = this.$findapi(name)
|
|
var rs
|
|
if(!Array.isArray(id))
|
|
rs = await this.$axios.delete(`${path}${found.url_detail}${id}`)
|
|
else {
|
|
let params = {action: 'delete'}
|
|
rs = await this.$axios.post(`${path}import-data/${found.url.substring(5,found.url.length-1)}/`, id, {params: params})
|
|
}
|
|
if(found.commit) {
|
|
let copy = JSON.parse(JSON.stringify(this.$store.state[found.name]))
|
|
if(!Array.isArray(id)) {
|
|
let index = copy.findIndex(v=>v.id===id)
|
|
if(index>=0) this.$delete(copy, index)
|
|
} else {
|
|
rs.data.forEach(element => {
|
|
let index = copy.findIndex(v=>v.id===element.id)
|
|
if(index>=0) this.$delete(copy, index)
|
|
})
|
|
}
|
|
this.$store.commit('updateStore', {name: found.name, data: copy})
|
|
}
|
|
return rs.data
|
|
} catch(err) {
|
|
console.log(err)
|
|
return 'error'
|
|
}
|
|
}
|
|
|
|
// insert row
|
|
Vue.prototype.$insertrow = async function(name, data, values, pagename) {
|
|
let result = await this.$insertapi(name, data, values)
|
|
if(result==='error') return
|
|
let arr = Array.isArray(result)? result : [result]
|
|
let copy = this.$copy(this.$store.state[pagename].data)
|
|
arr.map(x=>{
|
|
let index = copy.findIndex(v=>v.id===x.id)
|
|
index>=0? copy[index] = x : copy.unshift(x)
|
|
})
|
|
this.$store.commit('updateState', {name: pagename, key: 'data', data: copy})
|
|
let pagedata = this.$store.state[pagename]
|
|
if(pagedata.filters? pagedata.filters.length>0 : false) {
|
|
this.$store.commit('updateState', {name: pagename, key: 'filterby', data: this.$copy(pagedata.filters)})
|
|
}
|
|
}
|
|
|
|
// update row
|
|
Vue.prototype.$updaterow = async function(name, data, values, pagename) {
|
|
let result = await this.$updateapi(name, data, values)
|
|
if(result==='error') return
|
|
let arr = Array.isArray(result)? result : [result]
|
|
let copy = this.$copy(this.$store.state[pagename].data)
|
|
arr.map(x=>{
|
|
let index = copy.findIndex(v=>v.id===x.id)
|
|
index>=0? copy[index] = x : copy.unshift(x)
|
|
})
|
|
this.$store.commit('updateState', {name: pagename, key: 'data', data: copy})
|
|
let pagedata = this.$store.state[pagename]
|
|
if(pagedata.filters? pagedata.filters.length>0 : false) {
|
|
this.$store.commit('updateState', {name: pagename, key: 'filterby', data: this.$copy(pagedata.filters)})
|
|
}
|
|
}
|
|
|
|
// delete row
|
|
Vue.prototype.$deleterow = async function(name, id, pagename, ask) {
|
|
let self = this
|
|
var remove = async function() {
|
|
let result = await self.$deleteapi(name, id)
|
|
if(result==='error') return
|
|
let arr = Array.isArray(id)? id : [id]
|
|
let copy = self.$copy(self.$store.state[pagename].data)
|
|
arr.map(x=>{
|
|
let index = copy.findIndex(v=>v.id===x)
|
|
index>=0? self.$delete(copy,index) : false
|
|
})
|
|
self.$store.commit('updateState', {name: pagename, key: 'data', data: copy})
|
|
let pagedata = this.$store.state[pagename]
|
|
if(pagedata.filters? pagedata.filters.length>0 : false) {
|
|
this.$store.commit('updateState', {name: pagename, key: 'filterby', data: this.$copy(pagedata.filters)})
|
|
}
|
|
}
|
|
|
|
// ask confirm
|
|
if(ask) {
|
|
this.$buefy.dialog.confirm({
|
|
message: 'Bạn muốn xóa bản ghi: ' + id,
|
|
onConfirm: () => remove()
|
|
})
|
|
} else remove()
|
|
}
|
|
|
|
// update page
|
|
Vue.prototype.$updatepage = function(pagename, row, action) {
|
|
let pagedata = this.$store.state[pagename]
|
|
let copy = this.$copy(pagedata.data)
|
|
let idx = copy.findIndex(v=>v.id===row.id)
|
|
if(action==='delete') this.$delete(copy, idx)
|
|
else if(action==='insert') copy.unshift(row)
|
|
else copy[idx] = row
|
|
this.$store.commit('updateState', {name: pagename, key: 'data', data: copy})
|
|
if(pagedata.filters? pagedata.filters.length>0 : false) {
|
|
this.$store.commit('updateState', {name: pagename, key: 'filterby', data: this.$copy(pagedata.filters)})
|
|
}
|
|
}
|
|
|
|
Vue.prototype.$getdata = async function(name, filter, params, first) {
|
|
let found = this.$findapi(name)
|
|
if(params) found.params = params
|
|
else if(filter) found.params.filter = filter
|
|
let rs = await this.$getapi([found])
|
|
return first? (rs[0].data.rows.length>0? rs[0].data.rows[0] : undefined) : rs[0].data.rows
|
|
}
|
|
|
|
Vue.prototype.$getpage = function(showFilter) {
|
|
return {data: [], fields: [], filters: [], update: undefined, action: undefined, filterby: undefined, api: {full_data: true}, origin_api: {full_data: true},
|
|
tablesetting: undefined, setting: undefined, tabfield: true, setpage: {}, showFilter: this.$empty(showFilter)? true : showFilter}
|
|
}
|
|
|
|
Vue.prototype.$setpage = function(pagename, row, api) {
|
|
if(!this.$store.state[pagename])return
|
|
let json = row.detail
|
|
let fields = this.$updateSeriesFields(json.fields)
|
|
this.$store.commit('updateState', {name: pagename, key: 'fields', data: fields})
|
|
this.$store.commit('updateState', {name: pagename, key: 'setting', data: this.$copy(row)})
|
|
if(json.filters) this.$store.commit('updateState', {name: pagename, key: 'filters', data: this.$copy(json.filters)})
|
|
if(json.tablesetting) this.$store.commit('updateState', {name: pagename, key: 'tablesetting', data: json.tablesetting})
|
|
if(api) {
|
|
let copy = this.$copy(api)
|
|
delete copy.data
|
|
copy.full_data = api.data.full_data
|
|
copy.total_rows = api.data.total_rows
|
|
this.$store.commit('updateState', {name: pagename, key: 'api', data: copy})
|
|
this.$store.commit('updateState', {name: pagename, key: 'origin_api', data: copy})
|
|
}
|
|
}
|
|
|
|
Vue.prototype.$findpage = function(arr) {
|
|
var copy = this.$copy(this.$store.state.pagetrack)
|
|
var doFind = function() {
|
|
let found = undefined
|
|
for (let i=1; i<=30; i++) {
|
|
let name = `pagedata${i}`
|
|
if(!copy[name]) {
|
|
found = name
|
|
copy[name] = true
|
|
break;
|
|
}
|
|
}
|
|
if(!found) console.log('pagename not found')
|
|
return found
|
|
}
|
|
let result
|
|
if(arr) {
|
|
result = []
|
|
arr.map(v=>{ result.push({name: v, value: doFind()})})
|
|
} else {
|
|
result = doFind(copy)
|
|
}
|
|
this.$store.commit('updateStore', {name: 'pagetrack', data: copy})
|
|
return result
|
|
}
|
|
|
|
Vue.prototype.$clearpage = function(pagename) {
|
|
if(!pagename) return
|
|
if(pagename==='reset') return this.$store.commit('updateStore', {name: 'pagetrack', data: {}})
|
|
let copy = this.$copy(this.$store.state.pagetrack)
|
|
let arr = Array.isArray(pagename)? pagename : [pagename]
|
|
arr.map(v=>{
|
|
copy[v] = false
|
|
this.$store.commit('updateStore', {name: v, data: undefined})
|
|
})
|
|
this.$store.commit('updateStore', {name: 'pagetrack', data: copy})
|
|
}
|
|
|
|
Vue.prototype.$updatepath = function(val) {
|
|
path = `https://${val}/`
|
|
}
|
|
}
|
|
})
|
|
|