brand.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <div class="app-container">
  3. <!-- 查询和其他操作 -->
  4. <div class="filter-container">
  5. <el-input v-model="listQuery.id" clearable class="filter-item" style="width: 200px;" placeholder="请输入品牌商ID"/>
  6. <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入品牌商名称"/>
  7. <el-button v-permission="['GET /admin/brand/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
  8. <el-button v-permission="['POST /admin/brand/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
  9. <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
  10. </div>
  11. <!-- 查询结果 -->
  12. <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中。。。" border fit highlight-current-row>
  13. <el-table-column align="center" label="品牌商ID" prop="id"/>
  14. <el-table-column align="center" label="品牌商名称" prop="name"/>
  15. <el-table-column align="center" property="picUrl" label="品牌商图片">
  16. <template slot-scope="scope">
  17. <img v-if="scope.row.picUrl" :src="scope.row.picUrl" width="80">
  18. </template>
  19. </el-table-column>
  20. <el-table-column align="center" min-width="400px" label="介绍" prop="desc"/>
  21. <el-table-column align="center" label="底价" prop="floorPrice"/>
  22. <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
  23. <template slot-scope="scope">
  24. <el-button v-permission="['POST /admin/brand/update']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
  25. <el-button v-permission="['POST /admin/brand/delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
  26. </template>
  27. </el-table-column>
  28. </el-table>
  29. <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  30. <!-- 添加或修改对话框 -->
  31. <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
  32. <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
  33. <el-form-item label="品牌商名称" prop="name">
  34. <el-input v-model="dataForm.name"/>
  35. </el-form-item>
  36. <el-form-item label="介绍" prop="simpleDesc">
  37. <el-input v-model="dataForm.desc"/>
  38. </el-form-item>
  39. <el-form-item label="品牌商图片" prop="picUrl">
  40. <el-upload
  41. :headers="headers"
  42. :action="uploadPath"
  43. :show-file-list="false"
  44. :on-success="uploadPicUrl"
  45. class="avatar-uploader"
  46. accept=".jpg,.jpeg,.png,.gif">
  47. <img v-if="dataForm.picUrl" :src="dataForm.picUrl" class="avatar">
  48. <i v-else class="el-icon-plus avatar-uploader-icon"/>
  49. </el-upload>
  50. </el-form-item>
  51. <el-form-item label="底价" prop="floorPrice">
  52. <el-input v-model="dataForm.floorPrice"/>
  53. </el-form-item>
  54. </el-form>
  55. <div slot="footer" class="dialog-footer">
  56. <el-button @click="dialogFormVisible = false">取消</el-button>
  57. <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
  58. <el-button v-else type="primary" @click="updateData">确定</el-button>
  59. </div>
  60. </el-dialog>
  61. </div>
  62. </template>
  63. <style>
  64. .avatar-uploader .el-upload {
  65. border: 1px dashed #d9d9d9;
  66. border-radius: 6px;
  67. cursor: pointer;
  68. position: relative;
  69. overflow: hidden;
  70. }
  71. .avatar-uploader .el-upload:hover {
  72. border-color: #20a0ff;
  73. }
  74. .avatar-uploader-icon {
  75. font-size: 28px;
  76. color: #8c939d;
  77. width: 120px;
  78. height: 120px;
  79. line-height: 120px;
  80. text-align: center;
  81. }
  82. .avatar {
  83. width: 145px;
  84. height: 145px;
  85. display: block;
  86. }
  87. </style>
  88. <script>
  89. import { listBrand, createBrand, updateBrand, deleteBrand } from '@/api/brand'
  90. import { uploadPath } from '@/api/storage'
  91. import { getToken } from '@/utils/auth'
  92. import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  93. export default {
  94. name: 'Brand',
  95. components: { Pagination },
  96. data() {
  97. return {
  98. uploadPath,
  99. list: undefined,
  100. total: 0,
  101. listLoading: true,
  102. listQuery: {
  103. page: 1,
  104. limit: 20,
  105. id: undefined,
  106. name: undefined,
  107. sort: 'add_time',
  108. order: 'desc'
  109. },
  110. dataForm: {
  111. id: undefined,
  112. name: '',
  113. desc: '',
  114. floorPrice: undefined,
  115. picUrl: undefined
  116. },
  117. dialogFormVisible: false,
  118. dialogStatus: '',
  119. textMap: {
  120. update: '编辑',
  121. create: '创建'
  122. },
  123. rules: {
  124. name: [
  125. { required: true, message: '品牌商名称不能为空', trigger: 'blur' }
  126. ]
  127. },
  128. downloadLoading: false
  129. }
  130. },
  131. computed: {
  132. headers() {
  133. return {
  134. 'X-Litemall-Admin-Token': getToken()
  135. }
  136. }
  137. },
  138. created() {
  139. this.getList()
  140. },
  141. methods: {
  142. getList() {
  143. this.listLoading = true
  144. listBrand(this.listQuery)
  145. .then(response => {
  146. this.list = response.data.data.items
  147. this.total = response.data.data.total
  148. this.listLoading = false
  149. })
  150. .catch(() => {
  151. this.list = []
  152. this.total = 0
  153. this.listLoading = false
  154. })
  155. },
  156. handleFilter() {
  157. this.listQuery.page = 1
  158. this.getList()
  159. },
  160. resetForm() {
  161. this.dataForm = {
  162. id: undefined,
  163. name: '',
  164. desc: '',
  165. floorPrice: undefined,
  166. picUrl: undefined
  167. }
  168. },
  169. handleCreate() {
  170. this.resetForm()
  171. this.dialogStatus = 'create'
  172. this.dialogFormVisible = true
  173. this.$nextTick(() => {
  174. this.$refs['dataForm'].clearValidate()
  175. })
  176. },
  177. uploadPicUrl: function(response) {
  178. this.dataForm.picUrl = response.data.url
  179. },
  180. createData() {
  181. this.$refs['dataForm'].validate(valid => {
  182. if (valid) {
  183. createBrand(this.dataForm)
  184. .then(response => {
  185. this.list.unshift(response.data.data)
  186. this.dialogFormVisible = false
  187. this.$notify.success({
  188. title: '成功',
  189. message: '创建成功'
  190. })
  191. })
  192. .catch(response => {
  193. this.$notify.error({
  194. title: '失败',
  195. message: response.data.errmsg
  196. })
  197. })
  198. }
  199. })
  200. },
  201. handleUpdate(row) {
  202. this.dataForm = Object.assign({}, row)
  203. this.dialogStatus = 'update'
  204. this.dialogFormVisible = true
  205. this.$nextTick(() => {
  206. this.$refs['dataForm'].clearValidate()
  207. })
  208. },
  209. updateData() {
  210. this.$refs['dataForm'].validate(valid => {
  211. if (valid) {
  212. updateBrand(this.dataForm)
  213. .then(() => {
  214. for (const v of this.list) {
  215. if (v.id === this.dataForm.id) {
  216. const index = this.list.indexOf(v)
  217. this.list.splice(index, 1, this.dataForm)
  218. break
  219. }
  220. }
  221. this.dialogFormVisible = false
  222. this.$notify.success({
  223. title: '成功',
  224. message: '更新成功'
  225. })
  226. })
  227. .catch(response => {
  228. this.$notify.error({
  229. title: '失败',
  230. message: response.data.errmsg
  231. })
  232. })
  233. }
  234. })
  235. },
  236. handleDelete(row) {
  237. deleteBrand(row)
  238. .then(response => {
  239. this.$notify.success({
  240. title: '成功',
  241. message: '删除成功'
  242. })
  243. const index = this.list.indexOf(row)
  244. this.list.splice(index, 1)
  245. })
  246. .catch(response => {
  247. this.$notify.error({
  248. title: '失败',
  249. message: response.data.errmsg
  250. })
  251. })
  252. },
  253. handleDownload() {
  254. this.downloadLoading = true
  255. import('@/vendor/Export2Excel').then(excel => {
  256. const tHeader = [
  257. '品牌商ID',
  258. '品牌商名称',
  259. '介绍',
  260. '低价',
  261. '品牌商图片'
  262. ]
  263. const filterVal = ['id', 'name', 'desc', 'floorPrice', 'picUrl']
  264. excel.export_json_to_excel2(
  265. tHeader,
  266. this.list,
  267. filterVal,
  268. '品牌商信息'
  269. )
  270. this.downloadLoading = false
  271. })
  272. }
  273. }
  274. }
  275. </script>