index.vue 15 KB

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