DesignPic_20250813113512.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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>("");
  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. body: JSON.stringify({
  85. api_token: localStorage.getItem('access_token'),
  86. }),
  87. }).then(resjson => {
  88. return resjson.json();
  89. }).then(res => {
  90. if (res.data) {
  91. const { data = {} } = res;
  92. const { url = null } = data;
  93. setUploadUrl(url)
  94. }
  95. })
  96. // 获取当前活动标签页
  97. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  98. if (tab.id) {
  99. // 发送消息到content script请求获取tbody数据
  100. const response = await browser.tabs.sendMessage(tab.id, { action: "getTbodyData" });
  101. const table_datas = []
  102. // 在控制台输出结果
  103. if (response.tables && response.tables.length > 0) {
  104. console.log("找到的表格数据(包含背景图片):");
  105. // 显式声明类型
  106. let tables_arr: {
  107. good_info: any;
  108. sku_info: 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. design_info: { region: any; text: any; picture: any, cur_index: number }[];
  115. } = {
  116. good_info: null,
  117. sku_info: null,
  118. design_info: []
  119. };
  120. response.tables.forEach((table_node: any, index: number) => {
  121. console.log(`表格 ${index + 1}:`, table_node);
  122. // 处理成接口参数
  123. // 处理成数组对象
  124. if (table_node.length > 4) {
  125. if (table_boj.design_info.length) {
  126. // 一个商品的定制信息
  127. tables_arr.push(table_boj)
  128. // 重置
  129. table_boj = {
  130. good_info: null,
  131. sku_info: null,
  132. design_info: []
  133. }
  134. }
  135. table_boj = {
  136. good_info: table_node[1],
  137. sku_info: table_node[2],
  138. design_info: [{
  139. region: table_node[9],
  140. text: table_node[10],
  141. picture: table_node[11],
  142. cur_index: index + 1,
  143. }]
  144. }
  145. } else {
  146. table_boj.design_info.push({
  147. region: table_node[0],
  148. text: table_node[1],
  149. picture: table_node[2],
  150. cur_index: index + 1,
  151. })
  152. }
  153. // good_info sku_info design_info: region text picture cur_index
  154. });
  155. console.log(tables_arr, "tables_arrddd")
  156. let params_arr: any[] = []
  157. tables_arr.forEach(item => {
  158. const prarm = {
  159. title: '',
  160. label: '',
  161. file: '',
  162. cur_index: ''
  163. };
  164. const sku_info = item.sku_info?.text?.split('SKU:')
  165. if (item.design_info.length) {
  166. item.design_info.forEach(design_item => {
  167. if (design_item.picture && design_item.picture != '-') {
  168. // const text = design_item.text.split('}')
  169. const bg_pic = (design_item.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url;
  170. prarm.title = `${sku_info[1]}-${design_item.region}`
  171. prarm.label = `${sku_info[1]}-${design_item.region}`
  172. prarm.file = bg_pic ?? '';
  173. params_arr.push(prarm)
  174. }
  175. })
  176. }
  177. })
  178. console.log(params_arr, "params_arr")
  179. setTableData(params_arr)
  180. // const table_arrs = response.tables // 表格数组
  181. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  182. // let table_boj: {
  183. // good_info: any;
  184. // sku_info: any;
  185. // design_info: { region: any; text: any; picture: any }[];
  186. // } = {
  187. // good_info: null,
  188. // sku_info: null,
  189. // design_info: []
  190. // };
  191. // while (i < response.tables.length) {
  192. // // 长度大于4时,指针改变
  193. // if (table_node.length > 4) {
  194. // if (table_boj.design_info.length) {
  195. // // 一个商品的定制信息
  196. // tables_arr.push(table_boj)
  197. // // 重置
  198. // table_boj = {
  199. // good_info: null,
  200. // sku_info: null,
  201. // design_info: []
  202. // }
  203. // }
  204. // table_boj = {
  205. // good_info: table_node[1],
  206. // sku_info: table_node[2],
  207. // design_info: [{
  208. // region: table_node[9],
  209. // text: table_node[10],
  210. // picture: table_node[11],
  211. // }]
  212. // }
  213. // i++
  214. // table_node = table_arrs[i]
  215. // } else {
  216. // table_boj.design_info.push({
  217. // region: table_node[0],
  218. // text: table_node[1],
  219. // picture: table_node[2],
  220. // })
  221. // i++
  222. // table_node = table_arrs[i]
  223. // }
  224. // }
  225. } else {
  226. console.log("页面上未找到表格数据");
  227. }
  228. }
  229. } catch (error) {
  230. console.error("获取表格数据失败:", error);
  231. }
  232. };
  233. const uploadPic = () => {
  234. const token = localStorage.getItem("access_token");
  235. // 在此处添加下标索引参数
  236. const table_datas = tableData.map((item, index) => {
  237. item.cur_index = index + 1
  238. item.api_token = token
  239. })
  240. let fetch_url = "", arr_index = 0
  241. if (uploadUrl.includes('http')) {
  242. fetch_url = `${uploadUrl}sheInPhoto/upload`
  243. } else {
  244. fetch_url = `http:${uploadUrl}sheInPhoto/upload`
  245. }
  246. const fetch_fn = (data: any) => {
  247. const formData = new FormData()
  248. for (var key in data) {
  249. formData.append(key, data[key])
  250. }
  251. fetch(fetch_url, {
  252. method: 'POST',
  253. // headers: {
  254. // 'Content-Type': 'application/json',
  255. // },
  256. body: formData,
  257. mode: 'cors'
  258. }).then(resjson => {
  259. return resjson.json();
  260. }).then(res => {
  261. if (res.code == -1) {
  262. Message.error(res.msg)
  263. return
  264. }
  265. arr_index++
  266. if (arr_index < table_datas.length) {
  267. fetch_fn(table_datas[arr_index])
  268. } else {
  269. // 上传完成
  270. Message.success(res.msg)
  271. }
  272. }).catch(error => {
  273. Message.error(error.message)
  274. })
  275. }
  276. fetch_fn(table_datas[arr_index])
  277. // setTableData(tableData)
  278. }
  279. return (
  280. <div className='w-3xl h-fit'>
  281. <Button style={{ marginBottom: '10px' }} onClick={() => {
  282. getPicData()
  283. }}>获取定制图片数据</Button>
  284. <Table dataSource={dataSourceWithIndex}>
  285. <Table.Column title="图片" cell={pic_render} />
  286. <Table.Column title="图片名称" cell={name_render} />
  287. <Table.Column title="图片标签" cell={label_render} />
  288. <Table.Column title="操作" cell={option_render} />
  289. </Table>
  290. <Button onClick={() => {
  291. uploadPic()
  292. }}>上传图片</Button>
  293. </div>
  294. )
  295. }