DesignPic_20250813103030.tsx 8.8 KB

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