addGood.vue 38 KB

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