DesignPic_20250813174324.tsx 14 KB

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