index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <template>
  2. <!-- #ifdef H5 -->
  3. <view>
  4. <!-- ---须知--- -->
  5. <view>
  6. <u-popup :show="show" :overlay="false">
  7. <view class="pop">
  8. <view class="contentXZ"> <u-parse :content="content"></u-parse> </view><br /><br /><br /><br /><br />
  9. <view class="bottom-container">
  10. <u-button @click="handleAgree" text="我已知晓" color="#5298ff" shape="circle"></u-button>
  11. </view>
  12. </view>
  13. </u-popup>
  14. </view>
  15. <!-- ---------- -->
  16. <view v-if="!show" class="content">
  17. <u--form labelPosition="top" labelWidth="150" :model="form" :rules="rules" ref="form">
  18. <u-form-item label="商品名称" prop="title" required borderBottom ref="title">
  19. <u--input v-model="form.title" placeholder="请输入商品名称" border="none"></u--input>
  20. </u-form-item>
  21. <u-form-item label="商品描述" prop="content" required borderBottom ref="item1">
  22. <u--input v-model="form.content" placeholder="请输入" border="none"></u--input>
  23. </u-form-item>
  24. <u-form-item label="商品图片" prop="" required borderBottom ref="item1">
  25. <zj-upload key="cp" @getFiles="getFiles" :fileList="fileList" :count="9" />
  26. </u-form-item>
  27. <u-form-item labelPosition="left" required label="商品分类" prop="categoryId" borderBottom ref="item1">
  28. <u-radio-group v-model="form.categoryId" placement="column">
  29. <u-radio
  30. v-for="(v, i) in categoryList"
  31. :key="i"
  32. :customStyle="{ marginBottom: '8px' }"
  33. :label="v.categoryName"
  34. :name="v.categoryId"
  35. >
  36. </u-radio>
  37. </u-radio-group>
  38. </u-form-item>
  39. <u-form-item label="所在位置" labelPosition="left" prop="address" required borderBottom ref="item1">
  40. <view @click="handleDW" class="letText">
  41. <text v-if="form.address">{{ form.address }}</text>
  42. <text v-if="!form.address">请选择定位</text>
  43. <u-icon name="arrow-right"></u-icon>
  44. </view>
  45. </u-form-item>
  46. <u-form-item label="商品价格(含运费) " labelPosition="left" required prop="amount" borderBottom ref="item1">
  47. <u--input v-model="form.amount" inputAlign="right" placeholder="请输入数字" border="none"></u--input>
  48. </u-form-item>
  49. <u-form-item label="上架数量" labelPosition="left" required prop="num" borderBottom ref="item1">
  50. <u--input v-model="form.num" inputAlign="right" placeholder="请输入" border="none"></u--input>
  51. </u-form-item>
  52. <u-form-item label="商品品牌" labelPosition="left" prop="brand" borderBottom ref="item1">
  53. <u--input
  54. v-model="form.brand"
  55. inputAlign="right"
  56. placeholder="请输入(如格力、美的)"
  57. border="none"
  58. ></u--input>
  59. </u-form-item>
  60. <u-form-item label="功率(W)" labelPosition="left" prop="power" borderBottom ref="item1">
  61. <u--input v-model="form.power" inputAlign="right" placeholder="请输入" border="none"></u--input>
  62. </u-form-item>
  63. <u-form-item labelPosition="left" label="能效标识" prop="energy" borderBottom ref="item1">
  64. <u-radio-group v-model="form.energy" placement="column">
  65. <u-radio
  66. v-for="(v, i) in dictList"
  67. :key="i"
  68. :customStyle="{ marginBottom: '8px' }"
  69. :label="v.dictValue"
  70. :name="v.dictValue"
  71. >
  72. </u-radio>
  73. </u-radio-group>
  74. </u-form-item>
  75. <u-form-item label="制造日期" labelPosition="left" prop="goodsCreateTime" borderBottom ref="item1">
  76. <view class="letText" @click="handleTime">
  77. <text v-if="form.goodsCreateTime">{{
  78. form.goodsCreateTime.split('-')[0] + '-' + form.goodsCreateTime.split('-')[1] || ''
  79. }}</text>
  80. <text v-if="!form.goodsCreateTime">请选择</text>
  81. <u-icon name="arrow-right"></u-icon>
  82. </view>
  83. </u-form-item>
  84. </u--form>
  85. <view class="" style="padding: 50rpx 0">
  86. <u-button :disabled="disabled" @click="submin" text="发布" color="#5298ff" shape="circle"></u-button>
  87. </view>
  88. </view>
  89. <u-datetime-picker
  90. @cancel="handleCancelTime"
  91. @confirm="handleConfirmTime"
  92. :show="showTime"
  93. v-model="time"
  94. mode="year-month"
  95. ></u-datetime-picker>
  96. </view>
  97. <!-- #endif -->
  98. <!-- #ifndef H5 -->
  99. <web-view :src="webViewHref('/pages/issue/index')"></web-view>
  100. <!-- #endif -->
  101. </template>
  102. <script>
  103. import zjUpload from '@/components/zj-upload/index.vue'
  104. export default {
  105. // #ifdef H5
  106. components: { zjUpload },
  107. data() {
  108. return {
  109. disabled: false,
  110. showTime: false,
  111. show: true,
  112. form: {
  113. title: '',
  114. content: '',
  115. categoryName: '',
  116. categoryId: '',
  117. lng: '',
  118. lat: '',
  119. address: '',
  120. amount: '',
  121. num: '',
  122. brand: '',
  123. power: '',
  124. energy: '',
  125. province: '',
  126. city: '',
  127. area: '',
  128. street: '',
  129. goodsCreateTime: ''
  130. },
  131. rules: {
  132. title: [
  133. {
  134. required: true,
  135. message: '必传',
  136. trigger: ['change', 'blur']
  137. }
  138. ],
  139. content: [
  140. {
  141. required: true,
  142. message: '必传',
  143. trigger: ['change', 'blur']
  144. }
  145. ],
  146. categoryId: [
  147. {
  148. required: true,
  149. message: '必传',
  150. trigger: ['change', 'blur']
  151. }
  152. ],
  153. address: [
  154. {
  155. required: true,
  156. message: '必传',
  157. trigger: ['change', 'blur']
  158. }
  159. ],
  160. amount: [
  161. {
  162. required: true,
  163. message: '必传',
  164. trigger: ['change', 'blur']
  165. },
  166. {
  167. pattern: /^\d+\.?\d*$/g,
  168. // 正则检验前先将值转为字符串
  169. transform(value) {
  170. return String(value)
  171. },
  172. message: '格式不正确',
  173. trigger: ['change', 'blur']
  174. }
  175. ],
  176. num: [
  177. {
  178. required: true,
  179. message: '必传',
  180. trigger: ['change', 'blur']
  181. },
  182. {
  183. pattern: /^\d+$/g,
  184. // 正则检验前先将值转为字符串
  185. transform(value) {
  186. return String(value)
  187. },
  188. message: '格式不正确',
  189. trigger: ['change', 'blur']
  190. }
  191. ]
  192. },
  193. time: Number(new Date()),
  194. categoryList: [],
  195. dictList: [],
  196. fileList: [],
  197. fileurl: [],
  198. content: '',
  199. type: '',
  200. goodsId: ''
  201. }
  202. },
  203. onReady() {
  204. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  205. // this.$refs.form.setRules(this.rules)
  206. },
  207. onLoad({ type, goodsId }) {
  208. // console.log(type, goodsId)
  209. // this.type = type
  210. // this.goodsId = goodsId
  211. // if (goodsId) {
  212. // this.show = false
  213. // this.getDetail()
  214. // }
  215. },
  216. onShow() {
  217. const value = uni.getStorageSync('issuePageParam')
  218. if (value) {
  219. this.type = value.type
  220. this.goodsId = value.goodsId
  221. this.show = false
  222. this.getDetail()
  223. uni.removeStorageSync('issuePageParam');
  224. }
  225. this.getConfig()
  226. this.getList()
  227. this.getDictList()
  228. },
  229. watch: {
  230. 'form.address': {
  231. handler(nl) {
  232. this.$refs.form.clearValidate('address')
  233. }
  234. }
  235. },
  236. methods: {
  237. //
  238. getDetail() {
  239. this.$api
  240. .postJson('/goods/detail', {
  241. id: this.goodsId
  242. })
  243. .then(res => {
  244. console.log(res)
  245. this.form = {
  246. title: res.data.title,
  247. content: res.data.content,
  248. categoryName: res.data.categoryName,
  249. categoryId: res.data.categoryId,
  250. lng: res.data.lng,
  251. lat: res.data.lat,
  252. address: res.data.address,
  253. amount: String(res.data.amount),
  254. num: String(res.data.num),
  255. brand: res.data.brand,
  256. power: res.data.power,
  257. energy: res.data.energy,
  258. province: res.data.province,
  259. city: res.data.city,
  260. area: res.data.area,
  261. street: res.data.street,
  262. goodsCreateTime: res.data.goodsCreateTime
  263. }
  264. this.fileList = res.data.goodsFiles.map(v => v.imgUrl)
  265. this.fileurl = []
  266. res.data.goodsFiles.forEach(v => {
  267. this.fileurl.push({
  268. imgUrl: v.imgUrl
  269. })
  270. })
  271. })
  272. .catch(() => {})
  273. },
  274. getConfig() {
  275. this.$api
  276. .get('/app/config/detail-by-type', {
  277. configType: '4'
  278. })
  279. .then(res => {
  280. this.content = res.data.content
  281. })
  282. .catch(() => {})
  283. },
  284. getFiles(value) {
  285. this.fileurl = []
  286. value.forEach(v => {
  287. this.fileurl.push({
  288. imgUrl: v
  289. })
  290. })
  291. },
  292. submin() {
  293. this.$refs.form
  294. .validate()
  295. .then(valid => {
  296. if (valid) {
  297. this.disabled = true
  298. this.form.categoryName =
  299. this.categoryList.find(v => v.categoryId === this.form.categoryId)?.categoryName ?? ''
  300. let params = {
  301. ...this.form,
  302. goodsFiles: this.fileurl
  303. }
  304. this.$toast('发布成功')
  305. setTimeout(() => {
  306. let path = '/goods/add'
  307. if (this.goodsId && this.type == 1) {
  308. path = '/goods/updateUp'
  309. params.id = this.goodsId
  310. }
  311. if (this.goodsId && this.type == 2) {
  312. path = '/goods/update'
  313. params.id = this.goodsId
  314. }
  315. this.$api
  316. .post(path, params)
  317. .then(res => {
  318. this.form = {
  319. title: '',
  320. content: '',
  321. categoryName: '',
  322. categoryId: '',
  323. lng: '',
  324. lat: '',
  325. address: '',
  326. amount: '',
  327. num: '',
  328. brand: '',
  329. power: '',
  330. province: '',
  331. city: '',
  332. area: '',
  333. street: '',
  334. energy: '',
  335. goodsCreateTime: ''
  336. }
  337. this.$refs.form.clearValidate()
  338. this.disabled = false
  339. this.show = true
  340. this.$navToPage(
  341. {
  342. url: '/pages/index/index'
  343. },
  344. 'switchTab'
  345. )
  346. })
  347. .catch(err => {
  348. this.$toast(err)
  349. })
  350. }, 1000)
  351. }
  352. })
  353. .catch(err => {
  354. this.$toast('缺少必要参数,请检查')
  355. })
  356. return
  357. },
  358. //获取商品分类数据
  359. async getList() {
  360. this.$api
  361. .get('/goods/category/list')
  362. .then(res => {
  363. this.categoryList = res.data
  364. })
  365. .catch(() => {})
  366. },
  367. //获取能效数据
  368. async getDictList() {
  369. this.$api
  370. .get('/goods/dict', {
  371. dictCode: '能效'
  372. })
  373. .then(res => {
  374. this.dictList = res.data
  375. })
  376. .catch(() => {})
  377. },
  378. handleDW() {
  379. uni.chooseLocation({
  380. success: res => {
  381. this.form.lng = res.longitude
  382. this.form.lat = res.latitude
  383. this.form.address = res.address
  384. this.$api
  385. .post(`/lbs/amap/region/query?lng=${res.longitude}&lat=${res.latitude}`)
  386. .then(res => {
  387. console.log(res)
  388. this.form.province = res.data.provinceName
  389. this.form.city = res.data.cityName
  390. this.form.area = res.data.areaName
  391. this.form.street = res.data.name
  392. })
  393. .catch(() => {})
  394. }
  395. })
  396. },
  397. handleConfirmTime(value) {
  398. const date = new Date(value.value) // JavaScript的Date对象使用的是毫秒时间戳,因此需要将Unix时间戳乘以1000
  399. const year = date.getFullYear()
  400. const month = String(date.getMonth() + 1).padStart(2, '0') // getMonth()返回的月份是从0开始的,因此需要加1,然后使用padStart()添加前导零
  401. const day = String(date.getDate()).padStart(2, '0')
  402. const hours = String(date.getHours()).padStart(2, '0')
  403. const minutes = String(date.getMinutes()).padStart(2, '0')
  404. const seconds = String(date.getSeconds()).padStart(2, '0')
  405. let res = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  406. let res2 = `${year}-${month}`
  407. this.form.goodsCreateTime = res
  408. this.time = res2
  409. this.showTime = false
  410. },
  411. handleCancelTime() {
  412. this.showTime = false
  413. },
  414. handleTime() {
  415. this.showTime = true
  416. },
  417. handleAgree() {
  418. this.show = false
  419. }
  420. }
  421. // #endif
  422. }
  423. </script>
  424. <style lang="scss" scoped>
  425. .contentXZ {
  426. padding: 20px;
  427. }
  428. .pop {
  429. overflow: auto;
  430. // padding: 0 20rpx;
  431. /* #ifdef H5 */
  432. // height: calc(100vh - 44px);
  433. height: 100vh;
  434. /* #endif */
  435. /* #ifndef H5 */
  436. height: 100vh;
  437. /* #endif */
  438. }
  439. .content {
  440. padding: 0 40rpx;
  441. margin: 20rpx 0;
  442. // height: calc(100vh - 44px);
  443. background-color: #fff;
  444. }
  445. .bottom-container {
  446. position: fixed;
  447. bottom: 0;
  448. width: 100%;
  449. background-color: #f1f1f1;
  450. padding: 18px 0;
  451. }
  452. .letText {
  453. width: 100%;
  454. display: flex;
  455. justify-content: flex-end;
  456. }
  457. ::v-deep .u-radio-label--undefined {
  458. justify-content: flex-end;
  459. }
  460. </style>