advertis-edit.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <div class="app-container" style="width: 800px; margin: 0 auto">
  3. <el-form ref="bannerForm" :model="form" label-width="130px">
  4. <el-form-item label="PC端图片:" prop="pcImage" :rules="rules.pcImage" style="margin-bottom: 40px">
  5. <div class="form-el-upload" style="width: 148px; height: 148px">
  6. <el-upload
  7. class="avatar-uploader"
  8. :action="actionUrl"
  9. :headers="getToken"
  10. :show-file-list="false"
  11. :on-success="handleSuccessPcImage"
  12. :before-upload="beforeUpload"
  13. >
  14. <div v-loading="loadPcImageLoading" class="avatar" style="width: 142px; height: 177px; display: block">
  15. <img
  16. v-if="form.pcImage"
  17. :src="form.pcImage"
  18. style="width: 142px; height: 177px; display: block"
  19. @error="reloadImage"
  20. @load="loadPcImageSucess"
  21. />
  22. <i
  23. v-else
  24. class="el-icon-plus avatar-uploader-icon"
  25. style="width: 142px; height: 177x; line-height: 177px"
  26. ></i>
  27. </div>
  28. </el-upload>
  29. <p class="uploader-tips">注:请尽量上传284*343(px)尺寸的图片。</p>
  30. </div>
  31. </el-form-item>
  32. <el-form-item label="小程序端图片:" prop="appImage" :rules="rules.appImage" style="margin-bottom: 40px">
  33. <div class="form-el-upload" style="width: 142px; height: 177px">
  34. <el-upload
  35. class="avatar-uploader"
  36. :action="actionUrl"
  37. :headers="getToken"
  38. :show-file-list="false"
  39. :on-success="handleSuccessAppImage"
  40. :before-upload="beforeUpload"
  41. >
  42. <div v-loading="loadImgLoading" class="avatar" style="width: 142px; height: 177px; display: block">
  43. <img
  44. v-if="form.appImage"
  45. :src="form.appImage"
  46. style="width: 153px; height: 230px; display: block"
  47. @error="reloadImage"
  48. @load="loadSucess"
  49. />
  50. <i
  51. v-else
  52. class="el-icon-plus avatar-uploader-icon"
  53. style="width: 153px; height: 230px; line-height: 177px"
  54. ></i>
  55. </div>
  56. </el-upload>
  57. <p class="uploader-tips">注:请尽量上传307*460(px)尺寸的图片。</p>
  58. </div>
  59. </el-form-item>
  60. <el-form-item label="供应商" prop="shops" :rules="rules.shops">
  61. <template v-if="form.shops && form.shops.length === 0">
  62. <el-button type="primary" autofocussize="mini" size="mini" icon="el-icon-plus" @click="handleDialogVisible">
  63. 选择供应商
  64. </el-button>
  65. <el-checkbox-group v-show="false" v-model="form.shops" />
  66. </template>
  67. <template v-else>
  68. <el-table :data="form.shops" border width="1000">
  69. <el-table-column prop="shopName" label="供应商名称" align="center" />
  70. <el-table-column prop="linkMan" label="联系人" align="center" />
  71. <el-table-column prop="mobile" label="手机号" align="center" />
  72. <el-table-column label="操作" align="center">
  73. <el-button type="text" @click="deleteShop"> 删除 </el-button>
  74. </el-table-column>
  75. </el-table>
  76. </template>
  77. </el-form-item>
  78. <el-form-item label="跳转链接:" prop="jumpLink" :rules="rules.jumpLink">
  79. <el-input v-model="form.jumpLink" placeholder="请输入跳转链接" maxlength="200" style="width: 600px" />
  80. </el-form-item>
  81. <el-form-item label="状态:" prop="status" :rules="rules.status">
  82. <el-select v-model="form.status" placeholder="请选择">
  83. <el-option label="上架" :value="0" />
  84. <el-option label="下架" :value="1" />
  85. </el-select>
  86. </el-form-item>
  87. </el-form>
  88. <div class="el-dialog__footer" style="text-align: center">
  89. <el-button type="primary" @click="onSubmit('bannerForm')"> 保存 </el-button>
  90. <el-button plain @click="backToList"> 返回 </el-button>
  91. </div>
  92. <!-- 供应商弹框 -->
  93. <shop-dialog v-if="shopDialogVisible" ref="shopDialog" @confirm="handleAddShopConfirm" @cancel="handleCancel" />
  94. </div>
  95. </template>
  96. <script>
  97. import { getShopAdvertisingImageById, saveShopAdvertisingImage } from '@/api/user/customer/customer'
  98. import ShopDialog from './components/shop-advertis-dialog'
  99. export default {
  100. name: 'AdvertisEdit',
  101. components: { ShopDialog },
  102. data() {
  103. const defaultForm = () => {
  104. return {
  105. id: '',
  106. shopId: this.$route.query.shopId,
  107. pcImage: '', // PC广告图
  108. appImage: '', // 小程序广告图
  109. jumpLink: '', // 跳转链接
  110. status: 0, // 上下架状态 0 上架 1 下架
  111. addTime: '', // 添加时间
  112. shops: []
  113. }
  114. }
  115. return {
  116. form: defaultForm(),
  117. editType: 'add',
  118. // 商品列表
  119. shopDialogVisible: false,
  120. loadImgLoading: false,
  121. loadPcImageLoading: false,
  122. rules: {
  123. pcImage: [{ required: true, message: '请上传PC广告图', trigger: 'blur' }],
  124. appImage: [{ required: true, message: '请上传小程序广告图', trigger: 'blur' }],
  125. shops: [{ required: true, type: 'array', message: '请添加供应商', trigger: ['change'] }],
  126. jumpLink: [{ required: true, message: '请填写跳转链接', trigger: 'blur' }],
  127. sort: [{ required: true, message: '排序值不能为空', trigger: 'blur' }],
  128. status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
  129. }
  130. }
  131. },
  132. computed: {
  133. getToken() {
  134. return {
  135. token: this.$store.getters.token
  136. }
  137. },
  138. actionUrl() {
  139. return process.env.VUE_APP_BASE_API + '/formData/MultiPictareaddData'
  140. }
  141. },
  142. created() {
  143. this.editType = this.$route.query.type || 'edit'
  144. if (this.editType === 'edit') {
  145. console.log('edit')
  146. this.getShopAdvertisingImageById(this.$route.query.id)
  147. }
  148. },
  149. methods: {
  150. async getShopAdvertisingImageById(id) {
  151. // 获取菜单详情
  152. try {
  153. const res = await getShopAdvertisingImageById(id)
  154. this.form = { ...this.form, ...res.data }
  155. } catch (error) {
  156. console.log('error', error)
  157. }
  158. },
  159. // 显示选择商品弹窗
  160. handleDialogVisible() {
  161. this.shopDialogVisible = true
  162. },
  163. handleGoodDialogVisible(index) {
  164. // 显示选择商品弹窗
  165. this.addIndex = index
  166. this.dialogGoodVisible = true
  167. },
  168. deleteShop() {
  169. // 清除供应商
  170. this.form.shopId = ''
  171. this.form.shops = []
  172. },
  173. handleAddShopConfirm(data) {
  174. // 确认选择商品
  175. this.form.shopId = data.shopId
  176. this.form.shops.push(data)
  177. this.handleCancel()
  178. },
  179. onSubmit(formName) {
  180. this.$refs[formName].validate((valid) => {
  181. if (valid) {
  182. this.$confirm('是否提交数据', '提示', {
  183. confirmButtonText: '确定',
  184. cancelButtonText: '取消',
  185. type: 'warning'
  186. }).then(() => {
  187. console.log('form', this.form)
  188. this.saveShopAdvertisingImage(this.form)
  189. })
  190. } else {
  191. return false
  192. }
  193. })
  194. },
  195. async saveShopAdvertisingImage(params) {
  196. // 保存
  197. await saveShopAdvertisingImage(params)
  198. this.$message.success('保存成功')
  199. setTimeout(() => {
  200. this.$router.push({ path: '/user/customer/advertis-list' })
  201. }, 1000)
  202. },
  203. // 取消选择商品
  204. handleCancel() {
  205. this.shopDialogVisible = false
  206. this.$refs.shopDialog.visible = false
  207. },
  208. // 上传PC广告图事件
  209. handleSuccessPcImage(response, file) {
  210. this.loadPcImageLoading = true
  211. this.$nextTick(() => {
  212. setTimeout(() => {
  213. this.form.pcImage = response.data
  214. }, 1000 * 2)
  215. })
  216. },
  217. // 上传小程序广告图事件
  218. handleSuccessAppImage(response, file) {
  219. this.loadImgLoading = true
  220. this.$nextTick(() => {
  221. setTimeout(() => {
  222. this.form.appImage = response.data
  223. }, 1000 * 2)
  224. })
  225. },
  226. // 对上传图片的大小、格式进行限制
  227. beforeUpload(file) {
  228. const isJPG = file.type === 'image/jpeg'
  229. const isJPG2 = file.type === 'image/jpg'
  230. const isPNG = file.type === 'image/png'
  231. const isLt5M = file.size / 1024 / 1024 < 5
  232. if (!isJPG && !isJPG2 && !isPNG) {
  233. this.$message.error('只支持jpg或png格式图片')
  234. }
  235. if (!isLt5M) {
  236. this.$message.error('请上传5MB以内的图片!')
  237. }
  238. return (isJPG || isJPG2 || isPNG) && isLt5M
  239. },
  240. reloadImage() {
  241. this.loadImgLoading = true
  242. setTimeout(() => {
  243. this.temp.classifyImage = this.temp.classifyImage.split('?')[0] + '?r=' + Math.floor(Math.random() * 1000)
  244. }, 1000 * 2)
  245. },
  246. loadPcImageSucess() {
  247. this.loadPcImageLoading = false
  248. },
  249. loadSucess() {
  250. this.loadImgLoading = false
  251. },
  252. backToList() {
  253. this.$store.dispatch('tagsView/delView', this.$route).then(() => {
  254. this.$nextTick(() => {
  255. this.$router.replace({ path: '/user/customer/advertis-list' })
  256. })
  257. })
  258. }
  259. }
  260. }
  261. </script>
  262. <style>
  263. .avatar-uploader .el-upload {
  264. border: 1px dashed #d9d9d9;
  265. border-radius: 6px;
  266. cursor: pointer;
  267. position: relative;
  268. overflow: hidden;
  269. float: left;
  270. }
  271. .avatar-uploader .el-upload:hover {
  272. border-color: #409eff;
  273. }
  274. .avatar-uploader-icon {
  275. font-size: 30px;
  276. color: #999999;
  277. }
  278. .el-form-item__label {
  279. text-align: right !important;
  280. }
  281. .el-upload__tip {
  282. line-height: 20px;
  283. color: red;
  284. text-align: left;
  285. position: absolute;
  286. right: -50%;
  287. bottom: 0;
  288. }
  289. .span_tip {
  290. font-size: 12px;
  291. color: red;
  292. margin-left: 5px;
  293. }
  294. .filter-item-temp {
  295. width: 100px;
  296. }
  297. .uploader-tips {
  298. position: absolute;
  299. bottom: 0;
  300. left: 160px;
  301. line-height: 28px;
  302. color: red;
  303. margin: 0;
  304. }
  305. </style>