AlgoParamEdit.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <div>
  3. <div class="algorithm-detail" v-loading="loading">
  4. <div class="algo-name">
  5. <div>【{{ curRow?.name }}】</div>
  6. <div class="algo-set" @click="isParamSetVisible = true" v-if="hasSetParamPermission()">
  7. <el-icon><Setting /></el-icon>
  8. 设置默认参数
  9. </div>
  10. </div>
  11. <div class="algo-title">
  12. <div class="title">算法介绍</div>
  13. <el-icon class="edit-icon" :size="16" @click="changeEditStatus" v-if="hasAlgoEditPermission()"
  14. ><EditPen
  15. /></el-icon>
  16. </div>
  17. <div class="media-section">
  18. <template v-if="editParam.url">
  19. <el-image
  20. v-if="isImage(editParam.url)"
  21. class="media-img"
  22. :src="editParam.url"
  23. :preview-src-list="[editParam.url]"
  24. :preview-teleported="true"
  25. fit="cover"
  26. />
  27. <video class="media-video" v-else :src="editParam.url" controls />
  28. <div class="media-set" v-if="isEditing">
  29. <el-upload
  30. :action="actionUrl"
  31. :show-file-list="false"
  32. :on-success="handleUpload"
  33. :headers="getHeaders()"
  34. :data="{ bizType: 'ALGO' }"
  35. >
  36. <el-icon class="icon-upload" :size="20"><Upload /></el-icon>
  37. </el-upload>
  38. <el-icon class="icon-delete" :size="20" @click="handleDelete"><Delete /></el-icon>
  39. </div>
  40. </template>
  41. <template v-else>
  42. <div class="empty-media">
  43. <img src="@/assets/images/algo/no-algo-url.png" alt="" width="200" height="200" />
  44. <el-upload
  45. v-if="isEditing"
  46. :action="actionUrl"
  47. :show-file-list="false"
  48. :on-success="handleUpload"
  49. :headers="getHeaders()"
  50. :data="{ bizType: 'ALGO' }"
  51. >
  52. <div class="empty-media-text">请点击上传算法介绍图片或视频</div>
  53. </el-upload>
  54. </div>
  55. </template>
  56. </div>
  57. <el-card class="remark-card">
  58. <template v-if="!isEditing">
  59. <div v-for="(line, idx) in remarkLines" :key="idx">{{ line }}</div>
  60. </template>
  61. <template v-else>
  62. <el-input
  63. v-model="editParam.remark"
  64. type="textarea"
  65. :autosize="{ minRows: 4, maxRows: 8 }"
  66. placeholder="请输入算法描述"
  67. />
  68. </template>
  69. </el-card>
  70. <div class="btn-group" v-if="isEditing">
  71. <el-button @click="cancelEdit">取消</el-button>
  72. <el-button type="primary" @click="saveChanges">保存</el-button>
  73. </div>
  74. </div>
  75. <el-dialog
  76. v-model="isParamSetVisible"
  77. title="请输入代码修改算法参数,提交后生效"
  78. width="60%"
  79. align-center
  80. @close="cancelAlgoParamsEdit"
  81. >
  82. <json-editor-vue
  83. class="json-editor"
  84. v-model="extraJson"
  85. :language="'zh-CN'"
  86. @update:modelValue="handleUpdateAlgoParam"
  87. @validationError="editError"
  88. />
  89. <template #footer>
  90. <div class="dialog-footer">
  91. <el-button @click="cancelAlgoParamsEdit">取消</el-button>
  92. <el-button type="primary" @click="saveAlgoParamsChanges"> 提交 </el-button>
  93. </div>
  94. </template>
  95. </el-dialog>
  96. </div>
  97. </template>
  98. <script setup lang="ts">
  99. import urlJoin from 'url-join';
  100. import { storeToRefs } from 'pinia';
  101. import { ref, computed, watch, nextTick } from 'vue';
  102. import { ElButton, ElIcon, ElMessage, ElUpload, ElDialog, ElInput, ElCard } from 'element-plus';
  103. import { EditPen, Upload, Delete, Setting } from '@element-plus/icons-vue';
  104. import { useGlobSetting } from '@/hooks/setting';
  105. import { useUserStore } from '@/store/modules/user';
  106. import { getHeaders } from '@/utils/http/axios';
  107. import { useAlgoDataStore } from '../useAlgoData';
  108. import { PERM_ALGO } from '@/types/permission/constants';
  109. import { UpdateAlgoParamParams, updateAlgoParam } from '@/api/algo/algo';
  110. import JsonEditorVue from 'json-editor-vue3';
  111. const { urlPrefix } = useGlobSetting();
  112. const userStore = useUserStore();
  113. const algoDataStore = useAlgoDataStore();
  114. const { curRow } = storeToRefs(algoDataStore);
  115. const { updateCurRow } = algoDataStore;
  116. const loading = ref(false);
  117. const isParamSetVisible = ref(false);
  118. const isEditing = ref(false);
  119. const editParam = ref<UpdateAlgoParamParams>({});
  120. const extraJson = ref({});
  121. const remarkLines = computed(() => (curRow.value?.remark ? curRow.value?.remark.split(/\r?\n/) : []));
  122. const actionUrl = computed(() => {
  123. return urlJoin(urlPrefix!, `/admin/minio/uploadFile`);
  124. });
  125. function initEditParam() {
  126. editParam.value.algoId = curRow.value?.id;
  127. editParam.value.algoParam = curRow.value?.extra;
  128. editParam.value.remark = curRow.value?.remark;
  129. editParam.value.url = curRow.value?.url;
  130. extraJson.value = JSON.parse(curRow.value?.extra || '{}');
  131. }
  132. function isImage(url: string) {
  133. return /\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(url);
  134. }
  135. watch(
  136. curRow,
  137. () => {
  138. if (curRow.value) {
  139. initEditParam();
  140. loading.value = false;
  141. }
  142. },
  143. { immediate: true, deep: true },
  144. );
  145. // 默认参数设置权限
  146. const hasSetParamPermission = () => {
  147. return userStore.checkPermission(PERM_ALGO.PREVIEW_PARAM_DEFAULT);
  148. };
  149. // 算法介绍设置权限
  150. const hasAlgoEditPermission = () => {
  151. return userStore.checkPermission(PERM_ALGO.PREVIEW_DESCRIPTION);
  152. };
  153. const handleUpload = (res) => {
  154. editParam.value.url = res.data.url;
  155. };
  156. const handleDelete = () => {
  157. editParam.value.url = '';
  158. };
  159. const changeEditStatus = () => {
  160. isEditing.value = !isEditing.value;
  161. initEditParam();
  162. };
  163. const saveChanges = () => {
  164. if (!editParam.value.remark) {
  165. ElMessage({
  166. message: '算法描述不可为空',
  167. type: 'warning',
  168. });
  169. return;
  170. }
  171. loading.value = true;
  172. updateAlgoParam(editParam.value).then(() => {
  173. updateCurRow(curRow.value!);
  174. ElMessage({
  175. message: '保存成功',
  176. type: 'success',
  177. });
  178. nextTick(() => {
  179. isEditing.value = false;
  180. loading.value = false;
  181. });
  182. });
  183. };
  184. const cancelEdit = () => {
  185. isEditing.value = false;
  186. initEditParam();
  187. };
  188. const handleUpdateAlgoParam = (newJsonData) => {
  189. editParam.value.algoParam = newJsonData;
  190. };
  191. const cancelAlgoParamsEdit = () => {
  192. isParamSetVisible.value = false;
  193. initEditParam();
  194. };
  195. const saveAlgoParamsChanges = () => {
  196. updateAlgoParam(editParam.value).then(() => {
  197. updateCurRow(curRow.value!);
  198. ElMessage({
  199. message: '算法参数设置成功',
  200. type: 'success',
  201. });
  202. isParamSetVisible.value = false;
  203. });
  204. };
  205. const editError = (_editor: any, errors: any[]) => {
  206. if (errors.length > 0) {
  207. ElMessage({
  208. message: 'JSON 格式错误,请检查',
  209. type: 'error',
  210. });
  211. }
  212. };
  213. </script>
  214. <style scoped lang="scss">
  215. .algorithm-detail {
  216. height: 100%;
  217. padding: 20px 8px;
  218. .algo-name {
  219. font-size: 20px;
  220. font-weight: bold;
  221. margin-bottom: 16px;
  222. display: flex;
  223. align-items: center;
  224. .algo-set {
  225. font-size: 14px;
  226. color: #409eff;
  227. cursor: pointer;
  228. display: flex;
  229. align-items: center;
  230. margin-left: 10px;
  231. .el-icon {
  232. margin-right: 5px;
  233. }
  234. }
  235. }
  236. .algo-title {
  237. height: 22px;
  238. display: flex;
  239. align-items: center;
  240. margin: 16px 0 8px 12px;
  241. font-weight: 600;
  242. .title {
  243. line-height: 22px;
  244. margin-left: 10px;
  245. position: relative;
  246. }
  247. .title::before {
  248. content: '';
  249. display: inline-block;
  250. width: 3px;
  251. height: 14px;
  252. background-color: #409eff;
  253. border-radius: 1px;
  254. position: absolute;
  255. left: -10px;
  256. top: 50%;
  257. transform: translateY(-50%);
  258. }
  259. .edit-icon {
  260. margin-left: 8px;
  261. color: #409eff;
  262. cursor: pointer;
  263. }
  264. }
  265. }
  266. .media-section {
  267. height: 40%;
  268. margin: 20px;
  269. display: flex;
  270. align-items: center;
  271. justify-content: center;
  272. flex-direction: column;
  273. position: relative;
  274. .media-img,
  275. .media-video {
  276. height: 100%;
  277. }
  278. .media-img .img {
  279. cursor: zoom-in;
  280. }
  281. .media-set {
  282. padding: 10px 5px;
  283. border-radius: 50px;
  284. position: absolute;
  285. top: 30%;
  286. right: 0;
  287. background: #fefefe;
  288. box-shadow: #cdcdcda3 2px 2px 4px 2px;
  289. }
  290. .icon-upload,
  291. .icon-delete {
  292. margin: 5px 0;
  293. cursor: pointer;
  294. }
  295. .icon-upload:hover,
  296. .icon-delete:hover {
  297. color: #409eff;
  298. }
  299. .icon-upload {
  300. margin-bottom: 20px;
  301. }
  302. }
  303. .empty-media {
  304. height: 100%;
  305. display: flex;
  306. flex-direction: column;
  307. align-items: center;
  308. justify-content: center;
  309. .empty-media-text {
  310. color: #409eff;
  311. cursor: pointer;
  312. }
  313. }
  314. .remark-card {
  315. height: 40%;
  316. margin: 0 10px 20px 10px;
  317. margin-bottom: 24px;
  318. }
  319. .btn-group {
  320. display: flex;
  321. justify-content: flex-end;
  322. gap: 12px;
  323. }
  324. :deep(.el-dialog__body) {
  325. height: 500px;
  326. overflow: auto;
  327. }
  328. .json-editor {
  329. width: 100%;
  330. height: 100%;
  331. }
  332. </style>