DesignPic_20250814102718.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import React, { useState } from 'react'
  2. import { Button, Table, Input, Message, Loading, Icon } from '@alifd/next';
  3. import { Success, Error, Warning, Info } from '@alifd/next/lib/icon'
  4. import '@alifd/next/dist/next.css'
  5. export default function DesignPic() {
  6. const [uploadUrl, setUploadUrl] = useState<string>("");
  7. // 添加状态管理表格数据
  8. const [tableData, setTableData] = useState<any[]>([]);
  9. const [loading, setLoading] = useState(false);
  10. const pic_render = (value: any, index: any, record: any) => {
  11. return (
  12. <>
  13. {
  14. record.file ? <>
  15. <img className='size-20' src={record.file}/></> : <>
  16. {/* <span>暂无图片</span> */}
  17. <Icon type="warning" />
  18. <Success/>
  19. </>
  20. }
  21. </>
  22. );
  23. }
  24. const text_render = (value: any, index: any, record: any) => {
  25. return (
  26. <p>{record.text}</p>
  27. );
  28. }
  29. // 修改图片名称渲染函数
  30. const name_render = (value: any, index: number, record: any) => {
  31. return <>
  32. <div>{record.design_sku}</div>
  33. </>
  34. };
  35. // 修改图片标签渲染函数
  36. const label_render = (value: any, index: number, record: any) => {
  37. return <>
  38. <div>{record.region}</div>
  39. </>
  40. };
  41. // 完善删除功能
  42. const handleDelete = (index: number, record: any) => {
  43. // 方案1: 直接删除
  44. const newData = [...tableData];
  45. newData.splice(index, 1);
  46. setTableData(newData);
  47. // 方案2: 带确认的删除 (可选)
  48. // if (window.confirm(`确定要删除 "${record.title.name}" 吗?`)) {
  49. // const newData = [...tableData];
  50. // newData.splice(index, 1);
  51. // setTableData(newData);
  52. // Message.success('删除成功');
  53. // }
  54. };
  55. // 删除操作渲染函数
  56. const option_render = (value: any, index: number, record: any) => {
  57. return (
  58. <a
  59. className='text-red-600 cursor-pointer'
  60. onClick={() => handleDelete(index, record)}
  61. >
  62. 删除
  63. </a>
  64. );
  65. };
  66. // 添加行索引以确保正确删除
  67. const dataSourceWithIndex = tableData.map((item, index) => ({
  68. ...item,
  69. index
  70. }));
  71. const getPicData = async () => {
  72. try {
  73. setLoading(true)
  74. // 获取上传路径
  75. fetch('http://merchant.landwu.com/api/photo/getUploadUrl', {
  76. method: 'POST',
  77. headers: {
  78. 'Content-Type': 'application/json',
  79. },
  80. body: JSON.stringify({
  81. api_token: localStorage.getItem('access_token'),
  82. }),
  83. }).then(resjson => {
  84. return resjson.json();
  85. }).then(res => {
  86. if (res.data) {
  87. const { data = {} } = res;
  88. const { url = null } = data;
  89. setUploadUrl(url)
  90. setLoading(false)
  91. }
  92. })
  93. // 获取当前活动标签页
  94. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  95. if (tab.id) {
  96. // 发送消息到content script请求获取tbody数据
  97. const response = await browser.tabs.sendMessage(tab.id, { action: "getTbodyData" });
  98. const table_datas = []
  99. // 在控制台输出结果
  100. if (response.tables && response.tables.length > 0) {
  101. console.log("找到的表格数据(包含背景图片):");
  102. // 显式声明类型
  103. let tables_arr: {
  104. good_info: any;
  105. sku_info: any;
  106. order_no: any;
  107. design_sku: any;
  108. design_info: { region: any; text: any; picture: any, cur_index: number }[];
  109. }[] = [];
  110. let table_boj: {
  111. good_info: any;
  112. sku_info: any;
  113. order_no: any;
  114. design_sku: any;
  115. design_info: { region: any; text: any; picture: any, cur_index: number }[];
  116. } = {
  117. good_info: null,
  118. sku_info: null,
  119. order_no: null,
  120. design_sku: null,
  121. design_info: []
  122. };
  123. response.tables.forEach((table_node: any, index: number) => {
  124. console.log(`表格 ${index + 1}:`, table_node);
  125. // 处理成接口参数
  126. // 处理成数组对象
  127. if (table_node.length > 4) {
  128. if (table_boj.design_info.length) {
  129. // 一个商品的定制信息
  130. tables_arr.push(table_boj)
  131. // 重置
  132. table_boj = {
  133. good_info: null,
  134. sku_info: null,
  135. order_no: null,
  136. design_sku: null,
  137. design_info: []
  138. }
  139. }
  140. table_boj = {
  141. good_info: table_node[1],
  142. sku_info: table_node[2],
  143. order_no: table_node[3],
  144. design_sku: table_node[4],
  145. design_info: [{
  146. region: table_node[9],
  147. text: table_node[10],
  148. picture: table_node[11],//11
  149. cur_index: index + 1,
  150. }]
  151. }
  152. } else {
  153. table_boj.design_info.push({
  154. region: table_node[0],
  155. text: table_node[1],
  156. picture: table_node[2],//2
  157. cur_index: index + 1,
  158. })
  159. // 循环结束
  160. if (index == response.tables.length - 1) {
  161. if (table_boj.design_info.length) {
  162. // 一个商品的定制信息
  163. tables_arr.push(table_boj)
  164. }
  165. }
  166. }
  167. // good_info sku_info design_info: region text picture cur_index
  168. });
  169. console.log(tables_arr, "tables_arrddd")
  170. // 上传图片的参数处理
  171. // let params_arr: any[] = []
  172. // tables_arr.forEach(item => {
  173. // // const sku_info = item.sku_info?.text?.split('SKU:')
  174. // let order_no = ""
  175. // if (item.order_no && item.order_no != "-") {
  176. // order_no = item.order_no.substring(0, 15); // 取前15位
  177. // }
  178. // if (item.design_info.length) {
  179. // item.design_info.forEach(design_item => {
  180. // const prarm = {
  181. // title: '',
  182. // label: '',
  183. // category_id: '',
  184. // category_name: '',
  185. // file: '',
  186. // cur_index: ''
  187. // };
  188. // if (order_no && design_item.picture && design_item.picture != '-') {
  189. // // const text = design_item.text.split('}')
  190. // const bg_pic = (design_item.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  191. // let bg_file = bg_pic.split('&imageMogr2/thumbnail/60x60')[0]
  192. // prarm.title = `${order_no}-${design_item.region}`
  193. // prarm.label = `${order_no}-${design_item.region}`
  194. // prarm.file = bg_file ?? '';
  195. // params_arr.push(prarm)
  196. // }
  197. // })
  198. // }
  199. // })
  200. // console.log(params_arr, "params_arr")
  201. // setTableData(params_arr)
  202. // 上传图片和文字的参数处理
  203. let pic_text_arr: any[] = []
  204. tables_arr.forEach(item => {
  205. let order_no = "", data_params: any[] = []
  206. if (item.order_no && item.order_no != "-") {
  207. order_no = item.order_no.substring(0, 15); // 取前15位
  208. }
  209. if (item.design_info.length) {
  210. item.design_info.forEach(items => {
  211. const param: any = {}
  212. if (order_no || item.design_sku) {
  213. let bg_pic = ""
  214. if (items.picture && items.picture != '-') {
  215. let bg_pic1 = (items.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  216. bg_pic = bg_pic1.split('&imageMogr2/thumbnail/60x60')[0]
  217. } else {
  218. bg_pic = ""
  219. }
  220. let des_text = ""
  221. if (items.text && items.text != '-') {
  222. const des_text1 = items.text.split(";}")[1]
  223. des_text = des_text1.split("查看")[0]
  224. } else {
  225. des_text = ""
  226. }
  227. param['region'] = items.region
  228. param['text'] = des_text
  229. param['design_pic'] = bg_pic
  230. data_params.push(param)
  231. }
  232. })
  233. }
  234. if (order_no || item.design_sku) {
  235. pic_text_arr.push({
  236. order_no,
  237. design_sku: item.design_sku,
  238. data_params
  239. })
  240. }
  241. })
  242. console.log(pic_text_arr, "pic_text_arr")
  243. // const params = {...pic_text_arr[0],api_token: localStorage.getItem('access_token')}
  244. // console.log(params,"params")
  245. // fetch(`http:${uploadUrl}photo/temuUpload `, {
  246. // method: 'POST',
  247. // headers: {
  248. // 'Content-Type': 'application/json',
  249. // },
  250. // body: JSON.stringify(params),
  251. // }).then(resjson => {
  252. // return resjson.json();
  253. // }).then(res => {
  254. // if (res.data) {
  255. // }
  256. // })
  257. // 数据显示
  258. let table_data: any[] = []
  259. pic_text_arr.forEach(item => {
  260. if (item.data_params.length) {
  261. item.data_params.forEach((items: any) => {
  262. table_data.push({
  263. order_no: item.order_no,
  264. design_sku: item.design_sku,
  265. file: items.design_pic,
  266. region: items.region,
  267. text: items.text,
  268. })
  269. })
  270. }
  271. })
  272. setTableData(table_data)
  273. // const table_arrs = response.tables // 表格数组
  274. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  275. // let table_boj: {
  276. // good_info: any;
  277. // sku_info: any;
  278. // design_info: { region: any; text: any; picture: any }[];
  279. // } = {
  280. // good_info: null,
  281. // sku_info: null,
  282. // design_info: []
  283. // };
  284. // while (i < response.tables.length) {
  285. // // 长度大于4时,指针改变
  286. // if (table_node.length > 4) {
  287. // if (table_boj.design_info.length) {
  288. // // 一个商品的定制信息
  289. // tables_arr.push(table_boj)
  290. // // 重置
  291. // table_boj = {
  292. // good_info: null,
  293. // sku_info: null,
  294. // design_info: []
  295. // }
  296. // }
  297. // table_boj = {
  298. // good_info: table_node[1],
  299. // sku_info: table_node[2],
  300. // design_info: [{
  301. // region: table_node[9],
  302. // text: table_node[10],
  303. // picture: table_node[11],
  304. // }]
  305. // }
  306. // i++
  307. // table_node = table_arrs[i]
  308. // } else {
  309. // table_boj.design_info.push({
  310. // region: table_node[0],
  311. // text: table_node[1],
  312. // picture: table_node[2],
  313. // })
  314. // i++
  315. // table_node = table_arrs[i]
  316. // }
  317. // }
  318. } else {
  319. console.log("页面上未找到表格数据");
  320. }
  321. }
  322. } catch (error) {
  323. console.error("获取表格数据失败:", error);
  324. }
  325. };
  326. const uploadPic = () => {
  327. const token = localStorage.getItem("access_token");
  328. console.log(tableData, "tableData")
  329. let table_obj: { [key: string]: any } = {};
  330. tableData.forEach(item => {
  331. if (table_obj[item.design_sku]) {
  332. const data_params = table_obj[item.design_sku].data_params
  333. data_params.push({
  334. design_pic: item.file,
  335. region: item.region,
  336. text: item.text
  337. })
  338. table_obj[item.design_sku] = {
  339. ...table_obj[item.design_sku],
  340. data_params,
  341. }
  342. } else {
  343. table_obj[item.design_sku] = {
  344. order_no: item.order_no,
  345. design_sku: item.design_sku,
  346. data_params: [{
  347. design_pic: item.file,
  348. region: item.region,
  349. text: item.text
  350. }]
  351. }
  352. }
  353. })
  354. console.log(table_obj, "table_obj", Object.values(table_obj))
  355. const table_params = Object.values(table_obj)
  356. let fetch_url = "", arr_index = 0
  357. if (uploadUrl.includes('http')) {
  358. fetch_url = `${uploadUrl}photo/temuUpload`
  359. } else {
  360. fetch_url = `http:${uploadUrl}photo/temuUpload`
  361. }
  362. const FetchFun = (params: any) => {
  363. console.log(params, "params")
  364. params['api_token'] = token
  365. fetch(fetch_url, {
  366. method: 'POST',
  367. headers: {
  368. 'Content-Type': 'application/json',
  369. },
  370. body: JSON.stringify(params),
  371. }).then(resjson => {
  372. return resjson.json();
  373. }).then(res => {
  374. if (res.data.error && res.data.error.length) {
  375. // 未上传成功的 图片
  376. console.log(res.data.error, "arr_index", arr_index)
  377. }
  378. arr_index++
  379. if (arr_index < table_params.length) {
  380. FetchFun(table_params[arr_index])
  381. } else {
  382. setLoading(false)
  383. Message.success("图片上传结束")
  384. }
  385. }).catch(err => {
  386. console.log(err, "err")
  387. arr_index++
  388. if (arr_index < table_params.length) {
  389. FetchFun(table_params[arr_index])
  390. } else {
  391. setLoading(false)
  392. Message.success("图片上传结束")
  393. }
  394. })
  395. }
  396. setLoading(true)
  397. FetchFun(table_params[arr_index])
  398. return
  399. // 在此处添加下标索引参数
  400. const table_datas = tableData.map((item, index) => {
  401. item.cur_index = index + 1
  402. item.api_token = token
  403. return item
  404. })
  405. if (!uploadUrl) {
  406. Message.error("图片上传路径未获取到,请重新点击获取定制图片数据按钮")
  407. return
  408. }
  409. const fetch_fn = (data: any) => {
  410. const formData = new FormData()
  411. for (var key in data) {
  412. formData.append(key, data[key])
  413. }
  414. fetch(fetch_url, {
  415. method: 'POST',
  416. // headers: {
  417. // 'Content-Type': 'application/json',
  418. // },
  419. body: formData,
  420. mode: 'cors'
  421. }).then(resjson => {
  422. return resjson.json();
  423. }).then(res => {
  424. if (res.code == -1) {
  425. Message.error(res.msg)
  426. }
  427. arr_index++
  428. if (arr_index < table_datas.length) {
  429. fetch_fn(table_datas[arr_index])
  430. } else {
  431. // 上传完成
  432. Message.success(res.msg)
  433. setLoading(false)
  434. }
  435. }).catch(error => {
  436. setLoading(false)
  437. Message.error(error.message)
  438. arr_index++
  439. if (arr_index < table_datas.length) {
  440. fetch_fn(table_datas[arr_index])
  441. }
  442. })
  443. }
  444. console.log(table_datas, "table_datasss")
  445. setLoading(true)
  446. fetch_fn(table_datas[arr_index])
  447. // setTableData(tableData)
  448. }
  449. return (
  450. <Loading visible={loading} fullScreen>
  451. <div className='w-3xl h-fit'>
  452. <Button type="primary" style={{ marginBottom: '10px', marginRight: '10px' }}
  453. onClick={() => {
  454. getPicData()
  455. }}>获取定制数据</Button>
  456. <Button disabled={!tableData.length} type="primary" style={{ marginBottom: '10px', marginRight: '10px' }}
  457. onClick={() => {
  458. uploadPic()
  459. }}>上传定制信息</Button>
  460. <span className='text-red-500'>ps:上传图片过程中请勿关闭插件</span>
  461. <Table dataSource={dataSourceWithIndex}>
  462. <Table.Column title="图片" cell={pic_render} />
  463. <Table.Column title="定制SKU" cell={name_render} />
  464. <Table.Column title="定制区域" cell={label_render} />
  465. <Table.Column title="文字" cell={text_render} />
  466. <Table.Column title="操作" cell={option_render} />
  467. </Table>
  468. </div>
  469. </Loading>
  470. )
  471. }