导出订单

导出抖店订单

  1. // ==UserScript==
  2. // @name 导出订单
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description 导出抖店订单
  6. // @author 大头肥猫
  7. // @match https://fxg.jinritemai.com/ffa/morder/order/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=jinritemai.com
  9. // @grant none
  10. // @license MIT
  11. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
  12. // @require https://unpkg.com/xlsx/dist/xlsx.full.min.js
  13. // @require https://cdn.jsdelivr.net/npm/exceljs@4.4.0/dist/exceljs.min.js
  14. // @require https://cdn.jsdelivr.net/npm/file-saver@2.0.5/dist/FileSaver.min.js
  15. // ==/UserScript==
  16. ;(function () {
  17. 'use strict'
  18. // Your code here...
  19.  
  20. waitForElementToAppear('.auxo-table-tbody', () => {
  21. console.log('.auxo-table-tbody')
  22. if ($('.export-order').length == 0) {
  23. addButton('.index_batchOpWrap__paous')
  24. }
  25. if ($('.copy-img').length == 0) {
  26. addImageBtn()
  27. }
  28.  
  29. }, 2000)
  30. })()
  31.  
  32. const start = () => {
  33. findElement()
  34. // export2Excel()
  35. }
  36.  
  37. const addImageBtn = () => {
  38. const listItem = Array.from($('.auxo-table-row-level-1'))
  39.  
  40. for (let i = 0; i < listItem.length; i++) {
  41. const btnImageOutput = $('<button class="copy-img">复制图片</button>')
  42. $(listItem[i]).find('.auxo-table-selection-column').append(btnImageOutput)
  43. }
  44.  
  45. $('.auxo-table-tbody').on('click', 'button', (e) => {
  46. const imgUrl = $($(e.target).parent().parent().find('img')[0]).attr('src')
  47. copyImage(imgUrl)
  48. })
  49. }
  50.  
  51. const findElement = () => {
  52. const dataArr = []
  53. const orderList = Array.from($('.auxo-table-row-level-0'))
  54. const listItem = Array.from($('.auxo-table-row-level-1'))
  55.  
  56. for (let i = 0; i < orderList.length; i++) {
  57. const orderId = $(orderList[i]).attr('data-row-key')
  58. let buyerInfo,
  59. name,
  60. phone,
  61. address = ''
  62.  
  63. for (let j = 0; j < listItem.length; j++) {
  64. const orderItem = $(listItem.shift())
  65.  
  66. if (j == 0) {
  67. name = orderItem.find('span.index_infoItem__ESU0o').eq(0).text()
  68. phone = orderItem.find('span.index_infoItem__ESU0o').eq(1).text()
  69. address = orderItem.find('span.index_infoItem__ESU0o').eq(2).text()
  70. buyerInfo = [name, phone, address].toString()
  71. }
  72.  
  73. const orderSpec = orderItem.find('.style_desc__1MaH9').text()
  74. const orderTitle = orderItem.find('.style_name__3ChB9').text()
  75. const storeName = $('.index_userName__16Isl').text()
  76. const strs = orderSpec.split('x')
  77. const orderNum = strs[strs.length - 1]
  78. const orderPrice = orderItem.find('.table_yuan__3Govr').parent().text().slice(1, -2) * orderNum
  79.  
  80. const dataObj = {
  81. 订单ID: orderId,
  82. 姓名: storeName,
  83. 产品标题: orderTitle,
  84. 图片: '',
  85. 规格: orderSpec,
  86. 价格: orderPrice,
  87. 采购价: '',
  88. 空行: '',
  89. 地址: '张乐超18266967615 浙江省义乌市江东街道大元村马坊院7栋2单元地下室(张乐超)' + orderId,
  90. }
  91. //价格不为0且有地址时才push
  92. if (dataObj.价格 != 0) {
  93. dataArr.push(dataObj)
  94. }
  95.  
  96. if (orderItem.attr('class').indexOf('auxo-pair-group-row-last') != -1) {
  97. break
  98. }
  99. }
  100. }
  101. // 创建工作簿
  102. const workbook = XLSX.utils.book_new()
  103. // 创建工作表
  104. const worksheet = XLSX.utils.json_to_sheet(dataArr)
  105. // 将工作表添加到工作簿中
  106. XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1')
  107. // 导出 Excel 文件
  108. XLSX.writeFile(workbook, `${Date.now()}_orderList.xlsx`)
  109.  
  110. console.log(dataArr)
  111. }
  112.  
  113. const export2Excel = async () => {
  114. const wb = new ExcelJS.Workbook()
  115. const worksheet = wb.addWorksheet('sheet1')
  116.  
  117. const dataUrl = await url2Base64Async(
  118. '//p3-aio.ecombdimg.com/ecom-shop-material/PmrbjfLV_m_61fcd9da317266fb87573729a904881f_sx_293526_www800-800~tplv-qzsgku4lz6-fxg-image:480:q75.image'
  119. )
  120. const imageId = wb.addImage({ base64: dataUrl, extension: 'png' })
  121. worksheet.addImage(imageId, `${String.fromCharCode(65 + 0 * 6)}1:${String.fromCharCode(
  122. 70 + 0 * 6
  123. )}16`)
  124. wb.xlsx
  125. .writeBuffer()
  126. .then(buffer => saveAs(new Blob([buffer]), `${Date.now()}_feedback.xlsx`))
  127. .catch(err => console.log('Error writing excel export', err))
  128. }
  129.  
  130. const img2Base64 = image => {
  131. let canvas = document.createElement('canvas')
  132. let width = image.width
  133. let height = image.height
  134.  
  135. canvas.width = width
  136. canvas.height = height
  137. let context = canvas.getContext('2d')
  138. context.drawImage(image, 0, 0, width, height)
  139. return canvas.toDataURL('image/png')
  140. }
  141.  
  142. const url2Base64 = (url, callback) => {
  143. let image = new Image()
  144.  
  145. image.setAttribute('crossOrigin', 'Anonymous')
  146. image.src = url + '?v=' + Math.random()
  147. image.onload = function () {
  148. let dataURL = img2Base64(image)
  149. if (callback) {
  150. callback(dataURL)
  151. }
  152. }
  153. }
  154.  
  155. const url2Base64Async = url => {
  156. return new Promise((resolve, reject) => {
  157. url2Base64(url, data => {
  158. resolve(data)
  159. })
  160. })
  161. }
  162.  
  163. function copyImage(url) {
  164. // var imgSrc = 'https://p3-aio.ecombdimg.com/ecom-shop-material/ybrkWYBe_m_41539adae41be9823d1f358de7a76cc9_sx_420177_www800-800~tplv-qzsgku4lz6-fxg-image:480:q75.image'
  165. let imgSrc = url
  166.  
  167. var canvas = document.createElement('canvas')
  168. var ctx = canvas.getContext('2d')
  169. var img = new Image()
  170. img.setAttribute("crossOrigin",'Anonymous')
  171.  
  172. img.onload = function () {
  173. canvas.width = img.width
  174. canvas.height = img.height
  175. ctx.drawImage(img, 0, 0, img.width, img.height)
  176.  
  177. canvas.toBlob(blob => {
  178. let data = [new ClipboardItem({ [blob.type]: blob })]
  179. navigator.clipboard.write(data).then(
  180. () => {
  181. console.log('success')
  182. },
  183. err => {
  184. console.error(err)
  185. alert('复制失败:' + err)
  186. },
  187. );
  188.  
  189. })
  190.  
  191. }
  192.  
  193. img.src = imgSrc
  194. }
  195.  
  196. const addButton = parent => {
  197. const attrElement = $(parent)
  198. const btn = $('<button class="export-order">导出订单</button>')
  199.  
  200. attrElement.append(btn)
  201. btn.css('margin-left', '10px')
  202.  
  203.  
  204. btn.click(async event => {
  205. event.stopPropagation()
  206. event.preventDefault()
  207. start()
  208. })
  209. }
  210.  
  211. // 检查某个元素是否出现
  212. function waitForElementToAppear(elementSelector, callback, intervalMs) {
  213. var checkInterval = setInterval(function () {
  214. var element = $(elementSelector)
  215. if (element.length > 0) {
  216. callback(element)
  217. // clearInterval(checkInterval)
  218. }
  219. }, intervalMs)
  220. }