DesignPic_20250813152910.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. if (index == response.tables.length - 1) {
  159. if (table_boj.design_info.length) {
  160. // 一个商品的定制信息
  161. tables_arr.push(table_boj)
  162. }
  163. }
  164. }
  165. // good_info sku_info design_info: region text picture cur_index
  166. });
  167. console.log(tables_arr, "tables_arrddd")
  168. // 上传图片的参数处理
  169. let params_arr: any[] = []
  170. tables_arr.forEach(item => {
  171. // const sku_info = item.sku_info?.text?.split('SKU:')
  172. let order_no = ""
  173. if (item.order_no && item.order_no != "-") {
  174. order_no = item.order_no.substring(0, 15); // 取前15位
  175. }
  176. if (item.design_info.length) {
  177. item.design_info.forEach(design_item => {
  178. const prarm = {
  179. title: '',
  180. label: '',
  181. category_id: '',
  182. category_name: '',
  183. file: '',
  184. cur_index: ''
  185. };
  186. if (order_no && design_item.picture && design_item.picture != '-') {
  187. // const text = design_item.text.split('}')
  188. const bg_pic = (design_item.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  189. prarm.title = `${order_no}-${design_item.region}`
  190. prarm.label = `${order_no}-${design_item.region}`
  191. prarm.file = bg_pic ?? '';
  192. params_arr.push(prarm)
  193. }
  194. })
  195. }
  196. })
  197. console.log(params_arr, "params_arr")
  198. setTableData(params_arr)
  199. // 上传图片和文字的参数处理
  200. let pic_text_arr: any[] = []
  201. tables_arr.forEach(item => {
  202. let order_no = "", data_params: any[] = []
  203. if (item.order_no && item.order_no != "-") {
  204. order_no = item.order_no.substring(0, 15); // 取前15位
  205. }
  206. if (item.design_info.length) {
  207. item.design_info.forEach(items => {
  208. const param: any = {}
  209. if (order_no) {
  210. let bg_pic = ""
  211. if (items.picture && items.picture != '-') {
  212. bg_pic = (items.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  213. } else {
  214. bg_pic = ""
  215. }
  216. let des_text = ""
  217. if (items.text && items.text != '-') {
  218. const des_text1 = items.text.split(";}")[1]
  219. des_text = des_text1.split("查看")[0]
  220. } else {
  221. des_text = ""
  222. }
  223. param['region'] = items.region
  224. param['text'] = des_text
  225. param['design_pic'] = bg_pic
  226. data_params.push(param)
  227. }
  228. })
  229. }
  230. if (order_no) {
  231. pic_text_arr.push({
  232. order_no,
  233. data_params
  234. })
  235. }
  236. })
  237. console.log(pic_text_arr, "pic_text_arr")
  238. // const table_arrs = response.tables // 表格数组
  239. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  240. // let table_boj: {
  241. // good_info: any;
  242. // sku_info: any;
  243. // design_info: { region: any; text: any; picture: any }[];
  244. // } = {
  245. // good_info: null,
  246. // sku_info: null,
  247. // design_info: []
  248. // };
  249. // while (i < response.tables.length) {
  250. // // 长度大于4时,指针改变
  251. // if (table_node.length > 4) {
  252. // if (table_boj.design_info.length) {
  253. // // 一个商品的定制信息
  254. // tables_arr.push(table_boj)
  255. // // 重置
  256. // table_boj = {
  257. // good_info: null,
  258. // sku_info: null,
  259. // design_info: []
  260. // }
  261. // }
  262. // table_boj = {
  263. // good_info: table_node[1],
  264. // sku_info: table_node[2],
  265. // design_info: [{
  266. // region: table_node[9],
  267. // text: table_node[10],
  268. // picture: table_node[11],
  269. // }]
  270. // }
  271. // i++
  272. // table_node = table_arrs[i]
  273. // } else {
  274. // table_boj.design_info.push({
  275. // region: table_node[0],
  276. // text: table_node[1],
  277. // picture: table_node[2],
  278. // })
  279. // i++
  280. // table_node = table_arrs[i]
  281. // }
  282. // }
  283. } else {
  284. console.log("页面上未找到表格数据");
  285. }
  286. }
  287. } catch (error) {
  288. console.error("获取表格数据失败:", error);
  289. }
  290. };
  291. const uploadPic = () => {
  292. const token = localStorage.getItem("access_token");
  293. // 在此处添加下标索引参数
  294. const table_datas = tableData.map((item, index) => {
  295. item.cur_index = index + 1
  296. item.api_token = token
  297. return item
  298. })
  299. let fetch_url = "", arr_index = 0
  300. if (uploadUrl.includes('http')) {
  301. fetch_url = `${uploadUrl}sheInPhoto/upload`
  302. } else {
  303. fetch_url = `http:${uploadUrl}sheInPhoto/upload`
  304. }
  305. const fetch_fn = (data: any) => {
  306. console.log(data, "data")
  307. const formData = new FormData()
  308. for (var key in data) {
  309. formData.append(key, data[key])
  310. }
  311. fetch(fetch_url, {
  312. method: 'POST',
  313. // headers: {
  314. // 'Content-Type': 'application/json',
  315. // },
  316. body: formData,
  317. mode: 'cors'
  318. }).then(resjson => {
  319. return resjson.json();
  320. }).then(res => {
  321. if (res.code == -1) {
  322. Message.error(res.msg)
  323. return
  324. }
  325. arr_index++
  326. if (arr_index < table_datas.length) {
  327. fetch_fn(table_datas[arr_index])
  328. } else {
  329. // 上传完成
  330. Message.success(res.msg)
  331. }
  332. }).catch(error => {
  333. Message.error(error.message)
  334. })
  335. }
  336. console.log(table_datas, "table_datasss")
  337. fetch_fn(table_datas[arr_index])
  338. // setTableData(tableData)
  339. }
  340. return (
  341. <div className='w-3xl h-fit'>
  342. <Button type="primary" style={{ marginBottom: '10px', marginRight: '10px' }}
  343. onClick={() => {
  344. getPicData()
  345. }}>获取定制图片数据</Button>
  346. <Button disabled={!tableData.length} type="primary" style={{ marginBottom: '10px' }}
  347. onClick={() => {
  348. uploadPic()
  349. }}>上传图片</Button>
  350. <Table dataSource={dataSourceWithIndex}>
  351. <Table.Column title="图片" cell={pic_render} />
  352. <Table.Column title="图片名称" cell={name_render} />
  353. <Table.Column title="图片标签" cell={label_render} />
  354. <Table.Column title="操作" cell={option_render} />
  355. </Table>
  356. </div>
  357. )
  358. }