addGood.vue 34 KB

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