index.vue 15 KB

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