indexImg-add-or-update.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <div class="mod-index-img">
  3. <el-dialog :title="!dataForm.imgId ? '新增' : '修改'"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible">
  6. <el-form :model="dataForm"
  7. ref="dataForm"
  8. :rules="dataRule"
  9. label-width="100px">
  10. <el-form-item label="轮播图片" prop="imgUrl">
  11. <pic-upload v-model="dataForm.imgUrl"></pic-upload>
  12. </el-form-item>
  13. <el-form-item label="顺序"
  14. prop="seq"
  15. :rules="[
  16. { required: false, pattern: /\s\S+|S+\s|\S/, message: '请输入正确的顺序', trigger: 'blur' }
  17. ]">
  18. <el-input v-model="dataForm.seq"></el-input>
  19. </el-form-item>
  20. <el-form-item label="状态"
  21. prop="status">
  22. <el-radio-group v-model="dataForm.status">
  23. <el-radio :label="0">禁用</el-radio>
  24. <el-radio :label="1">正常</el-radio>
  25. </el-radio-group>
  26. </el-form-item>
  27. <el-form-item label="类型">
  28. <el-radio-group v-model="dataForm.type"
  29. @change="deleteRelation">
  30. <el-radio :label="-1">无</el-radio>
  31. <el-radio :label="0">商品</el-radio>
  32. <!-- <el-radio :label="1">店铺</el-radio>
  33. <el-radio :label="2">活动</el-radio> -->
  34. </el-radio-group>
  35. <div v-if="dataForm.relation!=null">
  36. <el-card :body-style="{ padding: '0px' }"
  37. style="height: 160px;width: 120px">
  38. <img :src="card.pic"
  39. style="height:104px;width:100%">
  40. <div class="card-prod-bottom">
  41. <span class="card-prod-name">{{card.name}}</span>
  42. <el-button type="text"
  43. class="card-prod-name-button"
  44. @click="deleteRelation">删除</el-button>
  45. </div>
  46. </el-card>
  47. </div>
  48. <div v-if="dataForm.relation==null">
  49. <el-button @click="addProd"
  50. v-if=" dataForm.type == 0"
  51. size="small">选择商品</el-button>
  52. <!-- <el-button @click="addShop"
  53. v-if=" dataForm.type == 1"
  54. size="small">选择店铺</el-button>
  55. <el-button @click="addActivity"
  56. v-if="dataForm.type == 2"
  57. size="small">选择活动</el-button> -->
  58. </div>
  59. </el-form-item>
  60. <el-form-item>
  61. <el-button type="primary"
  62. @click="dataFormSubmit()">确定</el-button>
  63. </el-form-item>
  64. </el-form>
  65. </el-dialog>
  66. <!-- 商品选择弹窗-->
  67. <prods-select v-if="prodsSelectVisible"
  68. ref="prodsSelect"
  69. :isSingle="true"
  70. @refreshSelectProds="selectCouponProds"></prods-select>
  71. </div>
  72. </template>
  73. <script>
  74. import PicUpload from '@/components/pic-upload'
  75. import ProdsSelect from '@/components/prods-select'
  76. import { Debounce } from '@/utils/debounce'
  77. export default {
  78. data () {
  79. return {
  80. dataForm: {
  81. status: 1,
  82. des: '',
  83. imgUrl: '',
  84. seq: 0,
  85. imgId: 0,
  86. type: -1,
  87. relation: null
  88. },
  89. dataRule: {
  90. imgUrl: [
  91. {required: true, message: '轮播图片不能为空', trigger: 'blur'}
  92. ]
  93. },
  94. // 关联数据
  95. card: {
  96. id: 0,
  97. pic: '',
  98. name: '',
  99. realData: {
  100. prod: [],
  101. shop: [],
  102. activity: []
  103. }
  104. },
  105. page: {
  106. total: 0, // 总页数
  107. currentPage: 1, // 当前页数
  108. pageSize: 10 // 每页显示多少条
  109. },
  110. prodsSelectVisible: false,
  111. visible: false
  112. }
  113. },
  114. components: {
  115. PicUpload,
  116. ProdsSelect
  117. },
  118. methods: {
  119. // 获取分类数据
  120. init (id) {
  121. this.visible = true
  122. this.dataForm.imgId = id || 0
  123. if (this.dataForm.imgId) {
  124. // 获取产品数据
  125. this.$http({
  126. url: this.$http.adornUrl(`/admin/indexImg/info/${this.dataForm.imgId}`),
  127. method: 'get',
  128. params: this.$http.adornParams()
  129. }).then(({ data }) => {
  130. this.dataForm = data
  131. if (data.relation) {
  132. this.card.pic = data.pic
  133. this.card.name = data.prodName
  134. this.card.id = data.relation
  135. }
  136. })
  137. } else {
  138. this.$nextTick(() => {
  139. this.$refs['dataForm'].resetFields()
  140. this.dataForm.imgUrl = ''
  141. this.relation = null
  142. })
  143. }
  144. },
  145. // 表单提交
  146. dataFormSubmit: Debounce(function () {
  147. this.$refs['dataForm'].validate((valid) => {
  148. if (!valid) {
  149. return
  150. }
  151. let param = this.dataForm
  152. this.$http({
  153. url: this.$http.adornUrl(`/admin/indexImg`),
  154. method: param.imgId ? 'put' : 'post',
  155. data: this.$http.adornData(param)
  156. }).then(({ data }) => {
  157. this.$message({
  158. message: '操作成功',
  159. type: 'success',
  160. duration: 1500,
  161. onClose: () => {
  162. this.visible = false
  163. this.$emit('refreshDataList', this.page)
  164. }
  165. })
  166. })
  167. })
  168. }),
  169. // 删除关联数据
  170. deleteRelation () {
  171. this.dataForm.relation = null
  172. },
  173. // 打开选择商品
  174. addProd () {
  175. this.prodsSelectVisible = true
  176. this.$nextTick(() => {
  177. this.$refs.prodsSelect.init(this.card.realData.prod)
  178. })
  179. },
  180. // 添加指定商品
  181. selectCouponProds (prods) {
  182. this.card.realData.prods = prods
  183. if (prods.length) {
  184. let selectProd = prods[0]
  185. this.dataForm.relation = selectProd.prodId
  186. this.card.pic = selectProd.pic
  187. this.card.name = selectProd.prodName
  188. this.card.id = selectProd.prodId
  189. } else {
  190. this.card = {}
  191. this.relation = null
  192. }
  193. },
  194. addShop () {
  195. alert('选择店铺')
  196. },
  197. addActivity () {
  198. alert('选择活动')
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss">
  204. //card样式
  205. .card-prod-bottom {
  206. position: relative;
  207. text-align: left;
  208. .card-prod-name {
  209. margin: auto;
  210. padding: 0 6px;
  211. overflow: hidden;
  212. text-overflow: ellipsis;
  213. white-space: nowrap;
  214. width: 118px;
  215. display: inline-block;
  216. }
  217. .card-prod-name-button {
  218. position: absolute;
  219. top: 24px;
  220. right: 10px;
  221. }
  222. }
  223. </style>