index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <template>
  2. <div v-if="type == 'image'">
  3. <ul v-for="(item,index) in value" :key="index" class="el-upload-list el-upload-list--picture-card">
  4. <li tabindex="0" class="el-upload-list__item is-ready" :style="'width: '+width+'px;height: '+height+'px'">
  5. <div>
  6. <img :src="item" alt="" class="el-upload-list__item-thumbnail">
  7. <span class="el-upload-list__item-actions">
  8. <span v-if="index != 0" class="el-upload-list__item-preview" @click="moveMaterial(index,'up')">
  9. <i class="el-icon-back" />
  10. </span>
  11. <span class="el-upload-list__item-preview" @click="zoomMaterial(index)">
  12. <i class="el-icon-view" />
  13. </span>
  14. <span class="el-upload-list__item-delete" @click="deleteMaterial(index)">
  15. <i class="el-icon-delete" />
  16. </span>
  17. <span v-if="index != value.length-1" class="el-upload-list__item-preview" @click="moveMaterial(index,'down')">
  18. <i class="el-icon-right" />
  19. </span>
  20. </span>
  21. </div>
  22. </li>
  23. </ul>
  24. <div v-if="num > value.length" tabindex="0" class="el-upload el-upload--picture-card" :style="'width: '+width+'px;height: '+height+'px;'+'line-height:'+height+'px;'" @click="toSeleteMaterial">
  25. <i class="el-icon-plus" />
  26. </div>
  27. <!-- 查看 -->
  28. <el-dialog
  29. append-to-body
  30. :visible.sync="dialogVisible"
  31. width="35%"
  32. >
  33. <img :src="url" alt="" style="width: 100%">
  34. </el-dialog>
  35. <!-- 素材列表 -->
  36. <el-dialog
  37. title="图片素材库"
  38. append-to-body
  39. :visible.sync="listDialogVisible"
  40. width="70%"
  41. >
  42. <el-container>
  43. <el-aside width="unset">
  44. <div style="margin-bottom: 10px">
  45. <el-button
  46. class="el-icon-plus"
  47. size="small"
  48. @click="materialgroupAdd()"
  49. >
  50. 添加分组
  51. </el-button>
  52. </div>
  53. <div class="group-list">
  54. <div class="group-item" v-for="(group) in materialGroupList">
  55. <el-button @click="selectGroup(group)" type="primary" plain >{{group.name}}</el-button>
  56. </div>
  57. </div>
  58. </el-aside>
  59. <el-main>
  60. <el-card>
  61. <div slot="header">
  62. <el-row>
  63. <el-col :span="12">
  64. <span>{{ materialGroup.name }}</span>
  65. <span v-if="materialGroup.groupId >0">
  66. <el-button size="small" type="text" class="el-icon-edit" style="margin-left: 10px;" @click="materialgroupEdit(materialGroup)">重命名</el-button>
  67. <el-button size="small" type="text" class="el-icon-delete" style="margin-left: 10px;color: red" @click="materialgroupDelete(materialGroup)">删除</el-button>
  68. </span>
  69. </el-col>
  70. <el-col :span="12" style="text-align: right;">
  71. <el-upload
  72. :action="uploadUrl"
  73. :file-list="[]"
  74. :on-progress="handleProgress"
  75. :before-upload="beforeUpload"
  76. :on-success="handleSuccess"
  77. :data="{type: 1}"
  78. multiple
  79. >
  80. <el-button size="small" type="primary">批量上传</el-button>
  81. </el-upload>
  82. </el-col>
  83. </el-row>
  84. </div>
  85. <div v-loading="tableLoading">
  86. <el-alert
  87. v-if="tableData.length <= 0"
  88. title="暂无数据"
  89. type="info"
  90. :closable="false"
  91. center
  92. show-icon
  93. />
  94. <el-row :gutter="5">
  95. <el-checkbox-group v-model="urls" :max="num - value.length">
  96. <el-col v-for="(item,index) in tableData" :key="index" :span="4">
  97. <el-card :body-style="{ padding: '5px' }">
  98. <el-image
  99. style="width: 100%;height: 100px"
  100. :src="item.url"
  101. fit="contain"
  102. :preview-src-list="[item.url]"
  103. :z-index="9999"
  104. />
  105. <div>
  106. <el-checkbox class="material-name" :label="item.url">
  107. 选择
  108. </el-checkbox>
  109. <el-row>
  110. <el-col :span="24" class="col-do">
  111. <el-button type="text" size="medium" @click="materialDel(item)">删除</el-button>
  112. </el-col>
  113. </el-row>
  114. </div>
  115. </el-card>
  116. </el-col>
  117. </el-checkbox-group>
  118. </el-row>
  119. <pagination
  120. v-show="total>0"
  121. :total="total"
  122. :page.sync="queryParams.pageNum"
  123. :limit.sync="queryParams.pageSize"
  124. @pagination="getMaterialList"
  125. />
  126. </div>
  127. </el-card>
  128. </el-main>
  129. </el-container>
  130. <span slot="footer" class="dialog-footer">
  131. <el-button @click="listDialogVisible = false">取 消</el-button>
  132. <el-button type="primary" @click="submit">确 定</el-button>
  133. </span>
  134. </el-dialog>
  135. </div>
  136. </template>
  137. <script>
  138. import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial, exportMaterial } from "@/api/store/material";
  139. import { getAllMaterialGroup,listMaterialGroup, getMaterialGroup, delMaterialGroup, addMaterialGroup, updateMaterialGroup, exportMaterialGroup } from "@/api/store/materialGroup";
  140. export default {
  141. name: 'ImageSelect',
  142. props: {
  143. // 素材数据
  144. value: {
  145. type: Array,
  146. default() {
  147. return []
  148. }
  149. },
  150. // 素材类型
  151. type: {
  152. type: String
  153. },
  154. // 素材限制数量,默认5个
  155. num: {
  156. type: Number,
  157. default() {
  158. return 5
  159. }
  160. },
  161. // 宽度
  162. width: {
  163. type: Number,
  164. default() {
  165. return 150
  166. }
  167. },
  168. // 宽度
  169. height: {
  170. type: Number,
  171. default() {
  172. return 150
  173. }
  174. }
  175. },
  176. data() {
  177. return {
  178. uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadOSS",
  179. dialogVisible: false,
  180. url: '',
  181. listDialogVisible: false,
  182. materialGroupList: [],
  183. materialGroupLoading: false,
  184. materialGroup:{},
  185. tableData: [],
  186. resultNumber: 0,
  187. total: 0,
  188. queryParams: {
  189. pageNum: 1,
  190. pageSize: 10,
  191. type: null,
  192. groupId: null,
  193. name: null,
  194. url: null,
  195. isDel: null,
  196. createUserId: null,
  197. },
  198. tableLoading: false,
  199. urls: []
  200. }
  201. },
  202. mounted(){
  203. this.getAllMaterialGroup();
  204. },
  205. methods: {
  206. selectGroup(item){
  207. this.materialGroup=item;
  208. this.queryParams.groupId=item.groupId;
  209. this.getMaterialList();
  210. },
  211. materialgroupAdd() {
  212. const that = this
  213. this.$prompt('请输入分组名', '提示', {
  214. confirmButtonText: '确定',
  215. cancelButtonText: '取消'
  216. }).then(({ value }) => {
  217. addMaterialGroup({
  218. name: value
  219. }).then(function() {
  220. that.materialGroup={};
  221. that.getAllMaterialGroup()
  222. })
  223. }).catch(() => {
  224. })
  225. },
  226. materialgroupDelete(materialgroupObj) {
  227. const that = this
  228. this.$confirm('是否确认删除该分组?', '提示', {
  229. confirmButtonText: '确定',
  230. cancelButtonText: '取消',
  231. type: 'warning'
  232. }).then(function() {
  233. delMaterialGroup(materialgroupObj.groupId)
  234. .then(function() {
  235. that.materialGroup={};
  236. that.getAllMaterialGroup()
  237. })
  238. })
  239. },
  240. materialgroupEdit(materialgroupObj) {
  241. const that = this
  242. this.$prompt('请输入分组名', '提示', {
  243. confirmButtonText: '确定',
  244. cancelButtonText: '取消',
  245. inputValue: materialgroupObj.name
  246. }).then(({ value }) => {
  247. updateMaterialGroup({
  248. groupId: materialgroupObj.groupId,
  249. name: value
  250. }).then(function() {
  251. that.materialGroup={};
  252. that.getAllMaterialGroup()
  253. })
  254. }).catch(() => {
  255. })
  256. },
  257. getAllMaterialGroup() {
  258. this.materialGroupLoading = true;
  259. getAllMaterialGroup({}).then(response => {
  260. this.materialGroupList = response.data
  261. console.log(this.materialGroupList)
  262. this.materialGroupLoading = false;
  263. });
  264. },
  265. getMaterialList() {
  266. this.tableLoading = true;
  267. listMaterial(this.queryParams).then(response => {
  268. this.tableData = response.rows;
  269. this.total = response.total;
  270. this.tableLoading = false;
  271. });
  272. },
  273. moveMaterial(index, type) {
  274. if (type == 'up') {
  275. const tempOption = this.value[index - 1]
  276. this.$set(this.value, index - 1, this.value[index])
  277. this.$set(this.value, index, tempOption)
  278. }
  279. if (type == 'down') {
  280. const tempOption = this.value[index + 1]
  281. this.$set(this.value, index + 1, this.value[index])
  282. this.$set(this.value, index, tempOption)
  283. }
  284. },
  285. zoomMaterial(index) {
  286. this.dialogVisible = true
  287. this.url = this.value[index]
  288. },
  289. deleteMaterial(index) {
  290. const that = this
  291. this.$confirm('是否确认删除?', '提示', {
  292. confirmButtonText: '确定',
  293. cancelButtonText: '取消',
  294. type: 'warning'
  295. }).then(function() {
  296. that.value.splice(index, 1)
  297. that.urls = []
  298. })
  299. },
  300. toSeleteMaterial() {
  301. this.listDialogVisible = true
  302. this.getAllMaterialGroup()
  303. this.getMaterialList();
  304. },
  305. materialDel(item) {
  306. const that = this
  307. this.$confirm('是否确认删除该素材?', '提示', {
  308. confirmButtonText: '确定',
  309. cancelButtonText: '取消',
  310. type: 'warning'
  311. }).then(function() {
  312. delMaterial(item.materialId)
  313. .then(function() {
  314. that.queryParams.pageNum=0;
  315. that.getMaterialList();
  316. })
  317. })
  318. },
  319. handleProgress(event, file, fileList) {
  320. },
  321. handleSuccess(response, file, fileList) {
  322. const that = this
  323. addMaterial({
  324. type: '1',
  325. groupId: this.queryParams.groupId,
  326. name: file.name,
  327. url: response.url
  328. }).then(() => {
  329. this.resultNumber++
  330. if (fileList.length === this.resultNumber) {
  331. that.getMaterialList()
  332. this.resultNumber = 0
  333. }
  334. })
  335. },
  336. beforeUpload(file) {
  337. const isPic =
  338. file.type === 'image/jpeg' ||
  339. file.type === 'image/png' ||
  340. file.type === 'image/gif' ||
  341. file.type === 'image/jpg'
  342. const isLt2M = file.size / 1024 / 1024 < 2
  343. if (!isPic) {
  344. this.$message.error('上传图片只能是 JPG、JPEG、PNG、GIF 格式!')
  345. return false
  346. }
  347. if (!isLt2M) {
  348. this.$message.error('上传头像图片大小不能超过 2MB!')
  349. }
  350. return isPic && isLt2M
  351. },
  352. submit() {
  353. this.urls.forEach(item => {
  354. this.$set(this.value, this.value.length, item)
  355. })
  356. this.listDialogVisible = false
  357. }
  358. }
  359. }
  360. </script>
  361. <style rel="stylesheet/scss" lang="scss" scoped>
  362. ::v-deep .el-icon-circle-close{
  363. color: red;
  364. }
  365. .material-name{
  366. padding: 8px 0px;
  367. }
  368. .col-do{
  369. text-align: center;
  370. }
  371. .button-do{
  372. padding: unset!important;
  373. font-size: 12px;
  374. }
  375. .group-list{
  376. display: flex;
  377. flex-direction:column;
  378. align-items: flex-start;
  379. }
  380. .group-item{
  381. margin: 5px;
  382. }
  383. </style>