dialog.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <el-dialog
  3. :title="`${type}统一门户`"
  4. :visible.sync="dialogVisible"
  5. width="20%"
  6. >
  7. <el-form label-width="80px" class="zw-criterion">
  8. <el-form-item label="图片">
  9. <el-upload
  10. class="avatar-uploader"
  11. :action="''"
  12. :auto-upload="false"
  13. :show-file-list="false"
  14. :on-change="handleAvatarChangeIcon"
  15. >
  16. <img v-if="imageUrl" :src="imageUrl" class="avatar" />
  17. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  18. </el-upload>
  19. </el-form-item>
  20. <el-form-item label="名称" required>
  21. <el-input v-model="form.name" placeholder="请输入内容"></el-input>
  22. </el-form-item>
  23. <el-form-item label="排序" required>
  24. <el-input
  25. v-model="form.sort"
  26. type="number"
  27. placeholder="请输入内容"
  28. ></el-input>
  29. </el-form-item>
  30. <el-form-item label="链接" required>
  31. <el-input v-model="form.linkUrl" placeholder="请输入内容"></el-input>
  32. </el-form-item>
  33. <el-form-item label="链接类型" required>
  34. <DictSelection
  35. dictName="链接分类"
  36. :disabled="false"
  37. v-model="form.linkType"
  38. ></DictSelection>
  39. </el-form-item>
  40. <el-form-item label="架构类型" required>
  41. <DictSelection
  42. dictName="架构分类"
  43. :disabled="false"
  44. v-model="form.architType"
  45. ></DictSelection>
  46. </el-form-item>
  47. <el-form-item label="备注">
  48. <el-input v-model="form.remark" placeholder="请输入内容"></el-input>
  49. </el-form-item>
  50. </el-form>
  51. <div slot="footer" class="dialog-footer">
  52. <el-button size="small" @click="dialogVisible = false">关 闭</el-button>
  53. <el-button size="small" @click="config" type="primary">确 定</el-button>
  54. </div>
  55. </el-dialog>
  56. </template>
  57. <script>
  58. import { uploadFile } from '@/api/system/file/index.js';
  59. import { getImageUrl } from '@/utils/file';
  60. import {
  61. saveInfo,
  62. updateInfo,
  63. getDetails
  64. } from '@/api/system/unifiedPortal/index.js';
  65. export default {
  66. data() {
  67. return {
  68. dialogVisible: false,
  69. file: null,
  70. type: '新增',
  71. form: {},
  72. imageUrl: '',
  73. options: [
  74. {
  75. label: '程序应用',
  76. value: '1'
  77. },
  78. {
  79. label: '网页应用',
  80. value: '2'
  81. }
  82. ]
  83. };
  84. },
  85. methods: {
  86. handleClose() {
  87. this.dialogVisible = false;
  88. },
  89. async open(type, id) {
  90. this.type = type;
  91. if (type == '新增') {
  92. this.form = {};
  93. this.imageUrl = '';
  94. this.file = null;
  95. } else {
  96. this.file = null;
  97. this.form = await getDetails(id);
  98. this.imageUrl = getImageUrl(this.form.iconPath);
  99. }
  100. this.dialogVisible = true;
  101. },
  102. handleAvatarChangeIcon(file, fileList) {
  103. //选中文件触发的change事件
  104. console.log(file);
  105. const isJPG = file.raw.type === 'image/jpeg';
  106. const isPNG = file.raw.type === 'image/png';
  107. const isLt10M = file.raw.size / 1024 / 1024 < 10; //限制上传文件的大小
  108. if (!isLt10M) {
  109. this.$message.error('上传文件大小不能超过 10MB!');
  110. return false;
  111. }
  112. if (!isPNG && !isJPG && !isLt2M) {
  113. this.$message.error('上传图片只能是 JPG/PNG 格式!');
  114. return false;
  115. } else {
  116. this.imageUrl = URL.createObjectURL(file.raw); //赋值图片的url,用于图片回显功能
  117. this.file = file.raw; //赋值文件对象,用于上传操作
  118. }
  119. },
  120. async config() {
  121. if (!this.form.name) {
  122. return this.$message.error('请输入名称');
  123. }
  124. if (this.form.sort < 0) {
  125. return this.$message.error('请输入排序');
  126. }
  127. if (!this.form.linkUrl) {
  128. return this.$message.error('请输入链接');
  129. }
  130. if (this.form.linkType < 0) {
  131. return this.$message.error('请选择链接类型');
  132. }
  133. if (this.form.architType < 0) {
  134. return this.$message.error('请选择架构类型');
  135. }
  136. // 文件上传操作
  137. if (this.file) {
  138. try {
  139. let res = await uploadFile({
  140. multiPartFile: this.file,
  141. module: 'gateway'
  142. });
  143. this.form.icon = res.data.id;
  144. } catch (error) {
  145. console.log(error);
  146. return this.$message.error('上传文件失败');
  147. }
  148. } else {
  149. if (this.type == '新增') {
  150. return this.$message.error('请上传图片');
  151. }
  152. }
  153. if (this.type == '新增') {
  154. // 保存数据
  155. try {
  156. await saveInfo(this.form);
  157. this.$message.success('新增成功');
  158. this.dialogVisible = false;
  159. this.$emit('reload');
  160. } catch (error) {
  161. console.log(error);
  162. this.$message.error('新增失败');
  163. }
  164. } else {
  165. // 修改
  166. try {
  167. await updateInfo(this.form);
  168. this.$message.success('修改成功');
  169. this.dialogVisible = false;
  170. this.$emit('reload');
  171. } catch (error) {
  172. this.$message.error('修改失败');
  173. }
  174. }
  175. }
  176. }
  177. };
  178. </script>
  179. <style lang="scss" scoped>
  180. .typeSelect {
  181. width: 100%;
  182. }
  183. .avatar-uploader {
  184. height: 120px;
  185. width: 100%;
  186. }
  187. .avatar {
  188. width: 120px;
  189. height: 120px;
  190. object-fit: cover;
  191. }
  192. .avatar-uploader-icon {
  193. width: 120px;
  194. height: 120px;
  195. display: flex;
  196. align-items: center;
  197. justify-content: center;
  198. border: 1px dashed #d9d9d9;
  199. }
  200. </style>