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