index.vue 7.7 KB

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