addGood.vue 36 KB

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