DesignPic_20250813152600.tsx 12 KB

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