index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <div class="app-container">
  3. <div class="mymain-container">
  4. <div class="btn-group clearfix">
  5. <div class="fl">
  6. <el-button v-if="$restrict('add')" size="small" type="primary" icon="el-icon-plus" @click="addOrEdit('add')">添加品牌</el-button>
  7. </div>
  8. </div>
  9. <div class="table">
  10. <el-table ref="noticeTable" v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
  11. <el-table-column align="center" label="服务品牌" prop="brandName" min-width="180"></el-table-column>
  12. <el-table-column align="center" label="图片" prop="imageUrl">
  13. <template slot-scope="scope">
  14. <el-image style="width: 40px; height: 40px; display: block; margin: 0 auto;" :src="scope.row.imageUrl" :preview-src-list="[scope.row.imageUrl]" fit="cover">
  15. <div slot="error" style="height: 100%;font-size: 40px;">
  16. <i class="el-icon-picture-outline"></i>
  17. </div>
  18. </el-image>
  19. </template>
  20. </el-table-column>
  21. <el-table-column align="center" label="状态" class-name="status-col">
  22. <template slot-scope="scope">
  23. <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? '开启' : '禁用'}}</el-tag>
  24. </template>
  25. </el-table-column>
  26. <el-table-column align="center" label="创建人" prop="createBy" min-width="100"></el-table-column>
  27. <el-table-column align="center" label="创建时间" prop="createTime" min-width="160"></el-table-column>
  28. <el-table-column align="center" label="修改人" prop="updateBy" min-width="100"></el-table-column>
  29. <el-table-column align="center" label="修改时间" prop="updateTime" min-width="160"></el-table-column>
  30. <el-table-column align="center" label="操作" fixed="right" width="120">
  31. <template slot-scope="scope">
  32. <el-button type="text" v-if="$restrict('edit')" @click="addOrEdit('edit', scope.row.id)">编辑</el-button>
  33. <el-popconfirm v-if="$restrict('del')" style="margin-left: 10px;" title="确定删除吗?" @confirm="handleDelete(scope.row.id)">
  34. <el-button slot="reference" type="text">删除</el-button>
  35. </el-popconfirm>
  36. </template>
  37. </el-table-column>
  38. </el-table>
  39. </div>
  40. </div>
  41. <!-- 新增编辑 -->
  42. <el-dialog :title="addFormType === 'add' ? '新增品牌':'编辑品牌'" :visible.sync="addFormVisible" :show-close="false" width="50%" :close-on-click-modal="false">
  43. <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="left" label-width="80px">
  44. <el-form-item label="品牌名称" prop="name">
  45. <el-input v-model="addForm.name" autocomplete="off" placeholder="请输入品牌名称"></el-input>
  46. </el-form-item>
  47. <el-form-item label="图片" prop="imgUrl">
  48. <el-upload
  49. class="avatar-uploader"
  50. :action="baseURL + 'common/upload'"
  51. :headers="myHeaders"
  52. :show-file-list="false"
  53. :on-success="uploadSuccess"
  54. :before-upload="beforeUpload">
  55. <img v-if="addForm.imgUrl" :src="addForm.imgUrl" class="avatar">
  56. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  57. </el-upload>
  58. </el-form-item>
  59. <el-form-item label="状态" prop="status">
  60. <el-radio-group v-model="addForm.status">
  61. <el-radio :label="true">开启</el-radio>
  62. <el-radio :label="false">禁用</el-radio>
  63. </el-radio-group>
  64. </el-form-item>
  65. </el-form>
  66. <div slot="footer" class="dialog-footer">
  67. <el-button @click="cancelAddForm">取 消</el-button>
  68. <el-button type="primary" @click="submitAddForm">确 定</el-button>
  69. </div>
  70. </el-dialog>
  71. </div>
  72. </template>
  73. <script>
  74. import { getBrandList, getBrandDetail, saveBrand, deleteBrand } from "@/api/miniapp";
  75. import { getToken } from '@/utils/auth'
  76. export default {
  77. data() {
  78. return {
  79. baseURL: process.env.VUE_APP_BASE_API,
  80. myHeaders: {'x-token': getToken()},
  81. dataList: null, // 列表数据
  82. listLoading: true, // 列表加载loading
  83. editId: null,
  84. addFormType: 'add',
  85. addFormVisible: false,
  86. addForm: {
  87. name: '', // 名称
  88. imgUrl: '', // 图片
  89. status: true,
  90. },
  91. addFormRules: {
  92. name: [
  93. { required: true, message: '请输入品牌名称', trigger: 'blur' }
  94. ],
  95. imgUrl: [
  96. { required: true, message: '请上传图片', trigger: 'change' }
  97. ],
  98. status: [
  99. { required: true, message: '请选择状态', trigger: 'change' }
  100. ],
  101. },
  102. }
  103. },
  104. created() {
  105. this.getList();
  106. },
  107. methods: {
  108. getList() {
  109. getBrandList().then(res => {
  110. this.dataList = res.data;
  111. this.listLoading = false;
  112. })
  113. },
  114. // 新增编辑
  115. addOrEdit(type, id) {
  116. this.addFormType = type;
  117. this.addFormVisible = true;
  118. if(type == 'edit') {
  119. this.editId = id;
  120. getBrandDetail({id: id}).then(res => {
  121. this.addForm = {
  122. name: res.data.brandName,
  123. imgUrl: res.data.imageUrl,
  124. status: res.data.status,
  125. }
  126. })
  127. }
  128. },
  129. // 取消 新增编辑
  130. cancelAddForm(){
  131. this.addFormVisible = false;
  132. this.$refs.addForm.resetFields();
  133. this.selGoodsName = ''
  134. },
  135. // 提交 新增编辑
  136. submitAddForm() {
  137. this.$refs.addForm.validate((valid) => {
  138. if (valid) {
  139. let params = {
  140. brandName: this.addForm.name,
  141. imageUrl: this.addForm.imgUrl,
  142. status: this.addForm.status,
  143. }
  144. if(this.addFormType == 'edit') {
  145. params.id = this.editId;
  146. }
  147. saveBrand(params).then(res => {
  148. this.cancelAddForm();
  149. this.getList();
  150. this.$successMsg('保存成功');
  151. })
  152. }
  153. })
  154. },
  155. uploadSuccess(res, file) {
  156. this.addForm.imgUrl = res.data.url;
  157. },
  158. beforeUpload(file) {
  159. const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
  160. const whiteList = ['jpg', 'jpeg', 'png'];
  161. if (whiteList.indexOf(fileSuffix) === -1) {
  162. this.$errorMsg('只支持上传jpg/png文件!');
  163. return false;
  164. }
  165. },
  166. // 操作 - 删除
  167. handleDelete(id) {
  168. deleteBrand({id: id}).then(res => {
  169. this.getList();
  170. this.$successMsg();
  171. })
  172. },
  173. }
  174. }
  175. </script>
  176. <style scoped lang="scss">
  177. </style>