DesignPic_20250813150813.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // 上传图片的参数处理
  162. let params_arr: any[] = []
  163. tables_arr.forEach(item => {
  164. // const sku_info = item.sku_info?.text?.split('SKU:')
  165. let order_no = ""
  166. if (item.order_no && item.order_no != "-") {
  167. order_no = item.order_no.substring(0, 15); // 取前15位
  168. }
  169. if (item.design_info.length) {
  170. item.design_info.forEach(design_item => {
  171. const prarm = {
  172. title: '',
  173. label: '',
  174. category_id: '',
  175. category_name: '',
  176. file: '',
  177. cur_index: ''
  178. };
  179. if (order_no && design_item.picture && design_item.picture != '-') {
  180. // const text = design_item.text.split('}')
  181. const bg_pic = (design_item.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  182. prarm.title = `${order_no}-${design_item.region}`
  183. prarm.label = `${order_no}-${design_item.region}`
  184. prarm.file = bg_pic ?? '';
  185. params_arr.push(prarm)
  186. }
  187. })
  188. }
  189. })
  190. console.log(params_arr, "params_arr")
  191. setTableData(params_arr)
  192. // 上传图片和文字的参数处理
  193. let pic_text_arr: any[] = []
  194. tables_arr.forEach(item => {
  195. let order_no = "", data_params: any[] = []
  196. if (item.order_no && item.order_no != "-") {
  197. order_no = item.order_no.substring(0, 15); // 取前15位
  198. }
  199. if (item.design_info.length) {
  200. item.design_info.forEach(items => {
  201. const param: any = {}
  202. if (order_no) {
  203. let bg_pic = ""
  204. if (items.picture && items.picture != '-') {
  205. bg_pic = (items.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  206. } else {
  207. bg_pic = ""
  208. }
  209. let des_text = ""
  210. if (items.text && items.text != '-') {
  211. const des_text1 = items.text.split(";}")[1]
  212. des_text = des_text1.split("查看")[0]
  213. } else {
  214. des_text = ""
  215. }
  216. param['region'] = items.region
  217. param['text'] = des_text
  218. param['design_pic'] = bg_pic
  219. data_params.push(param)
  220. }
  221. })
  222. }
  223. if (order_no) {
  224. pic_text_arr.push({
  225. order_no,
  226. data_params
  227. })
  228. }
  229. })
  230. console.log(pic_text_arr,"pic_text_arr")
  231. // const table_arrs = response.tables // 表格数组
  232. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  233. // let table_boj: {
  234. // good_info: any;
  235. // sku_info: any;
  236. // design_info: { region: any; text: any; picture: any }[];
  237. // } = {
  238. // good_info: null,
  239. // sku_info: null,
  240. // design_info: []
  241. // };
  242. // while (i < response.tables.length) {
  243. // // 长度大于4时,指针改变
  244. // if (table_node.length > 4) {
  245. // if (table_boj.design_info.length) {
  246. // // 一个商品的定制信息
  247. // tables_arr.push(table_boj)
  248. // // 重置
  249. // table_boj = {
  250. // good_info: null,
  251. // sku_info: null,
  252. // design_info: []
  253. // }
  254. // }
  255. // table_boj = {
  256. // good_info: table_node[1],
  257. // sku_info: table_node[2],
  258. // design_info: [{
  259. // region: table_node[9],
  260. // text: table_node[10],
  261. // picture: table_node[11],
  262. // }]
  263. // }
  264. // i++
  265. // table_node = table_arrs[i]
  266. // } else {
  267. // table_boj.design_info.push({
  268. // region: table_node[0],
  269. // text: table_node[1],
  270. // picture: table_node[2],
  271. // })
  272. // i++
  273. // table_node = table_arrs[i]
  274. // }
  275. // }
  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. }