DesignPic_20250813105544.tsx 8.6 KB

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