addGood.vue 35 KB

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