addCampusDialog.vue 5.2 KB

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