DesignPic_20250813145924.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 = []
  194. tables_arr.forEach(item => {
  195. let order_no = ""
  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 && items.picture && items.picture != '-') {
  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. param['region'] = items.region
  210. param['text'] = items.text
  211. param['design_pic'] = bg_pic
  212. }
  213. })
  214. }
  215. })
  216. // const table_arrs = response.tables // 表格数组
  217. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  218. // let table_boj: {
  219. // good_info: any;
  220. // sku_info: any;
  221. // design_info: { region: any; text: any; picture: any }[];
  222. // } = {
  223. // good_info: null,
  224. // sku_info: null,
  225. // design_info: []
  226. // };
  227. // while (i < response.tables.length) {
  228. // // 长度大于4时,指针改变
  229. // if (table_node.length > 4) {
  230. // if (table_boj.design_info.length) {
  231. // // 一个商品的定制信息
  232. // tables_arr.push(table_boj)
  233. // // 重置
  234. // table_boj = {
  235. // good_info: null,
  236. // sku_info: null,
  237. // design_info: []
  238. // }
  239. // }
  240. // table_boj = {
  241. // good_info: table_node[1],
  242. // sku_info: table_node[2],
  243. // design_info: [{
  244. // region: table_node[9],
  245. // text: table_node[10],
  246. // picture: table_node[11],
  247. // }]
  248. // }
  249. // i++
  250. // table_node = table_arrs[i]
  251. // } else {
  252. // table_boj.design_info.push({
  253. // region: table_node[0],
  254. // text: table_node[1],
  255. // picture: table_node[2],
  256. // })
  257. // i++
  258. // table_node = table_arrs[i]
  259. // }
  260. // }
  261. } else {
  262. console.log("页面上未找到表格数据");
  263. }
  264. }
  265. } catch (error) {
  266. console.error("获取表格数据失败:", error);
  267. }
  268. };
  269. const uploadPic = () => {
  270. const token = localStorage.getItem("access_token");
  271. // 在此处添加下标索引参数
  272. const table_datas = tableData.map((item, index) => {
  273. item.cur_index = index + 1
  274. item.api_token = token
  275. return item
  276. })
  277. let fetch_url = "", arr_index = 0
  278. if (uploadUrl.includes('http')) {
  279. fetch_url = `${uploadUrl}sheInPhoto/upload`
  280. } else {
  281. fetch_url = `http:${uploadUrl}sheInPhoto/upload`
  282. }
  283. const fetch_fn = (data: any) => {
  284. console.log(data, "data")
  285. const formData = new FormData()
  286. for (var key in data) {
  287. formData.append(key, data[key])
  288. }
  289. fetch(fetch_url, {
  290. method: 'POST',
  291. // headers: {
  292. // 'Content-Type': 'application/json',
  293. // },
  294. body: formData,
  295. mode: 'cors'
  296. }).then(resjson => {
  297. return resjson.json();
  298. }).then(res => {
  299. if (res.code == -1) {
  300. Message.error(res.msg)
  301. return
  302. }
  303. arr_index++
  304. if (arr_index < table_datas.length) {
  305. fetch_fn(table_datas[arr_index])
  306. } else {
  307. // 上传完成
  308. Message.success(res.msg)
  309. }
  310. }).catch(error => {
  311. Message.error(error.message)
  312. })
  313. }
  314. console.log(table_datas, "table_datasss")
  315. fetch_fn(table_datas[arr_index])
  316. // setTableData(tableData)
  317. }
  318. return (
  319. <div className='w-3xl h-fit'>
  320. <Button type="primary" style={{ marginBottom: '10px', marginRight: '10px' }}
  321. onClick={() => {
  322. getPicData()
  323. }}>获取定制图片数据</Button>
  324. <Button disabled={!tableData.length} type="primary" style={{ marginBottom: '10px' }}
  325. onClick={() => {
  326. uploadPic()
  327. }}>上传图片</Button>
  328. <Table dataSource={dataSourceWithIndex}>
  329. <Table.Column title="图片" cell={pic_render} />
  330. <Table.Column title="图片名称" cell={name_render} />
  331. <Table.Column title="图片标签" cell={label_render} />
  332. <Table.Column title="操作" cell={option_render} />
  333. </Table>
  334. </div>
  335. )
  336. }