DesignPic_20250813174400.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import React, { useState } from 'react'
  2. import { Button, Table, Input, Message, Loading } from '@alifd/next';
  3. export default function DesignPic() {
  4. const [uploadUrl, setUploadUrl] = useState<string>("");
  5. // 添加状态管理表格数据
  6. const [tableData, setTableData] = useState<any[]>([]);
  7. const [loading, setLoading] = useState(false);
  8. const pic_render = (value: any, index: any, record: any) => {
  9. return (
  10. <img className='size-20' src={record.file} alt={record.cur_index} />
  11. );
  12. }
  13. const text_render = (value: any, index: any, record: any) => {
  14. return (
  15. <p>{record.text}</p>
  16. );
  17. }
  18. // 修改图片名称渲染函数
  19. const name_render = (value: any, index: number, record: any) => {
  20. return <>
  21. <div>{record.design_sku}</div>
  22. </>
  23. };
  24. // 完善删除功能
  25. const handleDelete = (index: number, record: any) => {
  26. // 方案1: 直接删除
  27. const newData = [...tableData];
  28. newData.splice(index, 1);
  29. setTableData(newData);
  30. // 方案2: 带确认的删除 (可选)
  31. // if (window.confirm(`确定要删除 "${record.title.name}" 吗?`)) {
  32. // const newData = [...tableData];
  33. // newData.splice(index, 1);
  34. // setTableData(newData);
  35. // Message.success('删除成功');
  36. // }
  37. };
  38. // 删除操作渲染函数
  39. const option_render = (value: any, index: number, record: any) => {
  40. return (
  41. <a
  42. className='text-red-600 cursor-pointer'
  43. onClick={() => handleDelete(index, record)}
  44. >
  45. 删除
  46. </a>
  47. );
  48. };
  49. // 添加行索引以确保正确删除
  50. const dataSourceWithIndex = tableData.map((item, index) => ({
  51. ...item,
  52. index
  53. }));
  54. const getPicData = async () => {
  55. try {
  56. setLoading(true)
  57. // 获取上传路径
  58. fetch('http://merchant.landwu.com/api/photo/getUploadUrl', {
  59. method: 'POST',
  60. headers: {
  61. 'Content-Type': 'application/json',
  62. },
  63. body: JSON.stringify({
  64. api_token: localStorage.getItem('access_token'),
  65. }),
  66. }).then(resjson => {
  67. return resjson.json();
  68. }).then(res => {
  69. if (res.data) {
  70. const { data = {} } = res;
  71. const { url = null } = data;
  72. setUploadUrl(url)
  73. setLoading(false)
  74. }
  75. })
  76. // 获取当前活动标签页
  77. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  78. if (tab.id) {
  79. // 发送消息到content script请求获取tbody数据
  80. const response = await browser.tabs.sendMessage(tab.id, { action: "getTbodyData" });
  81. const table_datas = []
  82. // 在控制台输出结果
  83. if (response.tables && response.tables.length > 0) {
  84. console.log("找到的表格数据(包含背景图片):");
  85. // 显式声明类型
  86. let tables_arr: {
  87. good_info: any;
  88. sku_info: any;
  89. order_no: any;
  90. design_sku: any;
  91. design_info: { region: any; text: any; picture: any, cur_index: number }[];
  92. }[] = [];
  93. let table_boj: {
  94. good_info: any;
  95. sku_info: any;
  96. order_no: any;
  97. design_sku: any;
  98. design_info: { region: any; text: any; picture: any, cur_index: number }[];
  99. } = {
  100. good_info: null,
  101. sku_info: null,
  102. order_no: null,
  103. design_sku: null,
  104. design_info: []
  105. };
  106. response.tables.forEach((table_node: any, index: number) => {
  107. console.log(`表格 ${index + 1}:`, table_node);
  108. // 处理成接口参数
  109. // 处理成数组对象
  110. if (table_node.length > 4) {
  111. if (table_boj.design_info.length) {
  112. // 一个商品的定制信息
  113. tables_arr.push(table_boj)
  114. // 重置
  115. table_boj = {
  116. good_info: null,
  117. sku_info: null,
  118. order_no: null,
  119. design_sku: null,
  120. design_info: []
  121. }
  122. }
  123. table_boj = {
  124. good_info: table_node[1],
  125. sku_info: table_node[2],
  126. order_no: table_node[3],
  127. design_sku: table_node[4],
  128. design_info: [{
  129. region: table_node[9],
  130. text: table_node[10],
  131. picture: table_node[11],//11
  132. cur_index: index + 1,
  133. }]
  134. }
  135. } else {
  136. table_boj.design_info.push({
  137. region: table_node[0],
  138. text: table_node[1],
  139. picture: table_node[2],//2
  140. cur_index: index + 1,
  141. })
  142. // 循环结束
  143. if (index == response.tables.length - 1) {
  144. if (table_boj.design_info.length) {
  145. // 一个商品的定制信息
  146. tables_arr.push(table_boj)
  147. }
  148. }
  149. }
  150. // good_info sku_info design_info: region text picture cur_index
  151. });
  152. console.log(tables_arr, "tables_arrddd")
  153. // 上传图片的参数处理
  154. // let params_arr: any[] = []
  155. // tables_arr.forEach(item => {
  156. // // const sku_info = item.sku_info?.text?.split('SKU:')
  157. // let order_no = ""
  158. // if (item.order_no && item.order_no != "-") {
  159. // order_no = item.order_no.substring(0, 15); // 取前15位
  160. // }
  161. // if (item.design_info.length) {
  162. // item.design_info.forEach(design_item => {
  163. // const prarm = {
  164. // title: '',
  165. // label: '',
  166. // category_id: '',
  167. // category_name: '',
  168. // file: '',
  169. // cur_index: ''
  170. // };
  171. // if (order_no && design_item.picture && design_item.picture != '-') {
  172. // // const text = design_item.text.split('}')
  173. // const bg_pic = (design_item.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  174. // let bg_file = bg_pic.split('&imageMogr2/thumbnail/60x60')[0]
  175. // prarm.title = `${order_no}-${design_item.region}`
  176. // prarm.label = `${order_no}-${design_item.region}`
  177. // prarm.file = bg_file ?? '';
  178. // params_arr.push(prarm)
  179. // }
  180. // })
  181. // }
  182. // })
  183. // console.log(params_arr, "params_arr")
  184. // setTableData(params_arr)
  185. // 上传图片和文字的参数处理
  186. let pic_text_arr: any[] = []
  187. tables_arr.forEach(item => {
  188. let order_no = "", data_params: any[] = []
  189. if (item.order_no && item.order_no != "-") {
  190. order_no = item.order_no.substring(0, 15); // 取前15位
  191. }
  192. if (item.design_info.length) {
  193. item.design_info.forEach(items => {
  194. const param: any = {}
  195. if (order_no || item.design_sku) {
  196. let bg_pic = ""
  197. if (items.picture && items.picture != '-') {
  198. let bg_pic1 = (items.picture.backgroundImages as { url: string }[] | undefined)?.[0]?.url ?? '';
  199. bg_pic = bg_pic1.split('&imageMogr2/thumbnail/60x60')[0]
  200. } else {
  201. bg_pic = ""
  202. }
  203. let des_text = ""
  204. if (items.text && items.text != '-') {
  205. const des_text1 = items.text.split(";}")[1]
  206. des_text = des_text1.split("查看")[0]
  207. } else {
  208. des_text = ""
  209. }
  210. param['region'] = items.region
  211. param['text'] = des_text
  212. param['design_pic'] = bg_pic
  213. data_params.push(param)
  214. }
  215. })
  216. }
  217. if (order_no || item.design_sku) {
  218. pic_text_arr.push({
  219. order_no,
  220. design_sku: item.design_sku,
  221. data_params
  222. })
  223. }
  224. })
  225. console.log(pic_text_arr, "pic_text_arr")
  226. // const params = {...pic_text_arr[0],api_token: localStorage.getItem('access_token')}
  227. // console.log(params,"params")
  228. // fetch(`http:${uploadUrl}photo/temuUpload `, {
  229. // method: 'POST',
  230. // headers: {
  231. // 'Content-Type': 'application/json',
  232. // },
  233. // body: JSON.stringify(params),
  234. // }).then(resjson => {
  235. // return resjson.json();
  236. // }).then(res => {
  237. // if (res.data) {
  238. // }
  239. // })
  240. // 数据显示
  241. let table_data: any[] = []
  242. pic_text_arr.forEach(item => {
  243. if (item.data_params.length) {
  244. item.data_params.forEach((items: any) => {
  245. table_data.push({
  246. order_no: item.order_no,
  247. design_sku: item.design_sku,
  248. file: items.design_pic,
  249. region: items.region,
  250. text: items.text,
  251. })
  252. })
  253. }
  254. })
  255. setTableData(table_data)
  256. // const table_arrs = response.tables // 表格数组
  257. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  258. // let table_boj: {
  259. // good_info: any;
  260. // sku_info: any;
  261. // design_info: { region: any; text: any; picture: any }[];
  262. // } = {
  263. // good_info: null,
  264. // sku_info: null,
  265. // design_info: []
  266. // };
  267. // while (i < response.tables.length) {
  268. // // 长度大于4时,指针改变
  269. // if (table_node.length > 4) {
  270. // if (table_boj.design_info.length) {
  271. // // 一个商品的定制信息
  272. // tables_arr.push(table_boj)
  273. // // 重置
  274. // table_boj = {
  275. // good_info: null,
  276. // sku_info: null,
  277. // design_info: []
  278. // }
  279. // }
  280. // table_boj = {
  281. // good_info: table_node[1],
  282. // sku_info: table_node[2],
  283. // design_info: [{
  284. // region: table_node[9],
  285. // text: table_node[10],
  286. // picture: table_node[11],
  287. // }]
  288. // }
  289. // i++
  290. // table_node = table_arrs[i]
  291. // } else {
  292. // table_boj.design_info.push({
  293. // region: table_node[0],
  294. // text: table_node[1],
  295. // picture: table_node[2],
  296. // })
  297. // i++
  298. // table_node = table_arrs[i]
  299. // }
  300. // }
  301. } else {
  302. console.log("页面上未找到表格数据");
  303. }
  304. }
  305. } catch (error) {
  306. console.error("获取表格数据失败:", error);
  307. }
  308. };
  309. const uploadPic = () => {
  310. const token = localStorage.getItem("access_token");
  311. // 在此处添加下标索引参数
  312. const table_datas = tableData.map((item, index) => {
  313. item.cur_index = index + 1
  314. item.api_token = token
  315. return item
  316. })
  317. if (!uploadUrl) {
  318. Message.error("图片上传路径未获取到,请重新点击获取定制图片数据按钮")
  319. return
  320. }
  321. let fetch_url = "", arr_index = 0
  322. if (uploadUrl.includes('http')) {
  323. fetch_url = `${uploadUrl}sheInPhoto/upload`
  324. } else {
  325. fetch_url = `http:${uploadUrl}sheInPhoto/upload`
  326. }
  327. const fetch_fn = (data: any) => {
  328. const formData = new FormData()
  329. for (var key in data) {
  330. formData.append(key, data[key])
  331. }
  332. fetch(fetch_url, {
  333. method: 'POST',
  334. // headers: {
  335. // 'Content-Type': 'application/json',
  336. // },
  337. body: formData,
  338. mode: 'cors'
  339. }).then(resjson => {
  340. return resjson.json();
  341. }).then(res => {
  342. if (res.code == -1) {
  343. Message.error(res.msg)
  344. }
  345. arr_index++
  346. if (arr_index < table_datas.length) {
  347. fetch_fn(table_datas[arr_index])
  348. } else {
  349. // 上传完成
  350. Message.success(res.msg)
  351. setLoading(false)
  352. }
  353. }).catch(error => {
  354. setLoading(false)
  355. Message.error(error.message)
  356. arr_index++
  357. if (arr_index < table_datas.length) {
  358. fetch_fn(table_datas[arr_index])
  359. }
  360. })
  361. }
  362. console.log(table_datas, "table_datasss")
  363. setLoading(true)
  364. fetch_fn(table_datas[arr_index])
  365. // setTableData(tableData)
  366. }
  367. return (
  368. <Loading visible={loading} fullScreen>
  369. <div className='w-3xl h-fit'>
  370. <Button type="primary" style={{ marginBottom: '10px', marginRight: '10px' }}
  371. onClick={() => {
  372. getPicData()
  373. }}>获取定制图片数据</Button>
  374. <Button disabled={!tableData.length} type="primary" style={{ marginBottom: '10px', marginRight: '10px' }}
  375. onClick={() => {
  376. uploadPic()
  377. }}>上传图片</Button>
  378. <span className='text-red-500'>ps:上传图片过程中请勿关闭插件</span>
  379. <Table dataSource={dataSourceWithIndex}>
  380. <Table.Column title="图片" cell={pic_render} />
  381. <Table.Column title="定制SKU" cell={name_render} />
  382. <Table.Column title="定制区域" cell={label_render} />
  383. <Table.Column title="文字" cell={text_render} />
  384. <Table.Column title="操作" cell={option_render} />
  385. </Table>
  386. </div>
  387. </Loading>
  388. )
  389. }