addGood.vue 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. <template>
  2. <div class="mainBox">
  3. <div class="layerBox">
  4. <tableTitle :name="tableDivTitle" />
  5. <el-form :model="form" ref="form" :rules="formRules" label-position="left" label-width="120px">
  6. <div class="formDiv">
  7. <el-form-item label="发布类型" prop="type_id" class="custom-align-right">
  8. <el-radio-group v-model="form.type_id">
  9. <el-radio :label="1">供应商品</el-radio>
  10. <el-radio :label="2">求购商品</el-radio>
  11. </el-radio-group>
  12. </el-form-item>
  13. <el-form-item label="商品名称:" prop="name" class="custom-align-right">
  14. <template #label>
  15. <span class="askBox" v-if="form.type_id == 1">
  16. 商品名称:
  17. </span>
  18. <span class="askBox" v-if="form.type_id == 2">
  19. 标题:
  20. </span>
  21. </template>
  22. <el-input v-model="form.name" autocomplete="off" placeholder="请输入商品标题"></el-input>
  23. </el-form-item>
  24. <el-form-item label="发布地点:" prop="city_arr_id" class="custom-align-right">
  25. <el-cascader :key="cascaderKey" v-model="form.city_arr_id" placeholder="请选择发布地点" :props="cityData"
  26. filterable clearable></el-cascader>
  27. </el-form-item>
  28. <el-form-item label="站点名称:" prop="website_id" class="custom-align-right" v-if="userType == 10000">
  29. <el-select v-model="form.website_id" placeholder="请选择站点名称" filterable clearable>
  30. <el-option v-for="item in websiteOptions" :key="item.value" :label="item.label" :value="item.value">
  31. </el-option>
  32. </el-select>
  33. </el-form-item>
  34. <el-form-item label="展示名称:" prop="cat_arr_id" class="custom-align-right">
  35. <el-cascader :key="parentKey" v-model="form.cat_arr_id" placeholder="请选择要绑定的展示名称" :props="parentData"
  36. filterable clearable></el-cascader>
  37. </el-form-item>
  38. <div v-if="user_type == 10000">
  39. <el-form-item label="推荐等级:" class="custom-align-right">
  40. <el-select v-model="form.level" multiple clearable placeholder="请选择推荐等级.." @change="levelChange">
  41. <el-option label="为你精选" :value="1"></el-option>
  42. <el-option label="热门产品" :value="2"></el-option>
  43. </el-select>
  44. </el-form-item>
  45. </div>
  46. <div v-if="form.type_id == 1">
  47. <el-form-item label="商品单价:" prop="price" class="custom-align-right">
  48. <el-input type="number" v-model="form.price" autocomplete="off" placeholder="请输入商品单价">
  49. <template #suffix>
  50. </template>
  51. </el-input>
  52. </el-form-item>
  53. <el-form-item label="单位:" prop="unit" class="custom-align-right">
  54. <el-input v-model="form.unit" autocomplete="off" placeholder="请输入单位"></el-input>
  55. </el-form-item>
  56. <el-form-item label="最小定量:" prop="min" class="custom-align-right">
  57. <el-input v-model="form.min" autocomplete="off" placeholder="请输入最小定量"></el-input>
  58. </el-form-item>
  59. <el-form-item label="供货总量:" prop="max" class="custom-align-right">
  60. <el-input v-model="form.max" autocomplete="off" placeholder="请输入供货总量"></el-input>
  61. </el-form-item>
  62. <el-form-item label="有效期:" prop="validity" class="custom-align-right">
  63. <el-date-picker v-model="form.validity" type="date" placeholder="选择日期" :disabled="form.islong == 1">
  64. </el-date-picker>
  65. <el-checkbox v-model="form.islong" @change="checked => selectChange(checked)">无期限</el-checkbox>
  66. </el-form-item>
  67. </div>
  68. <div v-if="form.type_id == 2">
  69. <el-form-item label="截止日期:" prop="validity" class="custom-align-right">
  70. <el-date-picker v-model="form.validity" type="date" placeholder="选择日期" :disabled="form.islong == 1">
  71. </el-date-picker>
  72. <el-checkbox v-model="form.islong" @change="checked => selectChange(checked)">无期限</el-checkbox>
  73. </el-form-item>
  74. </div>
  75. <el-form-item label="商品图:" class="custom-align-right" prop="imgurl">
  76. <div class="uploaderBox">
  77. <span v-if="imgurl.length > 0" class="uploaded-images">
  78. <div v-for="(url, index) in imgurl" :key="index"
  79. style="display: inline;float: left; width: 150px; height:140px;">
  80. <img :src="url" class="avatar" style="float: left; margin-right: 10px;">
  81. <div style="position: relative;
  82. transform: translate(-50%, -50%);
  83. right: -120px;
  84. top: -20px;
  85. background-color: rgba(0, 0, 0, 0.5);
  86. color: white;
  87. border-radius: 50%;
  88. width: 24px;
  89. height: 24px;
  90. display: flex;
  91. align-items: center;
  92. justify-content: center;
  93. cursor: pointer;
  94. z-index: 10;" @click="deleteImage(index)">
  95. <i class="el-icon-delete"></i>
  96. </div>
  97. </div>
  98. </span>
  99. <el-upload class="avatar-uploader1" action="#" :show-file-list="true" :before-upload="beforeAvatarUpload"
  100. style="display: inline;">
  101. <div style="width: 100px; display: inline-block;">
  102. <img src="@/assets/public/upload/noImage.png">
  103. <div>选择图片</div>
  104. </div>
  105. </el-upload>
  106. </div>
  107. </el-form-item>
  108. <div class="imgBox" style="margin-left: 100px ;margin-bottom: 20px; padding-top: -20px;">
  109. 最多可上传5张,每张图片大小不能超过500K,推荐上传图片尺寸
  110. 宽1000像素 高1000像素。(首张显示为商品头图)</div>
  111. <!-- <el-form-item label="商品关键词:" prop="keyword" class="custom-align-right">
  112. <el-input v-model="form.keyword" autocomplete="off" placeholder="请输入商品关键词"></el-input>
  113. </el-form-item> -->
  114. <el-form-item label="商品关键词:" prop="keyword" class="custom-align-right">
  115. <template #label>
  116. <span class="askBox">
  117. 商品关键词:
  118. <el-tooltip class="item" effect="dark" content="商品关键词,如:三农市场网、全国三农、信息一体化。" placement="top">
  119. <i class="el-icon-question"></i>
  120. </el-tooltip>
  121. </span>
  122. </template>
  123. <tagInput :initialTags="tags" @tags-updated="updateTags"></tagInput>
  124. </el-form-item>
  125. <el-form-item label="商品描述:" prop="description" class="custom-align-right">
  126. <el-input v-model="form.description" maxlength="300" autocomplete="off" placeholder="请输入商品描述"
  127. type="textarea" :rows="10"></el-input>
  128. </el-form-item>
  129. <el-form-item label="详情说明:" prop="description" class="custom-align-right">
  130. <!--这里是富文本-->
  131. <myEditor ref="myEditor" v-model="form.detail"></myEditor>
  132. </el-form-item>
  133. <el-form-item label="浏览量:" prop="hits" class="custom-align-right">
  134. <el-input type="number" v-model="form.hits" autocomplete="off" placeholder="请输入浏览量"></el-input>
  135. </el-form-item>
  136. <el-form-item label="联系人:" prop="contact" class="custom-align-right">
  137. <el-input v-model="form.contact" autocomplete="off" placeholder="请输入联系人"></el-input>
  138. </el-form-item>
  139. <el-form-item label="联系电话:" prop="phone" class="custom-align-right">
  140. <el-input v-model="form.phone" autocomplete="off" placeholder="请输入联系电话"></el-input>
  141. </el-form-item>
  142. <el-form-item label="座机:" prop="landline" class="custom-align-right">
  143. <el-input v-model="form.landline" autocomplete="off" placeholder="请输入座机"></el-input>
  144. </el-form-item>
  145. <el-form-item label="电子邮箱:" prop="email" class="custom-align-right">
  146. <el-input type="email" v-model="form.email" autocomplete="off" placeholder="请输入电子邮件"></el-input>
  147. </el-form-item>
  148. <el-form-item label="邮政编码:" prop="postal" class="custom-align-right">
  149. <el-input type="number" v-model="form.postal" autocomplete="off" placeholder="请输入邮政编码"></el-input>
  150. </el-form-item>
  151. <el-form-item label="详细地址:" prop="address" class="custom-align-right">
  152. <el-input v-model="form.address" autocomplete="off" placeholder="请输入详细地址"></el-input>
  153. </el-form-item>
  154. </div>
  155. </el-form>
  156. </div>
  157. <div class="bottomBtnBox">
  158. <el-button type="info" @click="returnPage">返回</el-button>
  159. <el-button type="primary" @click="editToServe" v-if="editStatus == true">确定</el-button>
  160. <el-button type="primary" @click="addToServe" v-else>发布商品</el-button>
  161. </div>
  162. </div>
  163. </template>
  164. <script>
  165. import { getWebSiteId, getUseType } from '@/utils/auth'
  166. //表格标题
  167. import tableTitle from './components/tableTitle';
  168. //引入tag标签组件
  169. import tagInput from '../../components/InputTag/index.vue';
  170. //引入公用样式
  171. import '@/styles/global.less';
  172. //格式化时间
  173. import { formatLocalDate } from '@/utils/public';
  174. //引入富文本编辑器
  175. import myEditor from '../../components/edit/myEditor.vue';
  176. export default {
  177. components: {
  178. tableTitle,
  179. myEditor,
  180. tagInput
  181. },
  182. data() {
  183. //0.全局操作 start ------------------------------------------------------------>
  184. //表单验证
  185. const validateNull = (rule, value, callback) => {
  186. console.log(value, '-------------------------------')
  187. if (value == '' || value == undefined || value == null) {
  188. callback(new Error('该项不能为空!'))
  189. } else {
  190. callback()
  191. }
  192. }
  193. const validateEmpty = (rule, value, callback) => {
  194. console.log(value, '-------------------------------')
  195. if (value.length == 0) {
  196. callback(new Error('该项不能为空!'))
  197. } else {
  198. callback()
  199. }
  200. }
  201. const validateArray = (rule, value, callback) => {
  202. if (value.length == 0) {
  203. callback(new Error('该项不能为空!'))
  204. } else {
  205. callback()
  206. }
  207. }
  208. let self = this;
  209. //0.全局操作 end ------------------------------------------------------------>
  210. return {
  211. userType: 0,
  212. websiteOptions: [],
  213. checked: false,
  214. formLabelWidth: '80px',//表单的长度
  215. //1.表单项 start ------------------------------------------------------------>
  216. editStatus: false,
  217. tableDivTitle: "添加商品",
  218. searchCascaderKey: 0, //列表缓存key
  219. // imgurl: ['http://192.168.1.127:9501/image/20250227/1740674706184955.jpg', 'http://192.168.1.127:9501/image/20250227/1740674706184955.jpg'],//图片路径
  220. website_id: "",
  221. tags: [],//标签数组
  222. //提交表单
  223. form: {
  224. //1.1使用了外链
  225. name: '',//商品标题
  226. type_id: 1,//布类型:1:供应,2求购
  227. city_arr_id: [],//行政区划
  228. city_id: '',//城市id
  229. cat_arr_id: '',//导航池名称
  230. website_id: "",
  231. catid: "",
  232. cat_arr_id: "",
  233. name: "",
  234. level: [],//推荐等级
  235. price: "",
  236. unit: "",
  237. min: null,
  238. max: null,
  239. islong: 0,
  240. validity: "",
  241. keyword: "",
  242. description: "",
  243. detail: "",
  244. hits: null,
  245. contact: '',
  246. phone: '',
  247. landline: '',
  248. email: '',
  249. postal: '',
  250. address: '',
  251. city_id: '',
  252. imgurl: [],//缩略图
  253. },
  254. //1.2 表单验证规则
  255. formRules: {
  256. type_id: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  257. //商品名称不能为空
  258. name: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  259. city_arr_id: [{ required: true, trigger: 'blur', validator: validateArray }],
  260. // 导航池名称不能为空
  261. cat_arr_id: [{ required: true, trigger: 'blur', validator: validateArray }],
  262. imgurl: [{ required: true, trigger: 'blur', validator: validateArray }],
  263. website_id: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  264. price: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  265. unit: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  266. validity: [{ required: true, trigger: 'blur', validator: validateNull }],
  267. description: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  268. detail: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  269. keyword: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  270. contact: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  271. phone: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  272. address: [{ required: true, trigger: 'blur', validator: validateEmpty }],
  273. },
  274. //1.3富文本编辑器配置
  275. showHtml: false, //用于保存源码内容
  276. editorHtml: '',
  277. cascaderKey: 0,//弹窗用的key
  278. websiteData: {
  279. lazy: true,
  280. checkStrictly: true,
  281. async lazyLoad(node, resolve) {
  282. const { level, data } = node;
  283. if (data && data.children && data.children.length !== 0) {
  284. return resolve(node)
  285. }
  286. console.log(level)
  287. let parentId = level == 0 ? 0 : data.value
  288. let parames = {
  289. 'page': 1,
  290. 'pageSize': 1000,
  291. }
  292. self.$store.dispatch('news/websiteList', parames).then(res => {
  293. if (res.data) {
  294. const nodes = res.data.rows.map(item => ({
  295. value: item.id,
  296. label: item.website_name,
  297. leaf: level >= 3,
  298. children: []
  299. }))
  300. console.log(nodes, 'leaf')
  301. resolve(nodes)
  302. }
  303. })
  304. },
  305. },
  306. cityData: {
  307. checkStrictly: true,
  308. lazy: true,
  309. async lazyLoad(node, resolve) {
  310. const { level, data } = node;
  311. if (data && data.children && data.children.length !== 0) {
  312. return resolve(node)
  313. }
  314. console.log(level, '====level-----')
  315. let parentId = level == 0 ? 0 : data.value
  316. let parames = {
  317. 'pid': parentId
  318. }
  319. self.$store.dispatch('pool/getcityList', parames).then(res => {
  320. if (res.data) {
  321. const nodes = res.data.map(item => ({
  322. value: item.id,
  323. label: item.name,
  324. leaf: level >= 3,
  325. children: []
  326. }))
  327. resolve(nodes)
  328. }
  329. })
  330. }
  331. },
  332. editorOptions: {
  333. placeholder: '请输入内容...',
  334. theme: 'snow', // 主题样式
  335. modules: {
  336. toolbar: {
  337. container: [
  338. [{ 'font': [] }], // 字体
  339. [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 标题
  340. [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
  341. ['bold', 'italic', 'underline', 'strike'], // 加粗、斜体、下划线、删除线
  342. [{ 'color': [] }, { 'background': [] }], // 文字颜色、背景颜色
  343. [{ 'script': 'sub' }, { 'script': 'super' }], // 上标、下标
  344. [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表
  345. [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进
  346. [{ 'align': [] }], // 对齐方式
  347. ['blockquote', 'code-block'], // 引用、代码块
  348. ['link', 'image', 'video'], // 链接、图片、视频
  349. ['clean'],
  350. [{ 'html': true }] // 添加自定义按钮的占位符
  351. ],
  352. handlers: {
  353. image: () => {
  354. this.handleImageClick();
  355. },
  356. showHtml: function () {
  357. this.$emit('toggleSourceMode');
  358. }
  359. }
  360. },
  361. imageResize: {
  362. displayStyles: {
  363. backgroundColor: 'black',
  364. border: 'none',
  365. color: 'white'
  366. },
  367. modules: ['Resize', 'DisplaySize', 'Toolbar'] // 启用不同的调整方式
  368. }
  369. }
  370. },
  371. //1.4图片上传
  372. // imgurl: ['http://192.168.1.127:9501/image/20250227/1740674706184955.jpg', 'http://192.168.1.127:9501/image/20250227/1740674706184955.jpg'],
  373. imgurl: [],
  374. //获取父级导航池
  375. parentKey: 0,//获取父级导航
  376. parentData: {
  377. checkStrictly: true,
  378. lazy: true,
  379. async lazyLoad(node, resolve) {
  380. console.log(resolve, 'node')
  381. const { level, data } = node;
  382. if (data && data.children && data.children.length !== 0) {
  383. return resolve(node)
  384. }
  385. console.log(level, 'level--1--');
  386. let pid = level == 0 ? 0 : data.value
  387. let website_id = self.form.website_id == '' ? 2 : self.form.website_id;
  388. console.log(website_id, 'website_id--111-----');
  389. let parames = {
  390. 'website_id': website_id,
  391. 'pid': pid
  392. }
  393. self.$store.dispatch('news/getWebsiteNavList', parames).then(res => {
  394. if (res.data) {
  395. const nodes = res.data.map(item => ({
  396. value: item.category_id,
  397. label: item.name,
  398. leaf: level >= 3,
  399. children: []
  400. }))
  401. resolve(nodes)
  402. } else {
  403. //输出报错原因
  404. console.log(res.msg, 'getWebsiteNavList--1--');
  405. }
  406. })
  407. }
  408. },
  409. inputList: [
  410. { value: '' }
  411. ]
  412. //表单项 end ------------------------------------------------------------>
  413. };
  414. },
  415. watch: {
  416. '$route'(to, from) {
  417. console.log(from, '---------------------------------------');
  418. // 监听路由参数中的 id 变化,若变化则更新页面状态并获取数据
  419. if (to.query.id) {
  420. this.getMainData();
  421. }
  422. },
  423. // 监听路由参数中的 id 变化,若变化则更新页面状态并获取数据
  424. // '$route.query.id': function (newVal, oldVal) {
  425. // console.log('当前 id:', newVal, '旧的 id:', oldVal);
  426. // if (newVal) {
  427. // this.editStatus = true;
  428. // this.tableDivTitle = "编辑资讯";
  429. // // // 调用方法名保持大小写一致
  430. // this.getMainData();
  431. // } else {
  432. // this.editStatus = false;
  433. // this.tableDivTitle = "添加资讯";
  434. // }
  435. // }
  436. },
  437. methods: {
  438. async fetchWebsiteOptions() {
  439. try {
  440. const response = await this.$store.dispatch('news/websiteList', { page: 1, pageSize: 1000 });
  441. if (response && response.data) {
  442. this.websiteOptions = response.data.rows.map(item => ({
  443. value: item.id,
  444. label: item.website_name,
  445. }));
  446. }
  447. } catch (error) {
  448. console.error('获取网站列表失败:', error);
  449. }
  450. },
  451. //获取用户身份信息
  452. getUserInfo() {
  453. this.$store.dispatch('public/getInfo').then(res => {
  454. console.log(res)
  455. this.userType = res.data.type_id;
  456. this.user_type = res.data.type_id;
  457. if (this.userType != 10000) {
  458. this.form.website_id = getWebSiteId()
  459. }
  460. //if(res.data.type_id==10000){}//管理员
  461. //if(res.data.type_id==4){}//调研员
  462. //个人会员=1 政务会员=2 企业会员=3 调研员=4 管理员=10000 游客=20000
  463. }).catch(() => {
  464. this.$message({
  465. type: 'info',
  466. message: '网络错误,请重试!'
  467. });
  468. })
  469. },
  470. //1.提交表单 start ------------------------------------------------------------>
  471. beforeAvatarUpload(file) {
  472. const isJPG = file.type === 'image/jpeg';
  473. const isPNG = file.type === 'image/png';
  474. const isLt2M = file.size / 1024 / 1024 < 2;
  475. console.log(this.imgurl.length, '-0--------------------------------')
  476. if (this.imgurl.length > 4) {
  477. this.$message.warning('只能上传5张图片');
  478. return false;
  479. }
  480. if (!isJPG && !isPNG) {
  481. this.$message.error('上传缩略图只能是 JPG 或 PNG 格式!');
  482. return false;
  483. }
  484. if (!isLt2M) {
  485. this.$message.error('上传缩略图大小不能超过 2MB!');
  486. return false;
  487. }
  488. const formData = new FormData();
  489. formData.append('file', file);
  490. this.$store.dispatch('pool/uploadFile', formData).then(res => {
  491. this.imgurl.push(res.data.imgUrl);//显示缩略图
  492. this.form.imgurl.push(res.data.imgUrl);//提供表单地址
  493. console.log(res.data.imgUrl, '00000')
  494. })
  495. // 阻止默认的上传行为
  496. return false;
  497. },
  498. deleteImage(index) { // 删除图片
  499. this.imgurl.splice(index, 1);
  500. },
  501. //1.2 提交表单
  502. addToServe() {
  503. this.form.website_id = getWebSiteId();
  504. console.log("提交时获取网站id:",this.form.website_id)
  505. //先进行验证
  506. this.$refs.form.validate(valid => {
  507. console.log(this.form.validity, 'this.form.validity-----------------')
  508. if (this.form.islong == 1 && (this.form.validity == null || this.form.validity == '')) {
  509. this.form.validity = '1970-01-01 00:00:00';
  510. }
  511. console.log(this.form.validity, 'this.form.validity-----------------')
  512. if (valid) {
  513. //如果推荐等级为空,则设置为0
  514. if (this.form.level == "" || this.form.level == "[0]") {
  515. this.form.level = "[0]";
  516. } else {
  517. this.form.level = JSON.stringify(this.form.level);
  518. }
  519. if (this.form.validity) {
  520. console.log(this.form.validity, 'p0------------------')
  521. this.form.validity = formatLocalDate(this.form.validity);
  522. console.log(this.form.validity, 'p1-3333333333333333333333333')
  523. }
  524. this.form.imgurl = this.imgurl;
  525. if (this.form.islong == 1) {
  526. this.form.validity = null;
  527. }
  528. if (this.form.min == '') {
  529. this.form.min = null;
  530. }
  531. if (this.form.max == '') {
  532. this.form.max = null;
  533. }
  534. if (this.form.hits == '') {
  535. this.form.hits = null;
  536. }
  537. console.log(this.form, '提交的数据')
  538. //判断用户身份
  539. if (this.user_type == 10000) {
  540. console.log("用户身份为管理员,无需审核直接发布!")
  541. this.form.status = 2;
  542. } else {
  543. console.log("用户身份为其他用户,提交到审核!")
  544. this.form.status = 1;
  545. }
  546. this.$store.dispatch('news/addGood', this.form).then(res => {
  547. if (res.code == 200) {
  548. //汇报结果
  549. this.$message({
  550. type: 'success',
  551. message: '已成功添加商品!'
  552. });
  553. this.cleatForm();
  554. //返回列表页
  555. this.returnPage()
  556. } else {
  557. this.$message({
  558. type: 'error',
  559. message: "商品发布失败,请稍后再试!"
  560. });
  561. }
  562. })
  563. }
  564. })
  565. },
  566. formatDate(date) {
  567. if (date instanceof Date) {
  568. return date.toISOString().slice(0, 19).replace('T', ' ');
  569. } else if (typeof date === 'string') {
  570. // 将 ISO 8601 格式的字符串转换为 MySQL 期望的格式
  571. const parsedDate = new Date(date);
  572. return parsedDate.toISOString().slice(0, 19).replace('T', ' ');
  573. } else {
  574. return date;
  575. }
  576. },
  577. //1.3 清理表单
  578. cleatForm() {
  579. },
  580. //提交表单 end ------------------------------------------------------------>
  581. //2.跳转操作 start ------------------------------------------------------------>
  582. returnPage() {
  583. this.$router.push({
  584. path: '/goodList',
  585. });
  586. },
  587. //跳转操作 end ------------------------------------------------------------>
  588. //3.回显操作 ------------------------------------------------------------>
  589. //3.1回显数据
  590. getMainData() {
  591. let data = {
  592. id: this.$route.query.id
  593. };
  594. this.$store.dispatch('news/getGoodInfo', data).then(res => {
  595. console.log(res);
  596. this.form.name = res.data.name;
  597. // 回显导航池
  598. this.form.cat_arr_id = Array.isArray(res.data.cat_arr_id) ? res.data.cat_arr_id : JSON.parse(res.data.cat_arr_id);
  599. this.form.city_arr_id = Array.isArray(res.data.city_arr_id) ? res.data.city_arr_id : JSON.parse(res.data.city_arr_id);
  600. this.parentKey += 1; // 触发级联选择器重新加载
  601. this.loadCascaderPath(this.form.cat_arr_id); // 加载路径数据
  602. //回显推荐等级
  603. this.form.imgurl = Array.isArray(res.data.imgurl) ? res.data.imgurl : JSON.parse(res.data.imgurl);
  604. this.imgurl = Array.isArray(res.data.imgurl) ? res.data.imgurl : JSON.parse(res.data.imgurl);
  605. this.form.type_id = res.data.type_id;
  606. this.form.website_id = res.data.website_id;
  607. this.form.catid = res.data.catid;
  608. this.form.price = res.data.price;
  609. this.form.unit = res.data.unit;
  610. this.form.min = res.data.min;
  611. this.form.max = res.data.max;
  612. this.form.islong = res.data.islong == 1 ? true : false;
  613. this.form.validity = res.data.validity;
  614. this.form.keyword = res.data.keyword;
  615. this.tags = res.data.keyword ? res.data.keyword.split(",") : [];
  616. this.form.description = res.data.description;
  617. //回显编辑器内容
  618. this.$nextTick(() => {
  619. this.form.detail = res.data.detail;
  620. });
  621. this.form.hits = res.data.hits;
  622. this.form.contact = res.data.contact;
  623. this.form.phone = res.data.phone;
  624. this.form.landline = res.data.landline;
  625. this.form.email = res.data.email;
  626. this.form.postal = res.data.postal;
  627. this.form.address = res.data.address;
  628. this.form.level = res.data.level;
  629. if (res.data.level == "[]" || res.data.level == "") {
  630. this.form.level = [];
  631. } else {
  632. this.form.level = JSON.parse(res.data.level);
  633. }
  634. })
  635. },
  636. async loadCascaderPath(path) {
  637. for (let i = 0; i < path.length; i++) {
  638. const parentId = path[i - 1] || 0; // 获取当前层级的父级ID
  639. const level = i; // 当前层级的索引
  640. // self.$store.dispatch('news/getWebsiteNavList', parames).then(res => {
  641. // if (res.data) {
  642. // const nodes = res.data.map(item => ({
  643. // value: item.category_id,
  644. // label: item.name,
  645. // leaf: level >= 3,
  646. // children: []
  647. // }))
  648. // resolve(nodes)
  649. await this.$store.dispatch('pool/categoryList', { pid: parentId })
  650. .then((res) => {
  651. const nodes = res.data.map(item => ({
  652. value: item.id,
  653. label: item.name,
  654. leaf: level >= 3, // 假设4层结构,设置叶子节点标记
  655. }));
  656. // 级联选择器加载数据
  657. if (level === path.length - 1) {
  658. this.form.cat_arr_id = path; // 确保最后一级路径正确设置
  659. this.parentKey += 1; // 强制刷新 cascader
  660. }
  661. });
  662. }
  663. },
  664. //1.3提交修改
  665. editToServe() {
  666. //提交之前先判断是否为外链
  667. //如果使用了外链,清理掉除了外链以外的内容
  668. console.log(this.form)
  669. //添加要修改的id
  670. this.form.id = this.editId;
  671. //先进行验证
  672. console.log(this.form.validity, 'validity')
  673. if (this.form.islong == 1 && this.form.validity == null) {
  674. this.form.validity = '1970-01-01 00:00:00';
  675. }
  676. this.$refs.form.validate(valid => {
  677. if (valid) {
  678. //如果推荐等级为空,则设置为0
  679. if (this.form.level == "" || this.form.level == "[]" || this.form.level == null) {
  680. this.form.level = "[]";
  681. } else {
  682. this.form.level = JSON.stringify(this.form.level);
  683. }
  684. if (this.form.validity) {
  685. console.log(this.form.validity, 'p0------------------')
  686. this.form.validity = formatLocalDate(this.form.validity);
  687. console.log(this.form.validity, '0------------')
  688. }
  689. if (this.form.islong == 1) {
  690. this.form.validity = null;
  691. }
  692. if (this.form.min == '') {
  693. this.form.min = null;
  694. }
  695. if (this.form.max == '') {
  696. this.form.max = null;
  697. }
  698. if (this.form.hits == '') {
  699. this.form.hits = null;
  700. }
  701. this.form.imgurl = this.imgurl;
  702. //console.log(this.form)
  703. //判断用户身份
  704. if (this.user_type == 10000) {
  705. console.log("用户身份为管理员,无需审核直接发布!")
  706. this.form.status = 2;
  707. } else {
  708. console.log("用户身份为其他用户,提交到审核!")
  709. this.form.status = 1;
  710. }
  711. this.$store.dispatch('news/updateGood', this.form).then(res => {
  712. if (res.code != 200) {
  713. this.$message.error("修改失败,请稍后再试!");
  714. } else {
  715. //汇报结果
  716. this.$message({
  717. type: 'success',
  718. message: '已成功编辑商品!'
  719. });
  720. this.cleatForm();
  721. //返回列表页
  722. this.returnPage()
  723. }
  724. }).catch(() => {
  725. this.$message({
  726. type: 'info',
  727. message: '网络错误,请重试!'
  728. });
  729. })
  730. }
  731. })
  732. },
  733. //推荐等级选择
  734. levelChange(value) {
  735. this.form.level = value;
  736. },
  737. //跳转操作 end ------------------------------------------------------------>
  738. //商品关键词
  739. updateTags(newTags) {
  740. // this.foem.seo_keywords = newTags;
  741. this.tags = newTags;
  742. this.form.keyword = newTags.join(',');
  743. },
  744. selectChange(checked){
  745. if(checked==true){
  746. this.form.validity = '1970-01-01 00:00:00';
  747. }else{
  748. }
  749. }
  750. },
  751. watch: {
  752. 'form.type_id': {
  753. handler(newVal, oldVal) {
  754. if (newVal !== oldVal) {
  755. if (this.$route.query.id != undefined) {
  756. } else {
  757. console.log('-------新增--变化了------------');
  758. this.form.name = '';
  759. this.form.city_arr_id = [];
  760. this.form.city_id = '';
  761. this.form.cat_arr_id = '';
  762. this.form.website_id = '';
  763. this.form.catid = "";
  764. this.form.price = "";
  765. this.form.unit = "";
  766. this.form.min = null;
  767. this.form.max = null;
  768. this.form.islong = 0;
  769. this.form.validity = "";
  770. this.form.keyword = "";
  771. this.form.description = "";
  772. this.form.detail = "";
  773. this.form.hits = null;
  774. this.form.contact = '';
  775. this.form.phone = '';
  776. this.form.landline = '';
  777. this.form.email = '';
  778. this.form.postal = '';
  779. this.form.address = '';
  780. this.form.imgurl = [];
  781. this.form.city_id = '';
  782. }
  783. // 清空特定字段的数据
  784. if (newVal === 1) {
  785. // 从求购商品切换到供应商品
  786. this.form.price = '';
  787. console.log('供应商品');
  788. } else if (newVal === 2) {
  789. // 从供应商品切换到求购商品
  790. console.log('求购商品');
  791. }
  792. }
  793. },
  794. immediate: true // 立即执行一次
  795. },
  796. //监控 website_id
  797. // 若 website_id 发生变化,则清空 cat_arr_id 和 catid
  798. 'form.website_id': {
  799. handler(newVal, oldVal) {
  800. if (newVal !== oldVal) {
  801. this.form.cat_arr_id = [];
  802. this.form.catid = "";
  803. this.parentKey += 1;
  804. }
  805. },
  806. immediate: true // 立即执行一次
  807. }
  808. },
  809. mounted() {
  810. this.user_type = getUseType();
  811. //1.判断是新建还是回显
  812. if (this.$route.query.id != undefined) {
  813. this.editId = this.$route.query.id;
  814. this.editStatus = true;
  815. this.tableDivTitle = "编辑商品";
  816. console.log("编辑商品")
  817. this.getMainData();
  818. } else {
  819. this.editStatus = false;
  820. console.log("添加商品!")
  821. }
  822. this.fetchWebsiteOptions();
  823. this.getUserInfo();
  824. },
  825. };
  826. </script>
  827. <style scoped lang="less">
  828. //执行v-deep穿透scope选择器 start------------------------------------------------------------>*/
  829. ::v-deep .custom-form-item>.el-form-item__label {
  830. line-height: 140px !important;
  831. }
  832. ::v-deep .custom-textarea .el-textarea__inner {
  833. resize: none;
  834. /* 禁止用户拖拽调整大小 */
  835. }
  836. ::v-deep .custom-align-right .el-form-item__label {
  837. text-align: right;
  838. /* 设置标签文字右对齐 */
  839. }
  840. ::v-deep .el-select {
  841. width: 100%;
  842. /* 禁止用户拖拽调整大小 */
  843. }
  844. ::v-deep .el-input-group__prepend {
  845. color: black !important;
  846. }
  847. .formLabelFloatBox {
  848. margin-bottom: 10px;
  849. position: relative;
  850. .formLabeladdIcon {
  851. position: absolute;
  852. right: 45px;
  853. top: 5px;
  854. width: 38px;
  855. height: 24px;
  856. }
  857. .formLabelDelIcon {
  858. position: absolute;
  859. right: 5px;
  860. top: 5px;
  861. width: 38px;
  862. height: 24px;
  863. }
  864. }
  865. ::v-deep .el-upload {
  866. display: inline-block;
  867. text-align: center;
  868. cursor: pointer;
  869. outline: none;
  870. border: 1px solid #ccc;
  871. padding: 26px 20px;
  872. border-radius: 10px;
  873. width: 140px;
  874. height: 140px;
  875. }
  876. ::v-deep .avatar {
  877. width: 140px;
  878. height: 140px;
  879. display: block;
  880. border: 1px solid #ccc;
  881. padding: 5px;
  882. border-radius: 10px;
  883. box-sizing: border-box;
  884. }
  885. ::v-deep .el-checkbox {
  886. margin-left: 20px;
  887. }
  888. .imgBox{
  889. color: #999;
  890. }
  891. //执行v-deep穿透scope选择器 end------------------------------------------------------------>*/</style>