DesignPic_20250813105303.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import React, { useState } from 'react'
  2. import { Button, Table, Input, Message } from '@alifd/next';
  3. export default function DesignPic() {
  4. const [uploadUrl, setUploadUrl] = useState<string | null>(null);
  5. const [params, setParams] = useState<any[]>([]);
  6. // 添加状态管理表格数据
  7. const [tableData, setTableData] = useState(() => {
  8. const result = [];
  9. for (let i = 0; i < 5; i++) {
  10. result.push({
  11. // title: { name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible` },
  12. id: 100306660940 + i,
  13. time: 2e3 + i,
  14. // 添加名称和标签字段
  15. title: '1235',
  16. label: ''
  17. });
  18. }
  19. return result;
  20. });
  21. const pic_render = (value: any, index: any, record: any) => {
  22. return (
  23. <img className='size-20' src={record.url} alt={record.title} />
  24. );
  25. }
  26. // 修改图片名称渲染函数
  27. const name_render = (value: any, index: number, record: any) => {
  28. return <>
  29. <Input.TextArea
  30. value={record.title}
  31. onChange={(value) => handleNameChange(value, index)}
  32. composition
  33. placeholder={'请输入图片名称'}
  34. />
  35. </>
  36. };
  37. // 修改图片标签渲染函数
  38. const label_render = (value: any, index: number, record: any) => {
  39. return <>
  40. <Input.TextArea
  41. value={record.label}
  42. onChange={(value) => handleLabelChange(value, index)}
  43. composition
  44. placeholder={'请输入图片标签'}
  45. />
  46. </>
  47. };
  48. // 添加名称变化处理函数
  49. const handleNameChange = (value: string, index: number) => {
  50. const newData = [...tableData];
  51. newData[index].title = value;
  52. setTableData(newData);
  53. };
  54. // 添加标签变化处理函数
  55. const handleLabelChange = (value: string, index: number) => {
  56. const newData = [...tableData];
  57. newData[index].label = value;
  58. setTableData(newData);
  59. };
  60. // 完善删除功能
  61. const handleDelete = (index: number, record: any) => {
  62. // 方案1: 直接删除
  63. const newData = [...tableData];
  64. newData.splice(index, 1);
  65. setTableData(newData);
  66. // 方案2: 带确认的删除 (可选)
  67. // if (window.confirm(`确定要删除 "${record.title.name}" 吗?`)) {
  68. // const newData = [...tableData];
  69. // newData.splice(index, 1);
  70. // setTableData(newData);
  71. // Message.success('删除成功');
  72. // }
  73. };
  74. // 删除操作渲染函数
  75. const option_render = (value: any, index: number, record: any) => {
  76. return (
  77. <a
  78. className='text-red-600 cursor-pointer'
  79. onClick={() => handleDelete(index, record)}
  80. >
  81. 删除
  82. </a>
  83. );
  84. };
  85. // 添加行索引以确保正确删除
  86. const dataSourceWithIndex = tableData.map((item, index) => ({
  87. ...item,
  88. index
  89. }));
  90. const getPicData = async () => {
  91. try {
  92. // 获取上传路径
  93. fetch('http://merchant.landwu.com/api/photo/getUploadUrl', {
  94. method: 'POST',
  95. headers: {
  96. 'Content-Type': 'application/json',
  97. }
  98. }).then(resjson => {
  99. return resjson.json();
  100. }).then(res => {
  101. if (res.data) {
  102. const { data = {} } = res;
  103. const { url = null } = data;
  104. setUploadUrl(url)
  105. }
  106. })
  107. // 获取当前活动标签页
  108. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  109. if (tab.id) {
  110. // 发送消息到content script请求获取tbody数据
  111. const response = await browser.tabs.sendMessage(tab.id, { action: "getTbodyData" });
  112. const table_datas = []
  113. // 在控制台输出结果
  114. if (response.tables && response.tables.length > 0) {
  115. console.log("找到的表格数据(包含背景图片):");
  116. // 显式声明类型
  117. let tables_arr: {
  118. good_info: any;
  119. sku_info: any;
  120. design_info: { region: any; text: any; picture: any, cur_index: number }[];
  121. }[] = [];
  122. let table_boj: {
  123. good_info: any;
  124. sku_info: any;
  125. design_info: { region: any; text: any; picture: any, cur_index: number }[];
  126. } = {
  127. good_info: null,
  128. sku_info: null,
  129. design_info: []
  130. };
  131. response.tables.forEach((table_node: any, index: number) => {
  132. console.log(`表格 ${index + 1}:`, table_node);
  133. // 处理成接口参数
  134. // 处理成数组对象
  135. if (table_node.length > 4) {
  136. if (table_boj.design_info.length) {
  137. // 一个商品的定制信息
  138. tables_arr.push(table_boj)
  139. // 重置
  140. table_boj = {
  141. good_info: null,
  142. sku_info: null,
  143. design_info: []
  144. }
  145. }
  146. table_boj = {
  147. good_info: table_node[1],
  148. sku_info: table_node[2],
  149. design_info: [{
  150. region: table_node[9],
  151. text: table_node[10],
  152. picture: table_node[11],
  153. cur_index: index + 1,
  154. }]
  155. }
  156. } else {
  157. table_boj.design_info.push({
  158. region: table_node[0],
  159. text: table_node[1],
  160. picture: table_node[2],
  161. cur_index: index + 1,
  162. })
  163. }
  164. // good_info sku_info design_info: region text picture cur_index
  165. });
  166. console.log(tables_arr, "tables_arrddd")
  167. let params_arr: any[] = []
  168. tables_arr.forEach(item => {
  169. const prarm = {
  170. title: '',
  171. label: '',
  172. file: '',
  173. cur_index: ''
  174. };
  175. const sku_info = item.sku_info?.text?.split('SKU:')
  176. if (item.design_info.length) {
  177. item.design_info.forEach(design_item => {
  178. if (design_item.picture && design_item.picture != '-') {
  179. // const text = design_item.text.split('}')
  180. const bg_pic = (design_item.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url;
  181. prarm.title = `${sku_info[1]}-${design_item.region}`
  182. prarm.label = `${sku_info[1]}-${design_item.region}`
  183. prarm.file = bg_pic ?? '';
  184. params_arr.push(prarm)
  185. }
  186. })
  187. }
  188. })
  189. params_arr.map((item, index) => item.cur_index = index + 1)
  190. console.log(params_arr,"params_arr")
  191. setParams(params_arr)
  192. // const table_arrs = response.tables // 表格数组
  193. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  194. // let table_boj: {
  195. // good_info: any;
  196. // sku_info: any;
  197. // design_info: { region: any; text: any; picture: any }[];
  198. // } = {
  199. // good_info: null,
  200. // sku_info: null,
  201. // design_info: []
  202. // };
  203. // while (i < response.tables.length) {
  204. // // 长度大于4时,指针改变
  205. // if (table_node.length > 4) {
  206. // if (table_boj.design_info.length) {
  207. // // 一个商品的定制信息
  208. // tables_arr.push(table_boj)
  209. // // 重置
  210. // table_boj = {
  211. // good_info: null,
  212. // sku_info: null,
  213. // design_info: []
  214. // }
  215. // }
  216. // table_boj = {
  217. // good_info: table_node[1],
  218. // sku_info: table_node[2],
  219. // design_info: [{
  220. // region: table_node[9],
  221. // text: table_node[10],
  222. // picture: table_node[11],
  223. // }]
  224. // }
  225. // i++
  226. // table_node = table_arrs[i]
  227. // } else {
  228. // table_boj.design_info.push({
  229. // region: table_node[0],
  230. // text: table_node[1],
  231. // picture: table_node[2],
  232. // })
  233. // i++
  234. // table_node = table_arrs[i]
  235. // }
  236. // }
  237. } else {
  238. console.log("页面上未找到表格数据");
  239. }
  240. }
  241. } catch (error) {
  242. console.error("获取表格数据失败:", error);
  243. }
  244. };
  245. return (
  246. <div className='w-3xl h-fit'>
  247. <Button style={{ marginBottom: '10px' }} onClick={() => {
  248. getPicData()
  249. }}>获取图片数据</Button>
  250. <Table dataSource={dataSourceWithIndex}>
  251. <Table.Column title="图片" cell={pic_render} />
  252. <Table.Column title="图片名称" cell={name_render} />
  253. <Table.Column title="图片标签" cell={label_render} />
  254. <Table.Column title="操作" cell={option_render} />
  255. </Table>
  256. </div>
  257. )
  258. }