DesignPic_20250814095747.tsx 16 KB

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