DesignPic_20250814102739.tsx 16 KB

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