DesignPic.tsx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. import React, { useState, useEffect } from 'react'
  2. import { Button, Table, Input, Message, Loading, Radio } from '@alifd/next';
  3. import { CustomMessage } from '@/components/CustomMessage'
  4. import { urlToFile } from './utils';
  5. const RadioGroup = Radio.Group
  6. export default function DesignPic() {
  7. const [uploadUrl, setUploadUrl] = useState<string>("");
  8. // 添加状态管理表格数据
  9. const [tableData, setTableData] = useState<any[]>([]);
  10. const [loading, setLoading] = useState(false);
  11. const [isPage, setIsPage] = useState(true);
  12. const [btnLoading, setBtnLoading] = useState(false);
  13. const [isPreImg, setIsPreImg] = useState(-1)
  14. useEffect(() => {
  15. is_temu_design()
  16. }, [])
  17. const is_temu_design = async () => {
  18. try {
  19. // 获取当前活动标签页
  20. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  21. // 获取页面域名
  22. if (tab.url) {
  23. const url = tab.url
  24. console.log('当前页面域名:', url);
  25. if (url.includes('main/customize-goods')) {
  26. // temu定制页面
  27. setIsPage(false)
  28. }
  29. }
  30. } catch (error) {
  31. console.error('获取当前页面域名时出错:', error);
  32. }
  33. }
  34. const order_render = (value: any, index: any, record: any) => {
  35. return (
  36. <div>{index + 1}</div>
  37. )
  38. }
  39. const design_render = (value: any, index: any, record: any) => {
  40. return <>
  41. {
  42. record.pre_info && record.pre_info.pre_pic ? <>
  43. <div className='flex items-center justify-center'><img className='size-20' src={record.pre_info?.pre_pic} /></div></> : <>
  44. <span>暂无图片</span>
  45. </>
  46. }
  47. </>
  48. }
  49. const pic_render = (value: any, index: any, record: any) => {
  50. return (
  51. <>
  52. {
  53. record.design_pic ? <>
  54. <div className='flex items-center justify-center'><img className='size-20' src={record.design_pic} /></div></> : <>
  55. <span>暂无图片</span>
  56. </>
  57. }
  58. </>
  59. );
  60. }
  61. const text_render = (value: any, index: any, record: any) => {
  62. return (
  63. <p>{record.text}</p>
  64. );
  65. }
  66. // 修改图片名称渲染函数
  67. const name_render = (value: any, index: number, record: any) => {
  68. return <>
  69. <div>{record.order_no}</div>
  70. </>
  71. };
  72. // 修改图片标签渲染函数
  73. const label_render = (value: any, index: number, record: any) => {
  74. return <>
  75. <div>{record.region}</div>
  76. </>
  77. };
  78. // 完善删除功能
  79. const handleDelete = (index: number, record: any) => {
  80. // 方案1: 直接删除
  81. const newData = [...tableData];
  82. newData.splice(index, 1);
  83. setTableData(newData);
  84. // 方案2: 带确认的删除 (可选)
  85. // if (window.confirm(`确定要删除 "${record.title.name}" 吗?`)) {
  86. // const newData = [...tableData];
  87. // newData.splice(index, 1);
  88. // setTableData(newData);
  89. // Message.success('删除成功');
  90. // }
  91. };
  92. // 删除操作渲染函数
  93. const option_render = (value: any, index: number, record: any) => {
  94. return (
  95. record.is_upload ? <p className='text-green-600'>上传成功</p> :
  96. <a
  97. className='text-red-600 cursor-pointer'
  98. onClick={() => handleDelete(index, record)}
  99. >
  100. 删除
  101. </a>
  102. );
  103. };
  104. // 添加行索引以确保正确删除
  105. const dataSourceWithIndex = tableData.map((item, index) => ({
  106. ...item,
  107. index
  108. }));
  109. const getPicData = async () => {
  110. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  111. // 获取页面域名
  112. if (tab.url) {
  113. const url = tab.url
  114. if (!url.includes('main/customize-goods')) {
  115. // temu定制页面
  116. CustomMessage.error('当前页面不是定制页面')
  117. return
  118. }
  119. }
  120. try {
  121. setLoading(true)
  122. // 获取上传路径
  123. fetch('http://merchant.landwu.com/api/photo/getUploadUrl', {
  124. method: 'POST',
  125. headers: {
  126. 'Content-Type': 'application/json',
  127. },
  128. body: JSON.stringify({
  129. api_token: localStorage.getItem('access_token'),
  130. }),
  131. }).then(resjson => {
  132. return resjson.json();
  133. }).then(res => {
  134. if (res.data) {
  135. const { data = {} } = res;
  136. const { url = null } = data;
  137. setUploadUrl(url)
  138. }
  139. }).catch(e => {
  140. setLoading(false)
  141. })
  142. setTableData([])
  143. setBtnLoading(true)
  144. // 获取当前活动标签页
  145. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  146. if (tab.id) {
  147. // 发送消息到content script请求获取tbody数据
  148. const response = await browser.tabs.sendMessage(tab.id, { action: "getTbodyData" });
  149. // 在控制台输出结果
  150. if (response.tables && response.tables.length > 0) {
  151. // console.log("找到的表格数据(包含背景图片):");
  152. // 显式声明类型
  153. let tables_arr: {
  154. good_info: any;
  155. sku_info: any;
  156. order_no: any;
  157. design_sku: any;
  158. pre_design_info: any;
  159. design_info: { region: any; text: any; picture: any }[];
  160. }[] = [];
  161. let table_boj: {
  162. good_info: any;
  163. sku_info: any;
  164. order_no: any;
  165. design_sku: any;
  166. pre_design_info: any;
  167. design_info: { region: any; text: any; picture: any }[];
  168. } = {
  169. good_info: null,
  170. sku_info: null,
  171. order_no: null,
  172. design_sku: null,
  173. pre_design_info: null,
  174. design_info: []
  175. };
  176. response.tables.forEach((table_node: any, index: number) => {
  177. console.log(`表格 ${index + 1}:`, table_node);
  178. // 处理成接口参数
  179. // 处理成数组对象
  180. if (table_node.length > 4) {
  181. if (table_boj.design_info.length) {
  182. // 一个商品的定制信息
  183. tables_arr.push(table_boj)
  184. // 重置
  185. table_boj = {
  186. good_info: null,
  187. sku_info: null,
  188. order_no: null,
  189. design_sku: null,
  190. pre_design_info: null,
  191. design_info: []
  192. }
  193. }
  194. table_boj = {
  195. good_info: table_node[1],
  196. sku_info: table_node[2],
  197. order_no: table_node[3],
  198. design_sku: table_node[4],
  199. pre_design_info: table_node[13],
  200. design_info: [{
  201. region: table_node[9],
  202. text: table_node[10],
  203. picture: table_node[11],//11
  204. }]
  205. }
  206. } else {
  207. table_boj.design_info.push({
  208. region: table_node[0],
  209. text: table_node[1],
  210. picture: table_node[2],//2
  211. })
  212. }
  213. // 循环结束
  214. if (index == response.tables.length - 1) {
  215. if (table_boj.design_info.length) {
  216. // 一个商品的定制信息
  217. tables_arr.push(table_boj)
  218. }
  219. }
  220. // good_info sku_info design_info: region text picture
  221. });
  222. console.log(tables_arr, "tables_arrddd")
  223. // 上传图片和文字的参数处理
  224. let pic_text_arr: any[] = [], img_values: any = {}
  225. tables_arr.forEach(item => {
  226. let order_no = "", data_params: any[] = [], pre_info = {};
  227. if (item.order_no && item.order_no != "-") {
  228. order_no = item.order_no.substring(0, 15); // 取前15位
  229. }
  230. if (order_no) {
  231. let pre_pic = ""
  232. if (item.pre_design_info && item.pre_design_info != '-') {
  233. let pre_pic1 = (item.pre_design_info.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  234. pre_pic = pre_pic1.split('&imageMogr2/thumbnail')[0]
  235. if (!img_values[pre_pic]) {
  236. img_values[pre_pic] = {
  237. src: pre_pic,
  238. width: '',
  239. height: '',
  240. order_no,
  241. file: ''
  242. }
  243. }
  244. if (pre_pic) {
  245. pre_info = {
  246. pre_pic,
  247. width: 400,
  248. height: 400
  249. }
  250. }
  251. } else {
  252. pre_pic = ""
  253. }
  254. }
  255. // 定制信息处理
  256. if (item.design_info.length) {
  257. item.design_info.forEach(items => {
  258. const param: any = {}
  259. if (order_no || item.design_sku) {
  260. let bg_pic = ""
  261. if (items.picture && items.picture != '-') {
  262. let bg_pic1 = (items.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  263. bg_pic = bg_pic1.split('&imageMogr2/thumbnail')[0]
  264. if (!img_values[bg_pic]) {
  265. img_values[bg_pic] = {
  266. src: bg_pic,
  267. width: '',
  268. height: ''
  269. }
  270. }
  271. if (bg_pic) {
  272. param['width'] = 400;
  273. param['height'] = 400;
  274. }
  275. } else {
  276. bg_pic = ""
  277. }
  278. let des_text = ""
  279. if (items.text && items.text != '-') {
  280. const des_text1 = items.text.split(";}")[1]
  281. des_text = des_text1.split("查看")[0]
  282. } else {
  283. des_text = ""
  284. }
  285. param['region'] = items.region
  286. param['text'] = des_text
  287. param['design_pic'] = bg_pic
  288. data_params.push(param)
  289. }
  290. })
  291. }
  292. if (order_no || item.design_sku) {
  293. pic_text_arr.push({
  294. order_no,
  295. design_sku: item.design_sku,
  296. pre_info,
  297. data_params
  298. })
  299. }
  300. })
  301. // console.log(img_values,'-img_valuesimg_values')
  302. let img_values1: any = {}, cIndex = 0
  303. Object.values(img_values).forEach((item, n) => {
  304. if (!img_values1[n]) {
  305. img_values1[n] = item
  306. }
  307. })
  308. const queueImg = () => {
  309. if (img_values1[cIndex]) {
  310. cIndex++
  311. queueImg()
  312. return
  313. {
  314. let img = new Image()
  315. img.onload = function () {
  316. img_values1[cIndex].width = img.width
  317. img_values1[cIndex].height = img.height
  318. img_values[img_values1[cIndex]?.src]['width'] = img.width
  319. img_values[img_values1[cIndex]?.src]['height'] = img.height
  320. cIndex++
  321. queueImg()
  322. }
  323. img.onerror = function () {
  324. cIndex++
  325. queueImg()
  326. }
  327. img.src = img_values1[cIndex]?.src
  328. }
  329. } else {
  330. getImgWh()
  331. }
  332. }
  333. queueImg()
  334. function getImgWh() {
  335. setBtnLoading(false)
  336. setLoading(false)
  337. // 筛选备货单号存在的值
  338. let pic_text_arr1 = pic_text_arr.filter(item => item.order_no && item.order_no != "-")
  339. // 为存在图片的对象添加宽高
  340. pic_text_arr1.map(item => {
  341. item.data_params.map((items: any) => {
  342. if (items.design_pic) {
  343. if (img_values[items.design_pic]) {
  344. items['width'] = img_values[items.design_pic].width
  345. items['height'] = img_values[items.design_pic].height
  346. items['file'] = img_values[items.design_pic].file
  347. }
  348. }
  349. })
  350. if (item.pre_info && item.pre_info.pre_pic) {
  351. item.pre_info.width = img_values[item.pre_info.pre_pic].width
  352. item.pre_info.height = img_values[item.pre_info.pre_pic].height
  353. }
  354. })
  355. // 数据显示
  356. let table_data: any[] = []
  357. pic_text_arr1.forEach(item => {
  358. if (item.data_params.length) {
  359. item.data_params.forEach((items: any) => {
  360. table_data.push({
  361. order_no: item.order_no,
  362. pre_info: item.pre_info,
  363. design_sku: item.design_sku,
  364. design_pic: items.design_pic,
  365. file: items.file,
  366. region: items.region,
  367. text: items.text,
  368. width: items.width || "",
  369. height: items.height || "",
  370. })
  371. })
  372. }
  373. })
  374. console.log(pic_text_arr1, "pic_text_arr1aaa", img_values1)
  375. setTableData(table_data)
  376. }
  377. } else {
  378. console.log("页面上未找到表格数据");
  379. CustomMessage.error("页面上未找到表格数据");
  380. }
  381. }
  382. } catch (error) {
  383. setBtnLoading(false)
  384. setLoading(false)
  385. console.error("获取表格数据失败:", error);
  386. CustomMessage.error("获取表格数据失败");
  387. }
  388. };
  389. interface imgValuesProps {
  390. [key: string]: {
  391. design_pic: string,
  392. url: string
  393. width: string | number
  394. height: string | number
  395. },
  396. }
  397. const uploadPic = () => {
  398. try {
  399. const token = localStorage.getItem("access_token");
  400. console.log(tableData, "tableData")
  401. let table_obj: { [key: string]: any } = {},
  402. img_values: imgValuesProps = {}, fetch_url = "";
  403. tableData.forEach(item => {
  404. if (item.design_pic && !img_values[item.design_pic]) {
  405. img_values[item.design_pic] = {
  406. design_pic: item.design_pic,
  407. url: '',
  408. width: '',
  409. height: ''
  410. }
  411. }
  412. if (!table_obj[item.design_sku]) {
  413. table_obj[item.design_sku] = ""
  414. }
  415. if (table_obj[item.design_sku]) {
  416. const data_params = table_obj[item.design_sku].data_params
  417. data_params.push({
  418. design_pic: item.design_pic,
  419. file: item.file || "",
  420. region: item.region,
  421. text: item.text,
  422. width: item.width,
  423. height: item.height,
  424. })
  425. table_obj[item.design_sku] = {
  426. ...table_obj[item.design_sku],
  427. data_params,
  428. }
  429. } else {
  430. table_obj[item.design_sku] = {
  431. order_no: item.order_no,
  432. design_sku: item.design_sku,
  433. data_params: [{
  434. design_pic: item.design_pic,
  435. file: item.file || '',
  436. region: item.region,
  437. text: item.text,
  438. width: item.width,
  439. height: item.height,
  440. }]
  441. }
  442. }
  443. })
  444. if (uploadUrl.includes('http')) {
  445. fetch_url = `${uploadUrl}photo/temuUploadStepTwo`
  446. } else {
  447. fetch_url = `https:${uploadUrl}photo/temuUploadStepTwo`
  448. }
  449. if (Object.values(img_values) && Object.values(img_values).length) {
  450. let queImgs: any = {}, queImgsCindex = 0;
  451. Object.values(img_values).forEach((v, cindex) => {
  452. if (!queImgs[cindex]) {
  453. queImgs[cindex] = {
  454. design_pic: v.design_pic
  455. }
  456. }
  457. })
  458. const imgUpdate = () => {
  459. if (queImgs[queImgsCindex]) {
  460. urlToFile(queImgs[queImgsCindex]?.design_pic, `${new Date().getTime()}.png`).then(file => {
  461. // photo/temuUploadStepTwo
  462. let form_data = new FormData()
  463. form_data.append('api_token', token as string)
  464. form_data.append('file', file)
  465. fetch(fetch_url, {
  466. method: 'POST',
  467. headers: {
  468. 'Content-Type': 'multipart/form-data',
  469. 'access-control-allow-credentials': 'true',
  470. 'X-CSRF-TOKEN': 'Bearer ' + token,
  471. 'Authorization': 'Bearer ' + token,
  472. },
  473. body: form_data,
  474. }).then(resjson => {
  475. return resjson.json();
  476. }).then(res => {
  477. console.log(res, '-resres')
  478. queImgsCindex++
  479. imgUpdate()
  480. })
  481. })
  482. }
  483. }
  484. imgUpdate()
  485. return
  486. }
  487. return
  488. console.log(table_obj, "table_obj", Object.values(table_obj))
  489. const table_params = Object.values(table_obj)
  490. let arr_index = 0
  491. if (!uploadUrl) {
  492. CustomMessage.warning("上传地址丢失,请再次点击获取定制数据!")
  493. return
  494. }
  495. console.log(table_params[arr_index], '-paramsparams')
  496. const FetchFun = (params: any) => {
  497. // console.log(params, "params")
  498. params['api_token'] = token
  499. let data_params = [], form_data = new FormData();
  500. // form_data.append('api_token', token)
  501. // form_data.append('order_no', table_params[arr_index].order_no)
  502. // form_data.append('design_sku', table_params[arr_index].design_sku)
  503. // table_params[arr_index].data_params.forEach((v:any, index:number) => {
  504. // form_data.append(`data_params[${index}]['design_pic']`, v.design_pic || '')
  505. // form_data.append(`data_params[${index}]['height']`, v.height || '')
  506. // form_data.append(`data_params[${index}]['width']`, v.width || '')
  507. // form_data.append(`data_params[${index}]['text']`, v.text || '')
  508. // form_data.append(`data_params[${index}]['region']`, v.region || '')
  509. // form_data.append(`data_params[${index}]['file']`, v.file || '')
  510. // })
  511. fetch(fetch_url, {
  512. method: 'POST',
  513. headers: {
  514. 'Content-Type': 'application/json',
  515. },
  516. body: JSON.stringify(params),
  517. }).then(resjson => {
  518. return resjson.json();
  519. }).then(res => {
  520. if (res.data.error && res.data.error.length) {
  521. // 未上传成功的 图片
  522. console.log(res.data.error, "arr_index", arr_index)
  523. res.data.error.forEach((item: any) => {
  524. CustomMessage.error(item.design_pic)
  525. })
  526. }
  527. arr_index++
  528. if (arr_index < table_params.length) {
  529. // tableData循环出备货单相同的数据,修改表格操作数据 is_upload
  530. let tableData1 = tableData.map(item => {
  531. if (item.order_no == params.order_no) {
  532. console.log(item.order_no, "item.order_no", params)
  533. item.is_upload = true
  534. }
  535. return item
  536. })
  537. // console.log(tableData1,"tableData111")
  538. setTableData(tableData1)
  539. FetchFun(table_params[arr_index])
  540. } else {
  541. setLoading(false)
  542. setTableData([])
  543. CustomMessage.success("图片上传结束")
  544. }
  545. }).catch(err => {
  546. console.log(err, "err")
  547. arr_index++
  548. if (arr_index < table_params.length) {
  549. FetchFun(table_params[arr_index])
  550. } else {
  551. setLoading(false)
  552. setTableData([])
  553. CustomMessage.success("图片上传结束")
  554. }
  555. })
  556. }
  557. setLoading(true)
  558. FetchFun(table_params[arr_index])
  559. } catch (error) {
  560. console.error(error, "报错")
  561. }
  562. }
  563. const uploadPic1 = () => {
  564. try {
  565. const token = localStorage.getItem("access_token");
  566. console.log(tableData, "tableData")
  567. let table_obj: { [key: string]: any } = {};
  568. tableData.forEach(item => {
  569. if (!table_obj[item.design_sku]) {
  570. table_obj[item.design_sku] = ""
  571. }
  572. if (table_obj[item.design_sku]) {
  573. const data_params = table_obj[item.design_sku].data_params
  574. data_params.push({
  575. design_pic: item.file,
  576. region: item.region,
  577. text: item.text,
  578. width: item.width,
  579. height: item.height,
  580. })
  581. table_obj[item.design_sku] = {
  582. ...table_obj[item.design_sku],
  583. data_params,
  584. }
  585. } else {
  586. table_obj[item.design_sku] = {
  587. order_no: item.order_no,
  588. design_sku: item.design_sku,
  589. data_params: [{
  590. design_pic: item.file,
  591. region: item.region,
  592. text: item.text,
  593. width: item.width,
  594. height: item.height,
  595. }]
  596. }
  597. }
  598. })
  599. console.log(table_obj, "table_obj", Object.values(table_obj))
  600. const table_params = Object.values(table_obj)
  601. let fetch_url = "", arr_index = 0, pic_index = 0;
  602. if (!uploadUrl) {
  603. CustomMessage.warning("上传地址丢失,请再次点击获取定制数据!")
  604. return
  605. }
  606. if (uploadUrl.includes('http')) {
  607. fetch_url = `${uploadUrl}photo/temuUpload`
  608. } else {
  609. fetch_url = `http:${uploadUrl}photo/temuUpload`
  610. }
  611. // 定制图片转为二进制
  612. const picFun = (item: any) => {
  613. let design_arr = item.data_params;
  614. console.log(design_arr, "design_arr")
  615. if (Array.isArray(design_arr) && design_arr.length) {
  616. const desFun = (items: any) => {
  617. if (items.design_pic && items.design_pic != '-') {
  618. } else {
  619. // 执行下一张图片
  620. des_index++
  621. if (design_arr[des_index]) {
  622. desFun(design_arr[des_index])
  623. } else {
  624. console.log(table_params[pic_index], "yyy")
  625. // 执行外部循环
  626. pic_index++
  627. if (table_params[pic_index]) {
  628. picFun(table_params[pic_index])
  629. } else {
  630. console.log(table_params, "table_paramssssss")
  631. }
  632. }
  633. }
  634. }
  635. let des_index = 0;
  636. desFun(design_arr[des_index])
  637. }
  638. }
  639. if (table_params[pic_index]) {
  640. picFun(table_params[pic_index])
  641. }
  642. return
  643. const FetchFun = async (params: any) => {
  644. // console.log(params, "params")
  645. // 创建 FormData 对象用于上传二进制文件
  646. const formData = new FormData();
  647. formData.append('api_token', token || '');
  648. formData.append('order_no', params.order_no);
  649. formData.append('design_sku', params.design_sku);
  650. formData.append('data_params', JSON.stringify(params.data_params));
  651. if (Array.isArray(params.data_params) && params.data_params.length) {
  652. let design_index = 0, design_files: any = [];
  653. const designFun = () => {
  654. let design_item = params.data_params[design_index];
  655. if (design_item && design_item.design_pic) {
  656. urlToFile(design_item.design_pic, `${params.order_no}_${design_item.region}.jpg`).then(file => {
  657. console.log(file, "file"); // 成功获取到 File 对象
  658. design_files.push({
  659. ...design_item,
  660. design_pic: file,
  661. })
  662. console.log(design_files, "design_files")
  663. design_index++
  664. if (params.data_params[design_index]) {
  665. designFun()
  666. }
  667. });
  668. }
  669. formData.append('design_files', JSON.stringify(design_files));
  670. console.log(formData, "formData")
  671. arr_index++
  672. if (arr_index < table_params.length) {
  673. // tableData循环出备货单相同的数据,修改表格操作数据 is_upload
  674. let tableData1 = tableData.map(item => {
  675. if (item.order_no == params.order_no) {
  676. console.log(item.order_no, "item.order_no", params)
  677. item.is_upload = true
  678. }
  679. return item
  680. })
  681. // console.log(tableData1,"tableData111")
  682. setTableData(tableData1)
  683. FetchFun(table_params[arr_index])
  684. } else {
  685. setLoading(false)
  686. setTableData([])
  687. CustomMessage.success("图片上传结束")
  688. }
  689. return
  690. fetch(fetch_url, {
  691. method: 'POST',
  692. // 注意:使用 FormData 时不要设置 Content-Type,浏览器会自动设置
  693. body: formData,
  694. }).then(response => response.json()).then(res => {
  695. if (res.data?.error?.length) {
  696. // 未上传成功的图片
  697. console.log(res.data.error, "arr_index", arr_index)
  698. res.data.error.forEach((item: any) => {
  699. CustomMessage.error(item.design_pic)
  700. })
  701. }
  702. arr_index++
  703. if (arr_index < table_params.length) {
  704. // tableData循环出备货单相同的数据,修改表格操作数据 is_upload
  705. let tableData1 = tableData.map(item => {
  706. if (item.order_no == params.order_no) {
  707. console.log(item.order_no, "item.order_no", params)
  708. item.is_upload = true
  709. }
  710. return item
  711. })
  712. // console.log(tableData1,"tableData111")
  713. setTableData(tableData1)
  714. FetchFun(table_params[arr_index])
  715. } else {
  716. setLoading(false)
  717. setTableData([])
  718. CustomMessage.success("图片上传结束")
  719. }
  720. }).catch(err => {
  721. console.log(err, "err")
  722. arr_index++
  723. if (arr_index < table_params.length) {
  724. FetchFun(table_params[arr_index])
  725. } else {
  726. setLoading(false)
  727. setTableData([])
  728. CustomMessage.success("图片上传结束")
  729. }
  730. })
  731. }
  732. designFun()
  733. } else {
  734. // 无定制信息情况
  735. setLoading(false)
  736. }
  737. }
  738. setLoading(true)
  739. FetchFun(table_params[arr_index])
  740. } catch (error) {
  741. console.error(error, "报错")
  742. }
  743. }
  744. const evChangeRadio = (e: any) => {
  745. let ev = e && e.target ? e.target.value : e
  746. setIsPreImg(ev)
  747. }
  748. return (
  749. <Loading visible={loading} fullScreen>
  750. <div className='w-3xl h-fit'>
  751. <Button
  752. type="primary"
  753. disabled={isPage || btnLoading}
  754. style={{ marginBottom: '10px', marginRight: '10px' }}
  755. onClick={() => {
  756. getPicData()
  757. }}>获取定制数据</Button>
  758. <Button
  759. disabled={(!tableData.length) || isPage || btnLoading}
  760. type="primary"
  761. style={{ marginBottom: '10px', marginRight: '10px' }}
  762. onClick={() => {
  763. uploadPic()
  764. }}>上传定制信息</Button>
  765. <span className='text-red-500'>ps:需在Temu定制商品内容页面获取数据,且上传图片过程中请勿关闭插件</span>
  766. <div className='flex items-center' style={{ marginBottom: '10px' }}>
  767. <p style={{ fontSize: 14, marginBottom: 0, marginRight: 5 }}>是否获取合成预览图: </p>
  768. <RadioGroup value={isPreImg} onChange={(e) => {
  769. evChangeRadio(e)
  770. }} aria-labelledby="groupId">
  771. <Radio value={1}>
  772. </Radio>
  773. <Radio value={-1}>
  774. </Radio>
  775. </RadioGroup>
  776. </div>
  777. <Table dataSource={dataSourceWithIndex}>
  778. <Table.Column title="序号" cell={order_render} align="center" />
  779. {
  780. isPreImg == 1 && <Table.Column title="定制区域图" cell={design_render} align="center" />
  781. }
  782. <Table.Column title="图片" cell={pic_render} align="center" />
  783. <Table.Column title="备货单" cell={name_render} align="center" />
  784. <Table.Column title="定制区域" cell={label_render} align="center" />
  785. <Table.Column title="文字" cell={text_render} align="center" />
  786. <Table.Column title="操作" cell={option_render} align="center" />
  787. </Table>
  788. <p className='text-red-500'>ps:若获取不到数据,可能是插件未连接成功,请刷新页面</p>
  789. </div>
  790. </Loading>
  791. )
  792. }