add.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <div class="app-content">
  3. <!-- 广告信息 -->
  4. <form @submit="formSubmit">
  5. <div class="module-com input-line-wrap">
  6. <tui-list-cell class="line-cell" :hover="false">
  7. <div class="tui-title required">广告图名称</div>
  8. <input v-model="form.adName" placeholder-class="phcolor" class="tui-input" name="adName" placeholder="请输入名称" maxlength="50" type="text" />
  9. </tui-list-cell>
  10. <tui-list-cell class="line-cell" :arrow="true">
  11. <div class="tui-title required">开始时间</div>
  12. <picker mode="date" :value="form.startTime" @change="startTimeFn" class="tui-input">
  13. <input v-model="form.startTime" name="startTime" type="text" hidden />
  14. <div v-if="form.startTime">{{ form.startTime }}</div>
  15. <div class="tui-placeholder" v-else>请选择</div>
  16. </picker>
  17. </tui-list-cell>
  18. <tui-list-cell class="line-cell" :arrow="true">
  19. <div class="tui-title required">到期时间</div>
  20. <picker mode="date" :value="form.endTime" @change="endTimeFn" class="tui-input">
  21. <input v-model="form.endTime" name="endTime" type="text" hidden />
  22. <div v-if="form.endTime">{{ form.endTime }}</div>
  23. <div class="tui-placeholder" v-else>请选择</div>
  24. </picker>
  25. </tui-list-cell>
  26. <tui-list-cell class="line-cell between switch" padding="14rpx 30rpx" :hover="false">
  27. <div class="tui-title">马上开启</div>
  28. <div>
  29. <switch :checked="form.status ? true : false" @change="switchChangeFn" />
  30. </div>
  31. </tui-list-cell>
  32. </div>
  33. <!-- 广告图片 -->
  34. <div class="module-com input-line-wrap">
  35. <tui-list-cell class="line-img-wrap" :hover="false">
  36. <div>
  37. <span class="ad-name required">广告图片</span>
  38. <span class="app-color-3 app-size-24">只能上传jpg/png格式</span>
  39. </div>
  40. <div class="line-img">
  41. <input :value="adImgStr" name="adImgStr" type="text" hidden />
  42. <app-uploader :multiple="false" ref="appUploader" :imgList.sync="form.adImg" />
  43. </div>
  44. </tui-list-cell>
  45. </div>
  46. <!-- 链接方式 -->
  47. <div class="module-com input-line-wrap">
  48. <tui-list-cell class="line-cell" :hover="false">
  49. <div class="tui-title required">链接方式</div>
  50. <radio-group class="tui-input flex-center-between" @change="radioChange">
  51. <label class="list" for="customLink">
  52. <radio id="customLink" value="0" :checked="form.style == 0" />
  53. <span class="checkbox-text">自定义链接</span>
  54. </label>
  55. <label class="list" for="goodsLink">
  56. <radio id="goodsLink" value="1" :checked="form.style == 1" />
  57. <span class="checkbox-text">链接商品</span>
  58. </label>
  59. </radio-group>
  60. </tui-list-cell>
  61. <block v-if="form.style == 0">
  62. <tui-list-cell class="line-cell" :hover="false">
  63. <textarea v-model="form.url" class="tui-textarea" placeholder-class="phcolor" placeholder="请填写链接,留空点图片不跳转" auto-height />
  64. </tui-list-cell>
  65. </block>
  66. <block v-else>
  67. <tui-list-cell class="goods-blo" :hover="false">
  68. <div class="goods-list-wrap" v-if="!$util.isEmpty(goodsData)">
  69. <div class="goods-list">
  70. <div class="list-img">
  71. <img :src="goodsData.smallImgList[0]" alt="商品图片" mode="center" />
  72. </div>
  73. <div class="list-right">
  74. <div>{{ goodsData.goodsName }}</div>
  75. <div class="app-price">¥{{ goodsData.price }}</div>
  76. </div>
  77. </div>
  78. </div>
  79. <div class="admin-button-com blue" @click="selGoodsFn">选择商品</div>
  80. </tui-list-cell>
  81. </block>
  82. </div>
  83. <!-- 确认 -->
  84. <div class="confirm-btn">
  85. <button class="admin-button-com big blue" formType="submit">确认</button>
  86. </div>
  87. </form>
  88. </div>
  89. </template>
  90. <script>
  91. import TuiListCell from '@/components/plugin/list-cell'
  92. import AppUploader from '@/components/app-uploader'
  93. import AppDatePicker from '@/components/app-date-picker'
  94. const form = require('@/utils/formValidation.js')
  95. // api
  96. import { getDet, add, update } from '@/api/ad'
  97. import { getDetailB as getGoodsDet } from '@/api/goods'
  98. export default {
  99. name: 'ad-add',
  100. components: {
  101. TuiListCell,
  102. AppUploader,
  103. AppDatePicker
  104. },
  105. data() {
  106. return {
  107. form: {
  108. adName: '',
  109. adImg: [],
  110. status: 0,
  111. inTurn: '1',
  112. type: 0,
  113. style: 0,
  114. url: '',
  115. goodsId: '',
  116. startTime: 0,
  117. endTime: 0
  118. },
  119. // imgList: [],
  120. goodsData: {}
  121. }
  122. },
  123. computed: {
  124. adImgStr() {
  125. // let img = JSON
  126. if (this.$util.isArray(this.form.adImg)) {
  127. return this.form.adImg.join()
  128. } else {
  129. return this.form.adImg
  130. }
  131. }
  132. },
  133. onLoad(option) {
  134. this.setPageTitle()
  135. // this.init()
  136. },
  137. methods: {
  138. init() {
  139. this.type = this.option.type
  140. let data = uni.getStorageSync('addAdData')
  141. if (data) {
  142. uni.removeStorageSync('addAdData')
  143. setTimeout(() => {
  144. this.form = data.form
  145. // this.imgList = data.imgList
  146. })
  147. this.goodsData = data.goodsData
  148. this.getFormAdImg(data.goodsData)
  149. }
  150. let goodsData = uni.getStorageSync('adSelGoods')
  151. if (goodsData) {
  152. uni.removeStorageSync('adSelGoods')
  153. this.goodsData = goodsData
  154. this.goodsId = goodsData.id
  155. this.getFormAdImg(data.goodsData)
  156. }
  157. if (this.option.id && !data) {
  158. this._getDet()
  159. }
  160. },
  161. setPageTitle() {
  162. if (this.option.id) {
  163. uni.setNavigationBarTitle({
  164. title: this.option.type == 0 ? '修改门店轮播图' : '修改商城首页轮播图'
  165. })
  166. } else {
  167. uni.setNavigationBarTitle({
  168. title: this.option.type == 0 ? '添加门店轮播图' : '添加商城首页轮播图'
  169. })
  170. }
  171. },
  172. getFormAdImg(data) {
  173. if (this.$util.isArray(data.adImg)) {
  174. this.form.adImg = data.adImg
  175. } else {
  176. this.form.adImg = [data.adImg]
  177. }
  178. },
  179. // api
  180. _getDet() {
  181. getDet({ id: this.option.id }).then(res => {
  182. if (this.$util.isEmpty(res.data)) return
  183. Object.keys(this.form).forEach((i, index) => {
  184. if (i == 'adImg') {
  185. this.form.adImg = [res.data.adImgUrl]
  186. } else {
  187. this.form[i] = res.data[i]
  188. }
  189. })
  190. this.form.startTime = this.$util.$formatDate(this.form.startTime, 'YYYY-MM-DD')
  191. this.form.endTime = this.$util.$formatDate(this.form.endTime, 'YYYY-MM-DD')
  192. if (this.form.style && this.form.style != '0') {
  193. this._getGoodsDet()
  194. }
  195. })
  196. },
  197. // 获取商品详情
  198. _getGoodsDet() {
  199. getGoodsDet({ id: this.form.goodsId }).then(res => {
  200. if (this.$util.isEmpty(res.data)) return
  201. this.goodsData = res.data
  202. })
  203. },
  204. // 操作
  205. // 选择商品
  206. selGoodsFn() {
  207. let data = {
  208. form: this.form,
  209. // imgList: this.imgList,
  210. goodsData: this.goodsData
  211. }
  212. uni.setStorageSync('addAdData', data)
  213. this.$util.pageTo({
  214. url: '/admin/ad/sel-goods',
  215. query: {
  216. ...this.option
  217. }
  218. })
  219. },
  220. startTimeFn(e) {
  221. console.log(e)
  222. this.form.startTime = e.detail.value
  223. },
  224. endTimeFn(e) {
  225. this.form.endTime = e.detail.value
  226. },
  227. switchChangeFn(e) {
  228. this.form.status = e.target.value ? 1 : 0
  229. },
  230. radioChange(e) {
  231. this.form.style = e.detail.value
  232. },
  233. confirmFn() {
  234. let hostFn = this.option.id ? update : add
  235. let form = JSON.parse(JSON.stringify(this.form))
  236. console.log(form)
  237. form.adImg = this.$util.imgSubstr(form.adImg)
  238. form.adImg = form.adImg.join()
  239. if (this.option.id) {
  240. form.id = this.option.id
  241. }
  242. if (form.style == 0) {
  243. delete form.goodsId
  244. } else {
  245. // form.url = ''
  246. delete form.url
  247. }
  248. hostFn(form).then(res => {
  249. let promptText = this.option.id ? '修改成功!' : '添加成功!'
  250. this.$msg(promptText)
  251. })
  252. },
  253. // 表单验证
  254. formSubmit(e) {
  255. // 表单规则
  256. let rules = [
  257. {
  258. name: 'adName',
  259. rule: ['required'],
  260. msg: ['请输入名称']
  261. },
  262. {
  263. name: 'startTime',
  264. rule: ['required'],
  265. msg: ['请选择开始时间']
  266. },
  267. {
  268. name: 'endTime',
  269. rule: ['required'],
  270. msg: ['请选择结束时间']
  271. },
  272. {
  273. name: 'adImgStr',
  274. rule: ['required'],
  275. msg: ['请先上传图片']
  276. }
  277. ]
  278. // 进行表单检查
  279. let formData = e.detail.value
  280. let checkRes = form.validation(formData, rules)
  281. // 验证通过!
  282. if (!checkRes) {
  283. this.confirmFn()
  284. } else {
  285. this.$msg(checkRes)
  286. }
  287. }
  288. }
  289. }
  290. </script>
  291. <style lang="scss" scoped>
  292. .app-content {
  293. min-height: calc(100vh - 20px);
  294. padding-top: 20px;
  295. }
  296. .module-com {
  297. margin-bottom: 20px;
  298. .line-img-wrap {
  299. display: block;
  300. .line-img {
  301. margin-top: 30px;
  302. }
  303. }
  304. .goods-blo {
  305. display: block;
  306. .goods-list-wrap {
  307. .goods-list {
  308. @include disFlex(center, flex-start);
  309. margin-bottom: 20px;
  310. .list-img {
  311. width: 120px;
  312. height: 120px;
  313. margin-right: 20px;
  314. img {
  315. height: 100%;
  316. }
  317. }
  318. .list-right {
  319. .app-price {
  320. margin-top: 10px;
  321. }
  322. }
  323. }
  324. }
  325. .admin-button-com {
  326. width: 125px;
  327. }
  328. }
  329. .ad-name {
  330. color: $fontColor2;
  331. margin-right: 20px;
  332. }
  333. }
  334. // 按钮
  335. .confirm-btn {
  336. width: calc(100% - 60px);
  337. margin: 60px 30px 20px;
  338. .admin-button-com {
  339. width: 100%;
  340. }
  341. }
  342. </style>