DesignPic_20250813174421.tsx 14 KB

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