DesignPic_20250813144433.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. order_no: any;
  110. design_info: { region: any; text: any; picture: any, cur_index: number }[];
  111. }[] = [];
  112. let table_boj: {
  113. good_info: any;
  114. sku_info: any;
  115. order_no: 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_info: []
  122. };
  123. response.tables.forEach((table_node: any, index: number) => {
  124. console.log(`表格 ${index + 1}:`, table_node);
  125. // 处理成接口参数
  126. // 处理成数组对象
  127. if (table_node.length > 4) {
  128. if (table_boj.design_info.length) {
  129. // 一个商品的定制信息
  130. tables_arr.push(table_boj)
  131. // 重置
  132. table_boj = {
  133. good_info: null,
  134. sku_info: null,
  135. order_no: null,
  136. design_info: []
  137. }
  138. }
  139. table_boj = {
  140. good_info: table_node[1],
  141. sku_info: table_node[2],
  142. order_no: table_node[3],
  143. design_info: [{
  144. region: table_node[9],
  145. text: table_node[10],
  146. picture: table_node[11],//11
  147. cur_index: index + 1,
  148. }]
  149. }
  150. } else {
  151. table_boj.design_info.push({
  152. region: table_node[0],
  153. text: table_node[1],
  154. picture: table_node[2],//2
  155. cur_index: index + 1,
  156. })
  157. }
  158. // good_info sku_info design_info: region text picture cur_index
  159. });
  160. console.log(tables_arr, "tables_arrddd")
  161. let params_arr: any[] = []
  162. tables_arr.forEach(item => {
  163. // const sku_info = item.sku_info?.text?.split('SKU:')
  164. let order_no = ""
  165. if (item.order_no && item.order_no != "-") {
  166. order_no = item.order_no.substring(0, 15); // 取前15位
  167. }
  168. if (item.design_info.length) {
  169. item.design_info.forEach(design_item => {
  170. const prarm = {
  171. title: '',
  172. label: '',
  173. category_id: '',
  174. category_name: '',
  175. file: '',
  176. cur_index: ''
  177. };
  178. if (order_no && 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 = `${order_no}-${design_item.region}`
  182. prarm.label = `${order_no}-${design_item.region}`
  183. prarm.file = bg_pic ?? '';
  184. params_arr.push(prarm)
  185. }
  186. })
  187. }
  188. })
  189. console.log(params_arr, "params_arr")
  190. setTableData(params_arr)
  191. // const table_arrs = response.tables // 表格数组
  192. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  193. // let table_boj: {
  194. // good_info: any;
  195. // sku_info: any;
  196. // design_info: { region: any; text: any; picture: any }[];
  197. // } = {
  198. // good_info: null,
  199. // sku_info: null,
  200. // design_info: []
  201. // };
  202. // while (i < response.tables.length) {
  203. // // 长度大于4时,指针改变
  204. // if (table_node.length > 4) {
  205. // if (table_boj.design_info.length) {
  206. // // 一个商品的定制信息
  207. // tables_arr.push(table_boj)
  208. // // 重置
  209. // table_boj = {
  210. // good_info: null,
  211. // sku_info: null,
  212. // design_info: []
  213. // }
  214. // }
  215. // table_boj = {
  216. // good_info: table_node[1],
  217. // sku_info: table_node[2],
  218. // design_info: [{
  219. // region: table_node[9],
  220. // text: table_node[10],
  221. // picture: table_node[11],
  222. // }]
  223. // }
  224. // i++
  225. // table_node = table_arrs[i]
  226. // } else {
  227. // table_boj.design_info.push({
  228. // region: table_node[0],
  229. // text: table_node[1],
  230. // picture: table_node[2],
  231. // })
  232. // i++
  233. // table_node = table_arrs[i]
  234. // }
  235. // }
  236. } else {
  237. console.log("页面上未找到表格数据");
  238. }
  239. }
  240. } catch (error) {
  241. console.error("获取表格数据失败:", error);
  242. }
  243. };
  244. const uploadPic = () => {
  245. const token = localStorage.getItem("access_token");
  246. // 在此处添加下标索引参数
  247. const table_datas = tableData.map((item, index) => {
  248. item.cur_index = index + 1
  249. item.api_token = token
  250. return item
  251. })
  252. let fetch_url = "", arr_index = 0
  253. if (uploadUrl.includes('http')) {
  254. fetch_url = `${uploadUrl}sheInPhoto/upload`
  255. } else {
  256. fetch_url = `http:${uploadUrl}sheInPhoto/upload`
  257. }
  258. const fetch_fn = (data: any) => {
  259. console.log(data, "data")
  260. const formData = new FormData()
  261. for (var key in data) {
  262. formData.append(key, data[key])
  263. }
  264. fetch(fetch_url, {
  265. method: 'POST',
  266. // headers: {
  267. // 'Content-Type': 'application/json',
  268. // },
  269. body: formData,
  270. mode: 'cors'
  271. }).then(resjson => {
  272. return resjson.json();
  273. }).then(res => {
  274. if (res.code == -1) {
  275. Message.error(res.msg)
  276. return
  277. }
  278. arr_index++
  279. if (arr_index < table_datas.length) {
  280. fetch_fn(table_datas[arr_index])
  281. } else {
  282. // 上传完成
  283. Message.success(res.msg)
  284. }
  285. }).catch(error => {
  286. Message.error(error.message)
  287. })
  288. }
  289. console.log(table_datas, "table_datasss")
  290. fetch_fn(table_datas[arr_index])
  291. // setTableData(tableData)
  292. }
  293. return (
  294. <div className='w-3xl h-fit'>
  295. <Button type="primary" style={{ marginBottom: '10px', marginRight: '10px' }}
  296. onClick={() => {
  297. getPicData()
  298. }}>获取定制图片数据</Button>
  299. <Button disabled={!tableData.length} type="primary" style={{ marginBottom: '10px' }}
  300. onClick={() => {
  301. uploadPic()
  302. }}>上传图片</Button>
  303. <Table dataSource={dataSourceWithIndex}>
  304. <Table.Column title="图片" cell={pic_render} />
  305. <Table.Column title="图片名称" cell={name_render} />
  306. <Table.Column title="图片标签" cell={label_render} />
  307. <Table.Column title="操作" cell={option_render} />
  308. </Table>
  309. </div>
  310. )
  311. }