mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-02-05 01:55:29 +08:00
JeecgBoot 2.3 里程碑版本发布,支持微服务和单体自由切换、提供新行编辑表格JVXETable
This commit is contained in:
170
ant-design-vue-jeecg/src/mixins/JVxeTableMixin.js
Normal file
170
ant-design-vue-jeecg/src/mixins/JVxeTableMixin.js
Normal file
@ -0,0 +1,170 @@
|
||||
import { VALIDATE_FAILED, getRefPromise, validateFormAndTables} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
|
||||
import { httpAction, getAction } from '@/api/manage'
|
||||
|
||||
export const JVxeTableMixin = {
|
||||
data() {
|
||||
return {
|
||||
title: '操作',
|
||||
visible: false,
|
||||
form: this.$form.createForm(this),
|
||||
confirmLoading: false,
|
||||
scrolling: true,
|
||||
model: {},
|
||||
labelCol: {
|
||||
xs: { span: 24 },
|
||||
sm: { span: 6 }
|
||||
},
|
||||
wrapperCol: {
|
||||
xs: { span: 24 },
|
||||
sm: { span: 18 }
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
|
||||
/** 获取所有的JVxeTable实例 */
|
||||
getAllTable() {
|
||||
if (!(this.refKeys instanceof Array)) {
|
||||
throw this.throwNotArray('refKeys')
|
||||
}
|
||||
let values = this.refKeys.map(key => getRefPromise(this, key))
|
||||
return Promise.all(values)
|
||||
},
|
||||
|
||||
/** 遍历所有的JVxeTable实例 */
|
||||
eachAllTable(callback) {
|
||||
// 开始遍历
|
||||
this.getAllTable().then(tables => {
|
||||
tables.forEach((item, index) => {
|
||||
if (typeof callback === 'function') {
|
||||
callback(item, index)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
/** 当点击新增按钮时调用此方法 */
|
||||
add() {
|
||||
if (typeof this.addBefore === 'function') this.addBefore()
|
||||
// 默认新增空数据
|
||||
let rowNum = this.addDefaultRowNum
|
||||
if (typeof rowNum !== 'number') {
|
||||
rowNum = 1
|
||||
console.warn('由于你没有在 data 中定义 addDefaultRowNum 或 addDefaultRowNum 不是数字,所以默认添加一条空数据,如果不想默认添加空数据,请将定义 addDefaultRowNum 为 0')
|
||||
}
|
||||
this.eachAllTable((item) => {
|
||||
item.addRows()
|
||||
//item.add(rowNum)
|
||||
})
|
||||
if (typeof this.addAfter === 'function') this.addAfter(this.model)
|
||||
this.edit({})
|
||||
},
|
||||
/** 当点击了编辑(修改)按钮时调用此方法 */
|
||||
edit(record) {
|
||||
if (typeof this.editBefore === 'function') this.editBefore(record)
|
||||
this.visible = true
|
||||
this.activeKey = this.refKeys[0]
|
||||
this.form.resetFields()
|
||||
this.model = Object.assign({}, record)
|
||||
if (typeof this.editAfter === 'function') this.editAfter(this.model)
|
||||
},
|
||||
/** 关闭弹窗,并将所有JVxeTable实例回归到初始状态 */
|
||||
close() {
|
||||
this.visible = false
|
||||
this.eachAllTable((item) => {
|
||||
item._remove()
|
||||
})
|
||||
this.$emit('close')
|
||||
},
|
||||
|
||||
/** 查询某个tab的数据 */
|
||||
requestSubTableData(url, params, tab, success) {
|
||||
tab.loading = true
|
||||
getAction(url, params).then(res => {
|
||||
let { result } = res
|
||||
let dataSource = []
|
||||
if (result) {
|
||||
if (Array.isArray(result)) {
|
||||
dataSource = result
|
||||
} else if (Array.isArray(result.records)) {
|
||||
dataSource = result.records
|
||||
}
|
||||
}
|
||||
tab.dataSource = dataSource
|
||||
typeof success === 'function' ? success(res) : ''
|
||||
}).finally(() => {
|
||||
tab.loading = false
|
||||
})
|
||||
},
|
||||
/** 发起请求,自动判断是执行新增还是修改操作 */
|
||||
request(formData) {
|
||||
let url = this.url.add, method = 'post'
|
||||
if (this.model.id) {
|
||||
url = this.url.edit
|
||||
method = 'put'
|
||||
}
|
||||
this.confirmLoading = true
|
||||
console.log("formData===>",formData);
|
||||
httpAction(url, formData, method).then((res) => {
|
||||
if (res.success) {
|
||||
this.$message.success(res.message)
|
||||
this.$emit('ok')
|
||||
this.close()
|
||||
} else {
|
||||
this.$message.warning(res.message)
|
||||
}
|
||||
}).finally(() => {
|
||||
this.confirmLoading = false
|
||||
})
|
||||
},
|
||||
|
||||
/* --- handle 事件 --- */
|
||||
|
||||
/** ATab 选项卡切换事件 */
|
||||
handleChangeTabs(key) {
|
||||
// 自动重置scrollTop状态,防止出现白屏
|
||||
getRefPromise(this, key).then(vxeTable => {
|
||||
vxeTable.resetScrollTop()
|
||||
})
|
||||
},
|
||||
/** 关闭按钮点击事件 */
|
||||
handleCancel() {
|
||||
this.close()
|
||||
},
|
||||
/** 确定按钮点击事件 */
|
||||
handleOk() {
|
||||
/** 触发表单验证 */
|
||||
this.getAllTable().then(tables => {
|
||||
/** 一次性验证主表和所有的次表 */
|
||||
return validateFormAndTables(this.form, tables)
|
||||
}).then(allValues => {
|
||||
if (typeof this.classifyIntoFormData !== 'function') {
|
||||
throw this.throwNotFunction('classifyIntoFormData')
|
||||
}
|
||||
let formData = this.classifyIntoFormData(allValues)
|
||||
// 发起请求
|
||||
return this.request(formData)
|
||||
}).catch(e => {
|
||||
if (e.error === VALIDATE_NO_PASSED) {
|
||||
// 如果有未通过表单验证的子表,就自动跳转到它所在的tab
|
||||
this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
|
||||
} else {
|
||||
console.error(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
/* --- throw --- */
|
||||
|
||||
/** not a function */
|
||||
throwNotFunction(name) {
|
||||
return `${name} 未定义或不是一个函数`
|
||||
},
|
||||
|
||||
/** not a array */
|
||||
throwNotArray(name) {
|
||||
return `${name} 未定义或不是一个数组`
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
116
ant-design-vue-jeecg/src/mixins/OnlAutoListMixin.js
Normal file
116
ant-design-vue-jeecg/src/mixins/OnlAutoListMixin.js
Normal file
@ -0,0 +1,116 @@
|
||||
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
|
||||
|
||||
export const HrefJump = {
|
||||
data() {
|
||||
return {
|
||||
fieldHrefSlots: [],
|
||||
hrefComponent: {
|
||||
model: {
|
||||
title: '',
|
||||
width: '100%',
|
||||
visible: false,
|
||||
destroyOnClose: true,
|
||||
style: {
|
||||
top: 0,
|
||||
left: 0,
|
||||
height: '100%',
|
||||
margin: 0,
|
||||
padding: 0
|
||||
},
|
||||
bodyStyle: { padding: '8px', height: 'calc(100vh - 108px)', overflow: 'auto', overflowX: 'hidden' },
|
||||
// 隐藏掉取消按钮
|
||||
cancelButtonProps: { style: { display: 'none' } }
|
||||
},
|
||||
on: {
|
||||
ok: () => this.hrefComponent.model.visible = false,
|
||||
cancel: () => this.hrefComponent.model.visible = false
|
||||
},
|
||||
is: null,
|
||||
params: {},
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 处理接收href参数
|
||||
handleAcceptHrefParams(){
|
||||
this.acceptHrefParams={}
|
||||
let hrefparam = this.$route.query;
|
||||
if(hrefparam){
|
||||
this.acceptHrefParams = {...hrefparam}
|
||||
}
|
||||
},
|
||||
//支持链接href跳转
|
||||
handleClickFieldHref(field, record) {
|
||||
let href = field.href
|
||||
let urlPattern = /(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?/
|
||||
let compPattern = /\.vue(\?.*)?$/
|
||||
if (typeof href === 'string') {
|
||||
href = href.trim().replace(/\${([^}]+)?}/g, (s1, s2) => record[s2])
|
||||
if (urlPattern.test(href)) {
|
||||
window.open(href, '_blank')
|
||||
} else if (compPattern.test(href)) {
|
||||
this.openHrefCompModal(href)
|
||||
} else {
|
||||
this.$router.push(href)
|
||||
}
|
||||
}
|
||||
},
|
||||
openHrefCompModal(href) {
|
||||
// 解析 href 参数
|
||||
let index = href.indexOf('?')
|
||||
let path = href
|
||||
if (index !== -1) {
|
||||
path = href.substring(0, index)
|
||||
let paramString = href.substring(index + 1, href.length)
|
||||
let paramArray = paramString.split('&')
|
||||
let params = {}
|
||||
paramArray.forEach(paramObject => {
|
||||
let paramItem = paramObject.split('=')
|
||||
params[paramItem[0]] = paramItem[1]
|
||||
})
|
||||
this.hrefComponent.params = params
|
||||
} else {
|
||||
this.hrefComponent.params = {}
|
||||
}
|
||||
this.hrefComponent.model.visible = true
|
||||
this.hrefComponent.model.title = '操作'
|
||||
this.hrefComponent.is = () => import('@/views/' + (path.startsWith('/') ? path.slice(1) : path))
|
||||
},
|
||||
/** 处理列中的 href 跳转和 dict 字典,使两者可以兼容存在 */
|
||||
handleColumnHrefAndDict(column = {}, fieldHrefSlotKeysMap = {}) {
|
||||
let { customRender, hrefSlotName } = column
|
||||
if (!hrefSlotName && (column.scopedSlots && column.scopedSlots.customRender)) {
|
||||
//hrefSlotName = column.scopedSlots.customRender
|
||||
}
|
||||
// 如果 customRender 有值则代表使用了字典
|
||||
// 如果 hrefSlotName 有值则代表使用了href跳转
|
||||
// 两者可以兼容。兼容的具体思路为:先获取到字典替换的值,再添加href链接跳转
|
||||
if (customRender || hrefSlotName) {
|
||||
let dictCode = customRender
|
||||
let replaceFlag = '_replace_text_'
|
||||
column.customRender = (text, record) => {
|
||||
let value = text
|
||||
// 如果 dictCode 有值,就进行字典转换
|
||||
if (dictCode) {
|
||||
if (dictCode.startsWith(replaceFlag)) {
|
||||
let textFieldName = dictCode.replace(replaceFlag, '')
|
||||
value = record[textFieldName]
|
||||
} else {
|
||||
value = filterMultiDictText(this.dictOptions[dictCode], text)
|
||||
}
|
||||
}
|
||||
// 如果 hrefSlotName 有值,就生成一个 a 标签,包裹住字典替换后(或原生)的值
|
||||
if (hrefSlotName) {
|
||||
let field = fieldHrefSlotKeysMap[hrefSlotName]
|
||||
if (field) {
|
||||
// 此处为 JSX 语法
|
||||
return (<a onClick={() => this.handleClickFieldHref(field, record)}>{value}</a>)
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,7 @@
|
||||
import store from '@/store/'
|
||||
import { ACCESS_TOKEN } from '@/store/mutation-types'
|
||||
import Vue from 'vue'
|
||||
|
||||
export const WebsocketMixin = {
|
||||
mounted() {
|
||||
this.initWebSocket();
|
||||
@ -9,6 +12,7 @@ export const WebsocketMixin = {
|
||||
},
|
||||
methods:{
|
||||
initWebSocket: function () {
|
||||
let token = Vue.ls.get(ACCESS_TOKEN)
|
||||
console.log("------------WebSocket连接成功");
|
||||
// WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
|
||||
var userId = store.getters.userInfo.id;
|
||||
@ -18,7 +22,7 @@ export const WebsocketMixin = {
|
||||
if(!this.socketUrl.endsWith('/')){
|
||||
this.socketUrl = this.socketUrl + '/'
|
||||
}
|
||||
var url = window._CONFIG['domianURL'].replace("https://","wss://").replace("http://","ws://") + this.socketUrl + userId;
|
||||
var url = window._CONFIG['domianURL'].replace("https://","wss://").replace("http://","ws://") + this.socketUrl + userId + "/" + token;
|
||||
this.websock = new WebSocket(url);
|
||||
this.websock.onopen = this.websocketOnopen;
|
||||
this.websock.onerror = this.websocketOnerror;
|
||||
|
||||
Reference in New Issue
Block a user