addEntityModal.vue.bak 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <!-- 实体-新增实体 -->
  2. <template>
  3. <a-modal
  4. :title="headTitle"
  5. :visible="show"
  6. :mask-closable="false"
  7. @ok="handleOk"
  8. @cancel="handleClose"
  9. >
  10. <div style="display: flex">
  11. <div style="width:130px;">
  12. <div style="width:100px;height:100px;overflow:hidden;border:1px solid #ddd">
  13. <img v-if="headImg" style="width:100px;height:100px;" :src="headImg" alt="头像">
  14. </div>
  15. <label
  16. class="btn btn-orange"
  17. for="uploads"
  18. style="display:inline-block;width: 100px;padding: 0;text-align: center;line-height: 28px;border:1px solid #ccc;margin-top: 20px;"
  19. >选择图片</label>
  20. <input
  21. id="uploads"
  22. type="file"
  23. :value="imgFile"
  24. style="position:absolute; clip:rect(0 0 0 0);display: none"
  25. accept="image/png, image/jpeg, image/gif, image/jpg"
  26. @change="uploadImg($event, 1)"
  27. >
  28. </div>
  29. <a-form :form="form" style="flex: 1">
  30. <a-form-item :label="TEXT.ENT + '名称:'">
  31. <a-input v-decorator="formRules.name" />
  32. </a-form-item>
  33. <a-form-item :label="TEXT.ENT + '描述:'">
  34. <a-input v-decorator="formRules.memo" type="textarea" />
  35. </a-form-item>
  36. </a-form>
  37. </div>
  38. <div v-if="!entityId">
  39. <a-table
  40. :columns="columns"
  41. :data-source="resultList"
  42. :pagination="false"
  43. size="small"
  44. row-key="attrClsID"
  45. bordered
  46. >
  47. <template slot="attrValueType" slot-scope="text">
  48. {{ attrClsType[text] }}
  49. </template>
  50. <template slot="attrValue" slot-scope="text,record">
  51. <!--{{record}}-->
  52. <editable-cell
  53. :record="record"
  54. :text="text"
  55. :editable="record.edit"
  56. @change="onCellChange(record,'attrValue', $event)"
  57. @close="closeCell(record)"
  58. />
  59. <!--<a-input placeholder="请输入属性值" v-model="record.attrValue" style="width: 120px;"/>-->
  60. </template>
  61. </a-table>
  62. </div>
  63. </a-modal>
  64. </template>
  65. <script>
  66. import EditableCell from '../../views/common/editableCell'
  67. const api = require('@/api')
  68. const TEXT = window.USER_ROLE_LANGUAGE[window.USER_ROLE]
  69. const formItemLayout = {
  70. labelCol: {
  71. xs: { span: 24 },
  72. sm: { span: 6 }
  73. },
  74. wrapperCol: {
  75. xs: { span: 24 },
  76. sm: { span: 16 }
  77. }
  78. }
  79. const columns = [
  80. {
  81. rowClassName: 'name',
  82. title: '名称',
  83. dataIndex: 'attrClsName',
  84. align: 'center'
  85. },
  86. {
  87. title: '数据类型',
  88. dataIndex: 'attrValueType',
  89. align: 'center',
  90. scopedSlots: { customRender: 'attrValueType' }
  91. },
  92. {
  93. title: '单位',
  94. dataIndex: 'attrUnit',
  95. align: 'center'
  96. },
  97. {
  98. title: '属性值',
  99. dataIndex: 'attrValue',
  100. width: '30%',
  101. scopedSlots: { customRender: 'attrValue' }
  102. }
  103. ]
  104. export default {
  105. name: 'AddEntityModal',
  106. components: {
  107. EditableCell
  108. },
  109. // paramsId 实体类id title:标题 entityName: 实体名字 entityId:实体id isLink:改变路由 entClsIcon: 实体图标
  110. props: ['paramsId', 'title', 'entityName', 'entityId', 'entityMemo', 'isLink', 'entClsIcon', 'isModel'],
  111. data() {
  112. return {
  113. TEXT,
  114. columns,
  115. attrClsType: this.$const.attrClsType,
  116. resultList: [], // 必填列表
  117. fileName: '', // 本机文件地址
  118. headImg: '',
  119. imgFile: '',
  120. formItemLayout,
  121. headTitle: '',
  122. form: this.$form.createForm(this),
  123. show: true,
  124. formRules: { // 所有验证规则
  125. name: [ // 用作标识,我们统一写成和表单提交字段一样
  126. 'name', // 表单提交的字段
  127. {
  128. rules: [{ required: true, message: '此选项必填', whitespace: true }],
  129. initialValue: this.entityName
  130. }
  131. ],
  132. memo: [ // 用作标识,我们统一写成和表单提交字段一样
  133. 'memo', // 表单提交的字段
  134. {
  135. rules: [{ required: false, message: '可以选择填' }],
  136. initialValue: this.entityMemo
  137. }
  138. ]
  139. }
  140. }
  141. },
  142. created() {
  143. const vm = this
  144. this.headTitle = this.title
  145. if (this.paramsId) { // 新增
  146. api.attrClass.getAttrClassById2(this.paramsId, 'ELEM').then(function(data) {
  147. const resultList = []
  148. data.forEach(v => {
  149. if (v.notNull) {
  150. v.edit = {
  151. abled: false
  152. }
  153. v.attrValue = ''
  154. resultList.push(v)
  155. }
  156. })
  157. vm.resultList = resultList
  158. })
  159. }
  160. },
  161. mounted() {
  162. // 判断是新增还是添加
  163. if (this.entityName) {
  164. this.headImg = this.entClsIcon
  165. }
  166. },
  167. methods: {
  168. // 选择本地图片
  169. uploadImg(e, num) {
  170. var max_size = 80// 20k
  171. var _this = this
  172. // 上传图片
  173. var file = e.target.files[0]
  174. _this.fileName = file.name
  175. var size = file.size
  176. if (size > max_size * 1024) {
  177. this.$message.warning('建议上传图标不要超过80KB')
  178. }
  179. if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG|JPEG|BMP)$/.test(e.target.value)) {
  180. alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
  181. return false
  182. }
  183. var reader = new FileReader()
  184. reader.onload = (e) => {
  185. let data
  186. data = e.target.result
  187. this.$open('cropperImgModal', {
  188. paramImgUrl: data
  189. }, function(data) {
  190. this.headImg = data.data
  191. })
  192. }
  193. // 转化为base64
  194. reader.readAsDataURL(file)
  195. // 转化为blob
  196. // reader.readAsArrayBuffer(file)
  197. },
  198. async getFormData() {
  199. var result = await this.$formValidateFields('form') // form的字符串
  200. // 在这里对form返回数据进行处理
  201. return result
  202. },
  203. async handleOk(e) {
  204. var result = await this.getFormData() // this是当前组件的this, 'form'此表单的名称,一定要是字符串
  205. const entClsID = this.paramsId // 实体类id
  206. const entId = this.entityId // 实体id
  207. const entName = result.name.trim() // 实体名字
  208. const uniqueMatchList = this.$checkSpecialChar(entName)
  209. if (uniqueMatchList.length > 0) {
  210. this.$message.error(`${this.isModel ? '模板' : '实体'}名称不能包含特殊字符【${uniqueMatchList.join('、')}】!`)
  211. return false
  212. }
  213. if (!result.memo) result.memo = ''
  214. const entMemo = result.memo.trim()
  215. const entIcon = (this.headImg && this.headImg != 'null') ? encodeURIComponent(this.headImg) : ''
  216. const notNullAttrVOList = []
  217. this.resultList.forEach(v => {
  218. notNullAttrVOList.push({ attrClsID: v.attrClsID, attrValue: v.attrValue })
  219. })
  220. if (entClsID) {
  221. // 新增实体
  222. if (notNullAttrVOList.some(v => { return !v.attrValue })) {
  223. this.$message.error('属性值必填')
  224. return false
  225. } else {
  226. api.entity.addEntity(entClsID, entName, entMemo, entIcon, notNullAttrVOList, this.isModel).then((data) => {
  227. if (data !== false) {
  228. if (this.isLink) {
  229. this.$trigger('entity:nodeCreated', data)
  230. }
  231. this.$close(data)
  232. }
  233. })
  234. }
  235. }
  236. if (entId) {
  237. // 修改实体
  238. api.entity.updateEntityName(entId, entName, entMemo, entIcon, this.isModel).then((data) => {
  239. if (data !== false) {
  240. if (this.isLink) {
  241. this.$trigger('entity:nodeUpdated', data)
  242. }
  243. this.$close(data)
  244. }
  245. })
  246. }
  247. },
  248. // 关闭模态窗
  249. handleClose(e) {
  250. this.$close(false)
  251. },
  252. closeCell(record) {
  253. record.edit.abled = false
  254. },
  255. onCellChange(target, data, value) {
  256. target.attrValue = value.toString()
  257. target.edit.abled = false
  258. }
  259. }
  260. }
  261. </script>
  262. <style scoped>
  263. </style>