index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. <view>
  100. <web-view v-if="isShow" :src="webViewHref('/pages/issue/index', pam)"></web-view>
  101. </view>
  102. <!-- #endif -->
  103. </template>
  104. <script>
  105. import zjUpload from '@/components/zj-upload/index.vue'
  106. export default {
  107. // #ifndef H5
  108. data(){
  109. return {
  110. isShow:false,
  111. pam:{},
  112. init:true
  113. }
  114. },
  115. onShow(){
  116. this.pam = uni.getStorageSync('issuePageParam')
  117. uni.removeStorageSync('issuePageParam');
  118. if(this.pam.isLoad || this.init){
  119. this.init=false
  120. delete this.pam.isLoad
  121. this.isShow = false
  122. this.$nextTick(()=>{
  123. this.isShow = true
  124. })
  125. }
  126. }
  127. // #endif
  128. // #ifdef H5
  129. components: { zjUpload },
  130. data() {
  131. return {
  132. disabled: false,
  133. showTime: false,
  134. show: true,
  135. form: {
  136. title: '',
  137. content: '',
  138. categoryName: '',
  139. categoryId: '',
  140. lng: '',
  141. lat: '',
  142. address: '',
  143. amount: '',
  144. num: '',
  145. brand: '',
  146. power: '',
  147. energy: '',
  148. province: '',
  149. city: '',
  150. area: '',
  151. street: '',
  152. goodsCreateTime: ''
  153. },
  154. rules: {
  155. title: [
  156. {
  157. required: true,
  158. message: '必传',
  159. trigger: ['change', 'blur']
  160. }
  161. ],
  162. content: [
  163. {
  164. required: true,
  165. message: '必传',
  166. trigger: ['change', 'blur']
  167. }
  168. ],
  169. categoryId: [
  170. {
  171. required: true,
  172. message: '必传',
  173. trigger: ['change', 'blur']
  174. }
  175. ],
  176. address: [
  177. {
  178. required: true,
  179. message: '必传',
  180. trigger: ['change', 'blur']
  181. }
  182. ],
  183. amount: [
  184. {
  185. required: true,
  186. message: '必传',
  187. trigger: ['change', 'blur']
  188. },
  189. {
  190. pattern: /^\d+\.?\d*$/g,
  191. // 正则检验前先将值转为字符串
  192. transform(value) {
  193. return String(value)
  194. },
  195. message: '格式不正确',
  196. trigger: ['change', 'blur']
  197. }
  198. ],
  199. num: [
  200. {
  201. required: true,
  202. message: '必传',
  203. trigger: ['change', 'blur']
  204. },
  205. {
  206. pattern: /^\d+$/g,
  207. // 正则检验前先将值转为字符串
  208. transform(value) {
  209. return String(value)
  210. },
  211. message: '格式不正确',
  212. trigger: ['change', 'blur']
  213. }
  214. ]
  215. },
  216. time: Number(new Date()),
  217. categoryList: [],
  218. dictList: [],
  219. fileList: [],
  220. fileurl: [],
  221. content: '',
  222. type: '',
  223. goodsId: ''
  224. }
  225. },
  226. onReady() {
  227. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  228. // this.$refs.form.setRules(this.rules)
  229. },
  230. onLoad({ type, goodsId }) {
  231. console.log(type, goodsId)
  232. this.type = type
  233. this.goodsId = goodsId
  234. if (goodsId) {
  235. this.show = false
  236. this.getDetail()
  237. }
  238. },
  239. onShow() {
  240. // const value = uni.getStorageSync('issuePageParam')
  241. // if (value) {
  242. // this.type = value.type
  243. // this.goodsId = value.goodsId
  244. // this.show = false
  245. // this.getDetail()
  246. // uni.removeStorageSync('issuePageParam');
  247. // }
  248. this.getConfig()
  249. this.getList()
  250. this.getDictList()
  251. },
  252. watch: {
  253. 'form.address': {
  254. handler(nl) {
  255. this.$refs.form.clearValidate('address')
  256. }
  257. }
  258. },
  259. methods: {
  260. //
  261. getDetail() {
  262. this.$api
  263. .postJson('/goods/detail', {
  264. id: this.goodsId
  265. })
  266. .then(res => {
  267. console.log(res)
  268. this.form = {
  269. title: res.data.title,
  270. content: res.data.content,
  271. categoryName: res.data.categoryName,
  272. categoryId: res.data.categoryId,
  273. lng: res.data.lng,
  274. lat: res.data.lat,
  275. address: res.data.address,
  276. amount: String(res.data.amount),
  277. num: String(res.data.num),
  278. brand: res.data.brand,
  279. power: res.data.power,
  280. energy: res.data.energy,
  281. province: res.data.province,
  282. city: res.data.city,
  283. area: res.data.area,
  284. street: res.data.street,
  285. goodsCreateTime: res.data.goodsCreateTime
  286. }
  287. this.fileList = res.data.goodsFiles.map(v => v.imgUrl)
  288. this.fileurl = []
  289. res.data.goodsFiles.forEach(v => {
  290. this.fileurl.push({
  291. imgUrl: v.imgUrl
  292. })
  293. })
  294. })
  295. .catch(() => {})
  296. },
  297. getConfig() {
  298. this.$api
  299. .get('/app/config/detail-by-type', {
  300. configType: '4'
  301. })
  302. .then(res => {
  303. this.content = res.data.content
  304. })
  305. .catch(() => {})
  306. },
  307. getFiles(value) {
  308. this.fileurl = []
  309. value.forEach(v => {
  310. this.fileurl.push({
  311. imgUrl: v
  312. })
  313. })
  314. },
  315. submin() {
  316. this.$refs.form
  317. .validate()
  318. .then(valid => {
  319. if (valid) {
  320. this.disabled = true
  321. this.form.categoryName =
  322. this.categoryList.find(v => v.categoryId === this.form.categoryId)?.categoryName ?? ''
  323. let params = {
  324. ...this.form,
  325. goodsFiles: this.fileurl
  326. }
  327. this.$toast('正在发布中,请稍后')
  328. setTimeout(() => {
  329. let path = '/goods/add'
  330. if (this.goodsId && this.type == 1) {
  331. path = '/goods/updateUp'
  332. params.id = this.goodsId
  333. }
  334. if (this.goodsId && this.type == 2) {
  335. path = '/goods/update'
  336. params.id = this.goodsId
  337. }
  338. this.$api
  339. .post(path, params)
  340. .then(res => {
  341. this.form = {
  342. title: '',
  343. content: '',
  344. categoryName: '',
  345. categoryId: '',
  346. lng: '',
  347. lat: '',
  348. address: '',
  349. amount: '',
  350. num: '',
  351. brand: '',
  352. power: '',
  353. province: '',
  354. city: '',
  355. area: '',
  356. street: '',
  357. energy: '',
  358. goodsCreateTime: ''
  359. }
  360. this.$refs.form.clearValidate()
  361. this.disabled = false
  362. this.show = true
  363. this.$navToPage(
  364. {
  365. url: '/pages/index/index'
  366. },
  367. 'switchTab'
  368. )
  369. })
  370. .catch(err => {
  371. this.disabled = false
  372. // this.$toast(err)
  373. })
  374. }, 1000)
  375. }
  376. })
  377. .catch(err => {
  378. this.$toast('缺少必要参数,请检查')
  379. })
  380. return
  381. },
  382. //获取商品分类数据
  383. async getList() {
  384. this.$api
  385. .get('/goods/category/list')
  386. .then(res => {
  387. this.categoryList = res.data
  388. })
  389. .catch(() => {})
  390. },
  391. //获取能效数据
  392. async getDictList() {
  393. this.$api
  394. .get('/goods/dict', {
  395. dictCode: '能效'
  396. })
  397. .then(res => {
  398. this.dictList = res.data
  399. })
  400. .catch(() => {})
  401. },
  402. handleDW() {
  403. uni.chooseLocation({
  404. success: res => {
  405. this.form.lng = res.longitude
  406. this.form.lat = res.latitude
  407. this.form.address = res.address
  408. this.$api
  409. .post(`/lbs/amap/region/query?lng=${res.longitude}&lat=${res.latitude}`)
  410. .then(res => {
  411. console.log(res)
  412. this.form.province = res.data.provinceName
  413. this.form.city = res.data.cityName
  414. this.form.area = res.data.areaName
  415. this.form.street = res.data.name
  416. })
  417. .catch(() => {})
  418. }
  419. })
  420. },
  421. handleConfirmTime(value) {
  422. const date = new Date(value.value) // JavaScript的Date对象使用的是毫秒时间戳,因此需要将Unix时间戳乘以1000
  423. const year = date.getFullYear()
  424. const month = String(date.getMonth() + 1).padStart(2, '0') // getMonth()返回的月份是从0开始的,因此需要加1,然后使用padStart()添加前导零
  425. const day = String(date.getDate()).padStart(2, '0')
  426. const hours = String(date.getHours()).padStart(2, '0')
  427. const minutes = String(date.getMinutes()).padStart(2, '0')
  428. const seconds = String(date.getSeconds()).padStart(2, '0')
  429. let res = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  430. let res2 = `${year}-${month}`
  431. this.form.goodsCreateTime = res
  432. this.time = res2
  433. this.showTime = false
  434. },
  435. handleCancelTime() {
  436. this.showTime = false
  437. },
  438. handleTime() {
  439. this.showTime = true
  440. },
  441. handleAgree() {
  442. this.show = false
  443. }
  444. }
  445. // #endif
  446. }
  447. </script>
  448. <style lang="scss" scoped>
  449. .contentXZ {
  450. padding: 20px;
  451. }
  452. .pop {
  453. overflow: auto;
  454. // padding: 0 20rpx;
  455. /* #ifdef H5 */
  456. // height: calc(100vh - 44px);
  457. height: 100vh;
  458. /* #endif */
  459. /* #ifndef H5 */
  460. height: 100vh;
  461. /* #endif */
  462. }
  463. .content {
  464. padding: 0 40rpx;
  465. margin: 20rpx 0;
  466. // height: calc(100vh - 44px);
  467. background-color: #fff;
  468. }
  469. .bottom-container {
  470. position: fixed;
  471. bottom: 0;
  472. width: 100%;
  473. background-color: #f1f1f1;
  474. padding: 18px 0;
  475. }
  476. .letText {
  477. width: 100%;
  478. display: flex;
  479. justify-content: flex-end;
  480. }
  481. ::v-deep .u-radio-label--undefined {
  482. justify-content: flex-end;
  483. }
  484. </style>