add.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. return this.form.adImg.join()
  127. }
  128. },
  129. onLoad(option) {
  130. this.setPageTitle()
  131. // this.init()
  132. },
  133. methods: {
  134. init() {
  135. this.type = this.option.type
  136. let data = uni.getStorageSync('addAdData')
  137. if (data) {
  138. uni.removeStorageSync('addAdData')
  139. setTimeout(() => {
  140. this.form = data.form
  141. // this.imgList = data.imgList
  142. })
  143. this.goodsData = data.goodsData
  144. }
  145. let goodsData = uni.getStorageSync('adSelGoods')
  146. if (goodsData) {
  147. uni.removeStorageSync('adSelGoods')
  148. this.goodsData = goodsData
  149. this.goodsId = goodsData.id
  150. }
  151. if (this.option.id && !data) {
  152. this._getDet()
  153. }
  154. },
  155. setPageTitle() {
  156. if (this.option.id) {
  157. uni.setNavigationBarTitle({
  158. title: this.option.type == 0 ? '修改门店轮播图' : '修改商城首页轮播图'
  159. })
  160. } else {
  161. uni.setNavigationBarTitle({
  162. title: this.option.type == 0 ? '添加门店轮播图' : '添加商城首页轮播图'
  163. })
  164. }
  165. },
  166. // api
  167. _getDet() {
  168. getDet({ id: this.option.id }).then(res => {
  169. if (this.$util.isEmpty(res.data)) return
  170. Object.keys(this.form).forEach((i, index) => {
  171. this.form[i] = res.data[i]
  172. })
  173. this.form.startTime = this.$util.$formatDate(this.form.startTime, 'YYYY-MM-DD')
  174. this.form.endTime = this.$util.$formatDate(this.form.endTime, 'YYYY-MM-DD')
  175. if (this.form.style) {
  176. this._getGoodsDet()
  177. }
  178. })
  179. },
  180. // 获取商品详情
  181. _getGoodsDet() {
  182. getGoodsDet({ id: this.form.goodsId }).then(res => {
  183. if (this.$util.isEmpty(res.data)) return
  184. this.goodsData = res.data
  185. })
  186. },
  187. // 操作
  188. // 选择商品
  189. selGoodsFn() {
  190. let data = {
  191. form: this.form,
  192. // imgList: this.imgList,
  193. goodsData: this.goodsData
  194. }
  195. uni.setStorageSync('addAdData', data)
  196. this.$util.pageTo({
  197. url: '/admin/ad/sel-goods',
  198. query: {
  199. ...this.option
  200. }
  201. })
  202. },
  203. startTimeFn(e) {
  204. console.log(e)
  205. this.form.startTime = e.detail.value
  206. },
  207. endTimeFn(e) {
  208. this.form.endTime = e.detail.value
  209. },
  210. switchChangeFn(e) {
  211. this.form.status = e.target.value ? 1 : 0
  212. },
  213. radioChange(e) {
  214. this.form.style = e.detail.value
  215. },
  216. confirmFn() {
  217. let hostFn = this.option.id ? update : add
  218. let form = JSON.parse(JSON.stringify(this.form))
  219. console.log(form)
  220. form.adImg = this.$util.imgSubstr(form.adImg)
  221. form.adImg = form.adImg.join()
  222. if (this.option.id) {
  223. form.id = this.option.id
  224. }
  225. if (form.style == 0) {
  226. delete form.goodsId
  227. } else {
  228. // form.url = ''
  229. delete form.url
  230. }
  231. hostFn(form).then(res => {
  232. let promptText = this.option.id ? '修改成功!' : '添加成功!'
  233. this.$msg(promptText)
  234. })
  235. },
  236. // 表单验证
  237. formSubmit(e) {
  238. // 表单规则
  239. let rules = [
  240. {
  241. name: 'adName',
  242. rule: ['required'],
  243. msg: ['请输入名称']
  244. },
  245. {
  246. name: 'startTime',
  247. rule: ['required'],
  248. msg: ['请选择开始时间']
  249. },
  250. {
  251. name: 'endTime',
  252. rule: ['required'],
  253. msg: ['请选择结束时间']
  254. },
  255. {
  256. name: 'adImgStr',
  257. rule: ['required'],
  258. msg: ['请先上传图片']
  259. }
  260. ]
  261. // 进行表单检查
  262. let formData = e.detail.value
  263. let checkRes = form.validation(formData, rules)
  264. // 验证通过!
  265. if (!checkRes) {
  266. this.confirmFn()
  267. } else {
  268. this.$msg(checkRes)
  269. }
  270. }
  271. }
  272. }
  273. </script>
  274. <style lang="scss" scoped>
  275. .app-content {
  276. min-height: calc(100vh - 20px);
  277. padding-top: 20px;
  278. }
  279. .module-com {
  280. margin-bottom: 20px;
  281. .line-img-wrap {
  282. display: block;
  283. .line-img {
  284. margin-top: 30px;
  285. }
  286. }
  287. .goods-blo {
  288. display: block;
  289. .goods-list-wrap {
  290. .goods-list {
  291. @include disFlex(center, flex-start);
  292. margin-bottom: 20px;
  293. .list-img {
  294. width: 120px;
  295. height: 120px;
  296. margin-right: 20px;
  297. img {
  298. height: 100%;
  299. }
  300. }
  301. .list-right {
  302. .app-price {
  303. margin-top: 10px;
  304. }
  305. }
  306. }
  307. }
  308. .admin-button-com {
  309. width: 125px;
  310. }
  311. }
  312. .ad-name {
  313. color: $fontColor2;
  314. margin-right: 20px;
  315. }
  316. }
  317. // 按钮
  318. .confirm-btn {
  319. width: calc(100% - 60px);
  320. margin: 60px 30px 20px;
  321. .admin-button-com {
  322. width: 100%;
  323. }
  324. }
  325. </style>