DesignPic_20250814100138.tsx 16 KB

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