index.vue 7.0 KB

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