addMenuDialog.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <div>
  3. <el-dialog
  4. :title="type == 'edit' ? '编辑菜单' : '添加菜单'"
  5. class="table"
  6. :visible.sync="show"
  7. :close-on-click-modal="false"
  8. width="800px"
  9. top="8vh"
  10. >
  11. <div v-loading="loading">
  12. <el-form ref="form" :model="form" label-width="100px">
  13. <el-form-item label="菜单名称">
  14. <el-input style="width: 300px" v-model="form.menuName"></el-input>
  15. </el-form-item>
  16. <el-form-item label="菜单默认图标">
  17. <el-tooltip
  18. class="item"
  19. effect="dark"
  20. :content="form.menuIcon ? '更改图标' : '上传图标'"
  21. placement="top"
  22. >
  23. <el-image
  24. style="width: 50px; height: 50px;cursor: pointer;display: flex;align-items: center;justify-content: center;"
  25. :src="form.menuIcon"
  26. fit="cover"
  27. @click="addUrlIcon('menuIcon')"
  28. >
  29. <div
  30. slot="error"
  31. class="image-slot"
  32. @click="addUrlIcon('menuIcon')"
  33. >
  34. <i class="el-icon-picture-outline"></i>
  35. </div>
  36. </el-image>
  37. </el-tooltip>
  38. </el-form-item>
  39. <el-form-item label="菜单激活图标" v-if="['admin-sidebar'].includes(this.tagType)">
  40. <el-tooltip
  41. class="item"
  42. effect="dark"
  43. :content="form.menuActiveIcon ? '更改图标' : '上传图标'"
  44. placement="top"
  45. >
  46. <el-image
  47. style="width: 50px; height: 50px;cursor: pointer;display: flex;align-items: center;justify-content: center;"
  48. :src="form.menuActiveIcon"
  49. fit="cover"
  50. @click="addUrlIcon('menuActiveIcon')"
  51. >
  52. <div
  53. slot="error"
  54. class="image-slot"
  55. @click="addUrlIcon('menuActiveIcon')"
  56. >
  57. <i class="el-icon-picture-outline"></i>
  58. </div>
  59. </el-image>
  60. </el-tooltip>
  61. </el-form-item>
  62. </el-form>
  63. </div>
  64. <!-- 按钮区域 -->
  65. <div slot="footer" class="el-dialog__footer">
  66. <el-button @click="close()">取 消</el-button>
  67. <el-button type="primary" @click="submit()">确认</el-button>
  68. </div>
  69. </el-dialog>
  70. </div>
  71. </template>
  72. <script>
  73. import "@/common/aws-sdk-2.235.1.min.js";
  74. export default {
  75. data() {
  76. return {
  77. show: false,
  78. loading: false,
  79. form: {
  80. menuName: "",
  81. menuIcon: "",
  82. menuActiveIcon: "",
  83. children:[]
  84. },
  85. type: null,
  86. tagType:null,
  87. };
  88. },
  89. methods: {
  90. open(data, type = "add",tagType) {
  91. if (data) {
  92. this.form = JSON.parse(JSON.stringify(data));
  93. } else {
  94. this.init();
  95. }
  96. this.type = type;
  97. this.tagType = tagType;
  98. this.show = true;
  99. },
  100. close() {
  101. this.show = false;
  102. this.init();
  103. },
  104. init() {
  105. this.form = {
  106. menuName: "",
  107. menuIcon: "",
  108. menuActiveIcon: "",
  109. children:[]
  110. };
  111. },
  112. submit() {
  113. if(!this.form.menuName || !this.form.menuIcon || (!['admin-school'].includes(this.tagType) && !this.form.menuActiveIcon ))return this.$message.error("请完善表单")
  114. this.$emit("success", {item:this.form,type:this.type,tagType:this.tagType});
  115. },
  116. async addUrlIcon(type = "menuIcon") {
  117. let _url = await this.uploadFile("image/*");
  118. if (_url) {
  119. this.form[type] = _url;
  120. } else {
  121. return console.log("无图片");
  122. }
  123. },
  124. uploadFile(accept = "*") {
  125. return new Promise((resolve) => {
  126. const input = document.createElement("input");
  127. input.type = "file";
  128. input.accept = accept;
  129. input.onchange = (event) => {
  130. const file = event.target.files[0];
  131. if (file) {
  132. let credentials = {
  133. accessKeyId: "AKIATLPEDU37QV5CHLMH",
  134. secretAccessKey: "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",
  135. }; //秘钥形式的登录上传
  136. window.AWS.config.update(credentials);
  137. window.AWS.config.region = "cn-northwest-1"; //设置区域
  138. let bucket = new window.AWS.S3({ params: { Bucket: "ccrb" } }); //选择桶
  139. // let _name = file.name;
  140. // let size = file.size;
  141. let params = {
  142. Key:
  143. file.name.split(".")[0] +
  144. new Date().getTime() +
  145. "." +
  146. file.name.split(".")[file.name.split(".").length - 1],
  147. ContentType: file.type,
  148. Body: file,
  149. "Access-Control-Allow-Credentials": "*",
  150. ACL: "public-read",
  151. }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  152. let options = {
  153. partSize: 2048 * 1024 * 1024,
  154. queueSize: 2,
  155. leavePartsOnError: true,
  156. };
  157. bucket
  158. .upload(params, options)
  159. .on("httpUploadProgress", (evt) => {
  160. console.log(evt);
  161. })
  162. .send((err, data) => {
  163. if (err) {
  164. this.$message.error("上传失败");
  165. return resolve("");
  166. } else {
  167. return resolve(data.Location);
  168. }
  169. });
  170. } else {
  171. resolve("");
  172. }
  173. };
  174. input.click();
  175. });
  176. },
  177. addArg(value) {
  178. if (this.form.argumentList.includes(value)) {
  179. this.form.argumentList.splice(this.form.argumentList.indexOf(value), 1);
  180. } else {
  181. this.form.argumentList.push(value);
  182. }
  183. },
  184. },
  185. };
  186. </script>
  187. <style scoped>
  188. .table >>> .el-dialog__header {
  189. padding: 15px 20px;
  190. background: #454545;
  191. }
  192. .table >>> .el-dialog__title {
  193. color: #fff;
  194. }
  195. </style>