home.vue 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. <template>
  2. <div>
  3. <el-container style="user-select: none; height: 100vh;">
  4. <el-container>
  5. <!-- #333744 -->
  6. <el-aside width="180px" style="border-right: 1px solid #fafafa; background-color: #333744; overflow-x: hidden;">
  7. <p class="soft-name" style="-webkit-app-region: drag; color: #fff;">
  8. <img src="../assets/image/icon.png" class="soft-icon" />
  9. <span style="letter-spacing: 1px;">{{productName}}</span>
  10. </p>
  11. <el-menu :default-openeds="['a', 'b']" :default-active="menuIndex" @select="setMenuIndex" active-text-color="#409EFF" background-color="#333744" text-color="#fff" style="margin-top: 10px;">
  12. <el-submenu index="a">
  13. <template slot="title"><img src="../assets/image/m-download.png" class="m-image"/>图片下载</template>
  14. <el-menu-item index="3">
  15. <img src="../assets/image/m-tmall.png" class="m-image"/><span slot="title">天猫</span>
  16. </el-menu-item>
  17. <el-menu-item index="2">
  18. <img src="../assets/image/m-jd.png" class="m-image"/><span slot="title">京东</span>
  19. </el-menu-item>
  20. <el-menu-item index="4">
  21. <img src="../assets/image/m-taobao.png" class="m-image"/><span slot="title">淘宝</span>
  22. </el-menu-item>
  23. <el-menu-item index="1">
  24. <img src="../assets/image/m-alibaba.png" class="m-image"/><span slot="title">阿里巴巴</span>
  25. </el-menu-item>
  26. <el-menu-item index="10">
  27. <img src="../assets/image/m-chrome.png" class="m-image"/><span slot="title">其他网址</span>
  28. </el-menu-item>
  29. </el-submenu>
  30. <el-submenu index="b">
  31. <template slot="title"><img src="../assets/image/m-edit.png" class="m-image"/>图片处理</template>
  32. <el-menu-item index="2-1">
  33. <img src="../assets/image/m-geshi.png" class="m-image"/><span slot="title">格式转换</span>
  34. </el-menu-item>
  35. <el-menu-item index="2-2">
  36. <img src="../assets/image/m-yasuo.png" class="m-image"/><span slot="title">图片压缩</span>
  37. </el-menu-item>
  38. <el-menu-item index="2-3">
  39. <img src="../assets/image/m-chicun.png" class="m-image"/><span slot="title">修改尺寸</span>
  40. </el-menu-item>
  41. <el-menu-item index="2-5">
  42. <img src="../assets/image/m-shuiyin.png" class="m-image"/><span slot="title">添加水印</span>
  43. </el-menu-item>
  44. </el-submenu>
  45. </el-menu>
  46. </el-aside>
  47. <el-container>
  48. <el-header height="45px" style="background-color: #fafafa; padding: 0 10px;">
  49. <soft-header ref="headerRef" @update-soft="updateSoft()" @export-file="exportFile" @login-url="loginUrl" @clear-cache="clearCache"></soft-header>
  50. </el-header>
  51. <soft-img ref="imgRef" v-show="['2-1', '2-2', '2-3', '2-5'].indexOf(menuIndex) > -1" @open-vip="openVip" @check-authority="checkAuthority"></soft-img>
  52. <el-main v-show="['1', '2', '3', '4', '10'].indexOf(menuIndex) > -1" ref="el-main" style="background-color: #fafafa;">
  53. <template>
  54. <div class="content-top">
  55. <div>
  56. <el-button-group>
  57. <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline"
  58. @click="addVisible = true;">添加链接</el-button>
  59. <el-button type="primary" size="mini" icon="el-icon-upload"
  60. @click="pickLink()">导入链接</el-button>
  61. <el-button type="primary" size="mini" icon="el-icon-delete"
  62. @click="clearList()">清空链接</el-button>
  63. </el-button-group>
  64. <el-link type="info" style="margin-left: 20px; vertical-align:baseline;" @click="downloadExample()">模板下载<i class="el-icon-download"></i></el-link>
  65. </div>
  66. <el-row type="flex" style="align-items: center;">
  67. <div class="set-item">
  68. <span class="set-title">保存目录:</span>
  69. <el-input :title="downloadDir" ref="upload-input" size="mini" @focus="pickPath" placeholder="请选择输出目录" v-model="downloadDir" readonly style="width:200px;" prefix-icon="el-icon-folder"></el-input>
  70. <el-popover placement="bottom" popper-class="popper-open" trigger="hover" content="打开保存目录">
  71. <i class="el-icon-folder-opened" slot="reference" style="padding-left: 5px; cursor: pointer; font-size: 22px; vertical-align: middle;" @click="openFolder()"></i>
  72. </el-popover>
  73. </div>
  74. <el-button type="danger" @click="exportFile()" :loading="loading">开始下载</el-button>
  75. </el-row>
  76. </div>
  77. <div style="padding: 20px 20px 0 20px; height: calc(100% - 62px);">
  78. <el-row type="flex" justify="space-between">
  79. <div>
  80. <h3 style="display: inline-block;">
  81. <span v-if="menuIndex == '1'">阿里巴巴 - </span>
  82. <span v-if="menuIndex == '2'">京东 - </span>
  83. <span v-if="menuIndex == '3'">天猫 - </span>
  84. <span v-if="menuIndex == '4'">淘宝 - </span>
  85. <span v-if="menuIndex == '10'">网页 - </span>
  86. 图片下载
  87. </h3>
  88. <el-link v-if="menuIndex == '3' || menuIndex == '4'" :underline="false" type="danger" style="text-align: center; font-size: 12px;">
  89. (需登录{{menuIndex == '3' ? '天猫' : '淘宝'}}账号后才能下载)
  90. </el-link>
  91. <el-link v-if="menuIndex == '2'" :underline="false" type="danger" style="text-align: center; font-size: 12px;">
  92. (需登录京东账号后才能下载)
  93. </el-link>
  94. </div>
  95. <template v-if="menuIndex == '2' || menuIndex == '3' || menuIndex == '4'">
  96. <el-link v-if="tbStatus == 3 || jdStatus == 3" :underline="false" type="danger" style="text-align: center; font-size: 12px;">登录完成后请关闭浏览器</el-link>
  97. </template>
  98. </el-row>
  99. <div style="padding: 15px 0 20px;">
  100. <el-row type="flex" justify="space-between">
  101. <div v-if="menuIndex == '10'" style="padding-top: 10px;">
  102. <label>下载类型:</label>
  103. <el-checkbox :value="true" style="opacity: 0.6; cursor: not-allowed;">图片</el-checkbox>
  104. </div>
  105. <div v-if="menuIndex < 10" style="padding-top: 10px;">
  106. <label>下载类型:</label>
  107. <el-checkbox :value="true" style="opacity: 0.6; cursor: not-allowed;">主图</el-checkbox>
  108. <el-checkbox v-model="settingData.detailImg" >详情图</el-checkbox>
  109. <el-checkbox v-model="settingData.skuImg" >SKU图</el-checkbox>
  110. <el-checkbox v-model="settingData.commentImg" v-if="menuIndex == '3' || menuIndex == '4'">评论图</el-checkbox>
  111. <el-checkbox v-model="settingData.video" >视频</el-checkbox>
  112. </div>
  113. <!-- 天猫/淘宝 -->
  114. <template v-if="menuIndex == '3' || menuIndex == '4'">
  115. <div>
  116. <el-tag type="info" size="mini" v-if="tbStatus == 1">未检测</el-tag>
  117. <el-tag type="success" size="mini" v-if="tbStatus == 2">{{menuIndex == '3' ? '天猫' : '淘宝'}}账号已登录</el-tag>
  118. <el-link type="danger" style="text-decoration: underline;" v-if="tbStatus == 3" :underline="false" @click="loginUrl('https://login.taobao.com')">未登录,点击登录<span>{{menuIndex == '3' ? '天猫' : '淘宝'}}</span>账号</el-link>
  119. <el-button size="mini" type="warning" :loading="checkLoading" style="margin-left: 10px;" :disabled='tbStatus == 2' @click="checkLogin">检测登录状态</el-button>
  120. </div>
  121. </template>
  122. <!-- 京东账号 -->
  123. <template v-if="menuIndex == '2'">
  124. <div>
  125. <el-tag type="info" size="mini" v-if="jdStatus == 1">未检测</el-tag>
  126. <el-tag type="success" size="mini" v-if="jdStatus == 2">京东账号已登录</el-tag>
  127. <el-link type="danger" style="text-decoration: underline;" v-if="jdStatus == 3" :underline="false" @click="loginUrl('https://passport.jd.com/new/login.aspx')">未登录,点击登录京东账号</el-link>
  128. <el-button size="mini" type="warning" :loading="checkLoading" style="margin-left: 10px;" :disabled='jdStatus == 2' @click="checkJdLogin">检测登录状态</el-button>
  129. </div>
  130. </template>
  131. </el-row>
  132. </div>
  133. <div class="table-scroll">
  134. <!-- 1、 -->
  135. <vxe-table ref="xTable" show-overflow class="img-table" max-height="100%" empty-text="没有更多数据了!" :loading="tabLoading" :row-config="{isHover: true}"
  136. :loading-config="{icon: 'vxe-icon-indicator roll', text: '列表加载中...'}" :data="this[listStr+'List']" :scroll-y="{enabled: true}">
  137. <vxe-column type="seq" width="60"></vxe-column>
  138. <vxe-column field="title" title="目录名称" width="200">
  139. <template #default="{ row, rowIndex }">
  140. <span v-if="row.title">{{row.title}}</span>
  141. <el-tag size="mini" v-else>默认使用网页标题</el-tag>
  142. </template>
  143. </vxe-column>
  144. <vxe-column field="url" title="网页链接"></vxe-column>
  145. <vxe-column field="status" title="下载状态" width="200">
  146. <template #default="{ row }">
  147. <template v-if="row.status == '1'">
  148. <i class="el-icon-info" style="font-size: 16px; color: #999;"></i>
  149. <span>待操作</span>
  150. </template>
  151. <template v-if="row.status == '2'">
  152. <i class="el-icon-loading" style="font-size: 16px; color: #999;"></i>
  153. <span>任务处理中...</span>
  154. </template>
  155. <template v-if="row.status == '3'">
  156. <i class="el-icon-loading" style="font-size: 16px; color: #999;"></i>
  157. <span>图片下载中..<span v-if="row.num > 0">第{{row.num}}张</span></span>
  158. </template>
  159. <template v-if="row.status == '4'">
  160. <i class="el-icon-success" style="font-size: 16px; color: #19be6b;"></i>
  161. <span>下载完成</span>
  162. </template>
  163. <template v-if="row.status == '5'">
  164. <i class="el-icon-error" style="font-size: 16px; color: #ed4014;"></i>
  165. <span>网络异常,请重试!</span>
  166. </template>
  167. </template>
  168. </vxe-column>
  169. <vxe-column title="操作" width="80">
  170. <template #default="{ row, rowIndex }">
  171. <i class="el-icon-delete cur-pointer" style="font-size: 20px;" @click="delFile(rowIndex)"></i>
  172. </template>
  173. </vxe-column>
  174. </vxe-table>
  175. </div>
  176. </div>
  177. </template>
  178. <el-dialog title="添加链接" :visible.sync="addVisible" width="500px" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
  179. <div>
  180. <el-form label-position="right" label-width="80px" :rules="rules" :model="formData" ref="formData">
  181. <el-form-item label="目录名称" prop="title">
  182. <el-input v-model="formData.title" placeholder="为空则默认使用网页标题前20个字符"></el-input>
  183. </el-form-item>
  184. <el-form-item label="网页链接" prop="url">
  185. <el-input type="textarea" :rows="10" :placeholder="'请输入网址链接(例:' + exampleUrl[menuIndex-1] + ')'" v-model="formData.url"></el-input>
  186. </el-form-item>
  187. </el-form>
  188. </div>
  189. <span slot="footer" class="dialog-footer">
  190. <el-button @click="addVisible = false; $refs['formData'].resetFields();">取 消</el-button>
  191. <el-button type="primary" @click="onSubmit">确 定</el-button>
  192. </span>
  193. </el-dialog>
  194. <el-dialog title="提示" :visible.sync="loginVisible" width="400px" :close-on-click-modal="false" :close-on-press-escape="false">
  195. <div style="text-align: center; color: #999; font-size: 14px;">
  196. <p>{{menuIndex == '2' ? '京东' : '天猫/淘宝'}}渠道需要登录后才能下载</p>
  197. <p style="font-size: 18px; color: #f73131; font-weight: 600; padding: 30px 40px 0;">目前检测还未登录{{menuIndex == '2' ? '京东' : '天猫/淘宝'}}账号,需立即登录</p>
  198. </div>
  199. <div slot="footer" class="dialog-footer-center">
  200. <el-button v-if="menuIndex == '2'" @click="loginVisible = false; loginUrl('https://passport.jd.com/new/login.aspx')">点击登录京东账号</el-button>
  201. <el-button v-else @click="loginVisible = false; loginUrl('https://login.taobao.com')">点击登录天猫/淘宝账号</el-button>
  202. </div>
  203. </el-dialog>
  204. </el-main>
  205. <el-footer height="48px">
  206. <!-- 更新 -->
  207. <soft-update ref="updateRef" :showDowload="dowloadModel" :dowloadFinish="finishModel"></soft-update>
  208. </el-footer>
  209. </el-container>
  210. </el-container>
  211. </el-container>
  212. </div>
  213. </template>
  214. <script>
  215. import os from 'os'
  216. import fs from 'fs'
  217. import request from 'request'
  218. import path from 'path';
  219. import xlsx from 'node-xlsx';
  220. import softUpdate from './update.vue';
  221. import softHeader from './header.vue';
  222. import softImg from './img.vue';
  223. import electronApi from '@/utils/electronApi';
  224. import pjson from '/package.json'
  225. import puppeteer from 'puppeteer'
  226. let separator = '';
  227. if (os.platform == 'linux') {
  228. separator = '/'
  229. } else {
  230. separator = '\\'
  231. }
  232. // let chromePath = process.cwd() + '\\resources\\app\\node_modules\\puppeteer\\.local-chromium\\win64-1045629\\chrome-win\\chrome.exe';
  233. // if (process.env.NODE_ENV == 'development') {
  234. // chromePath = process.cwd() + '\\node_modules\\puppeteer\\.local-chromium\\win64-1045629\\chrome-win\\chrome.exe';
  235. // }
  236. export default {
  237. name: 'landing-page',
  238. components: {
  239. softUpdate,
  240. softHeader,
  241. softImg
  242. },
  243. data() {
  244. return {
  245. loginVisible: false,
  246. addVisible: false,
  247. formData: {
  248. title: '',
  249. url: ''
  250. },
  251. rules:{ //
  252. title: [
  253. { min: 0, max: 20, message: '不能超过 20 个字符', trigger: 'blur' },
  254. { pattern: /^[^\\/:*?"<>|]*$/, message: '标题中不能包含 \\ / : * ? " < > |', trigger: 'blur' }
  255. ],
  256. url: [
  257. { required: true, message: '请输入网页链接', trigger: 'blur' },
  258. { pattern: /^(http|https):\/\/.+$/, message: '请输入正确的网址链接(http://或https://开头)', trigger: 'blur' }
  259. ],
  260. },
  261. tabLoading: false,
  262. alibabaList: [],
  263. jdList: [],
  264. tbList: [],
  265. tmallList: [],
  266. commonList: [],
  267. productName: pjson.softInfo.softName,
  268. imgUrl: this.$api.imgUrl,
  269. imgSrc: '',
  270. menuIndex: '3',
  271. jdBrowser: '', // 打开京东的浏览器
  272. settingData: {
  273. detailImg: true,
  274. skuImg: true,
  275. commentImg: false,
  276. video: false,
  277. },
  278. exampleUrl: ['https://www.1688.com', 'https://www.jd.com', 'https://www.tmall.com', 'https://www.taobao.com'],
  279. fileList: [],
  280. downloadDir: os.userInfo().homedir + separator + "Downloads",
  281. dowloadModel: false,
  282. finishModel: false,
  283. loading: false,
  284. checkLoading: false, //点击检测登录状态
  285. tbStatus: 1, // 1、未检测 2、已经登录 3、未登录
  286. jdStatus: 1, // 1、未检测 2、已经登录 3、未登录
  287. execLimit: 2,
  288. execNum: 3, // 限制5张
  289. };
  290. },
  291. computed: {
  292. listStr: function(){
  293. let str = 'alibaba';
  294. switch(this.menuIndex){
  295. case '1':
  296. str = 'alibaba';
  297. break;
  298. case '2':
  299. str = 'jd';
  300. break;
  301. case '3':
  302. str = 'tmall';
  303. break;
  304. case '4':
  305. str = 'tb';
  306. break;
  307. case '10':
  308. str = 'common';
  309. break;
  310. }
  311. return str;
  312. }
  313. },
  314. async mounted() {
  315. this.$refs.updateRef.updateSoft(true);
  316. let homedir = os.userInfo().homedir;
  317. if (fs.existsSync(homedir + separator + "Desktop")) {
  318. this.downloadDir = homedir + separator + "Desktop"
  319. } else {
  320. this.downloadDir = homedir + separator + "Downloads"
  321. }
  322. if (!fs.existsSync(os.tmpdir() + separator + 'chrome-data-capture')) {
  323. fs.mkdirSync(os.tmpdir() + separator + 'chrome-data-capture');
  324. }
  325. if (!fs.existsSync(os.tmpdir() + separator + 'chrome-data-capture-jd')) {
  326. fs.mkdirSync(os.tmpdir() + separator + 'chrome-data-capture-jd');
  327. }
  328. let path = os.tmpdir() + separator + 'chrome-data-capture';
  329. let path2 = os.tmpdir() + separator + 'chrome-data-capture-jd';
  330. this.deleteAll(path, false);
  331. this.deleteAll(path2, false);
  332. // 打开浏览器
  333. const {
  334. shell
  335. } = require('electron');
  336. const links = document.querySelectorAll('a[href]');
  337. links.forEach(link => {
  338. link.addEventListener('click', e => {
  339. const url = link.getAttribute('href');
  340. e.preventDefault();
  341. shell.openExternal(url);
  342. });
  343. });
  344. },
  345. methods: {
  346. // 删除文件夹内容
  347. deleteAll(folderPath, flag) {
  348. if (fs.existsSync(folderPath)) {
  349. fs.readdirSync(folderPath).forEach((file, index) => {
  350. var curPath = path.join(folderPath, file);
  351. if (fs.lstatSync(curPath).isDirectory()) {
  352. this.deleteAll(curPath, true);
  353. } else {
  354. fs.unlinkSync(curPath);
  355. }
  356. });
  357. if(flag){
  358. fs.rmdirSync(folderPath);
  359. }
  360. }
  361. },
  362. checkAuthority(){
  363. let authority = this.$refs.headerRef.authority;
  364. this.$refs.imgRef.authority = authority;
  365. },
  366. setMenuIndex(index){
  367. this.menuIndex = index;
  368. if(index.indexOf('2-') > -1){
  369. let num = Number(index.split('-')[1]);
  370. this.$refs.imgRef.setMenuIndex(num);
  371. }
  372. },
  373. // 选择目录
  374. pickPath() {
  375. this.$refs['upload-input'].blur();
  376. electronApi.call('pickDir', []).then((path) => {
  377. if (path) {
  378. this.downloadDir = path;
  379. }
  380. });
  381. },
  382. // 打开自定义下载目录
  383. openFolder() {
  384. let path = this.downloadDir;
  385. if (fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName)) {
  386. path = this.downloadDir + separator + pjson.softInfo.softName;
  387. } else {
  388. fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName);
  389. path = this.downloadDir + separator + pjson.softInfo.softName;
  390. }
  391. electronApi.call('showItemInfolder', [path + '\\tty.tty'])
  392. },
  393. openVip() {
  394. this.$refs.headerRef.openVip();
  395. },
  396. updateSoft() {
  397. this.$refs.updateRef.updateSoft();
  398. },
  399. // 删除文件
  400. delFile(rowIndex){
  401. let type = 'alibaba';
  402. switch(this.menuIndex){
  403. case '1':
  404. type = 'alibaba';
  405. break;
  406. case '2':
  407. type = 'jd';
  408. break;
  409. case '3':
  410. type = 'tmall';
  411. break;
  412. case '4':
  413. type = 'tb';
  414. break;
  415. case '10':
  416. type = 'common';
  417. break;
  418. }
  419. this.$confirm('确认删除此行数据吗?', '提示', {
  420. confirmButtonText: '确定',
  421. cancelButtonText: '取消',
  422. type: 'warning'
  423. }).then(() => {
  424. this[type+'List'].splice(rowIndex, 1);
  425. if(this[type+'List'].length == 0) {
  426. this.clearList();
  427. }
  428. }).catch(() => {
  429. });
  430. },
  431. // 清除列表
  432. clearList() {
  433. switch(this.menuIndex){
  434. case '1':
  435. this.alibabaList = [];
  436. break;
  437. case '2':
  438. this.jdList = [];
  439. break;
  440. case '3':
  441. this.tmallList = [];
  442. break;
  443. case '4':
  444. this.tbList = [];
  445. break;
  446. case '10':
  447. this.commonList = [];
  448. break;
  449. }
  450. },
  451. // 提交表单
  452. onSubmit(){
  453. this.$refs['formData'].validate((valid) => {
  454. if (valid) {
  455. let info = {
  456. url: this.formData.url,
  457. title: this.formData.title,
  458. status: '1',
  459. num: 0,
  460. newPath: ''
  461. }
  462. switch(this.menuIndex){
  463. case '1':
  464. this.alibabaList.push(info);
  465. break;
  466. case '2':
  467. this.jdList.push(info);
  468. break;
  469. case '3':
  470. this.tmallList.push(info);
  471. break;
  472. case '4':
  473. this.tbList.push(info);
  474. break;
  475. case '10':
  476. this.commonList.push(info);
  477. break;
  478. }
  479. this.$refs['formData'].resetFields();
  480. this.addVisible = false;
  481. } else {
  482. return false;
  483. }
  484. });
  485. },
  486. async pickLink() {
  487. const spinLoad = this.$loading();
  488. await electronApi.call('pickFile', ['xlsx,xls', true]).then(async (path) => {
  489. spinLoad.close();
  490. if(path.length > 0){
  491. this.$notify({
  492. title: '提示',
  493. message: '导入成功',
  494. type: 'success'
  495. });
  496. let workSheetsFromBuffer = xlsx.parse(fs.readFileSync(path[0]));
  497. if(workSheetsFromBuffer && workSheetsFromBuffer.length > 0){
  498. let errMsg = "";
  499. workSheetsFromBuffer[0].data.map((item, index) => {
  500. if(item.length > 0){
  501. let ititle = '';
  502. if(item[0]){
  503. ititle = item[0].toString().trim();
  504. }
  505. if(item[0] == '目录名称' && item[1] == '网页链接'){
  506. return false;
  507. }
  508. if(this.containsAnyChar(ititle.toString(), ['\\', '/', ':', '*', '?', '"', '<', '>', '|'])){ //判断是否含有特殊字符
  509. errMsg += "第" + (index+1) + '行-名称不能包以下字符 \\ / : * ? " < > |';
  510. ititle = ititle.replace(/[\\|/|:|*|?|"|<|>||]/g, "");
  511. }
  512. if(item[1] == undefined){
  513. errMsg += "第" + (index+1) + "行格式有误</br>";
  514. item[1] = '';
  515. }else if(!/^(http|https):\/\/.+$/.exec(item[1])){
  516. errMsg += "第" + (index+1) + "行网址格式有误</br>";
  517. }else{
  518. let info = {
  519. url: item[1],
  520. title: ititle,
  521. status: '1',
  522. num: 0,
  523. newPath: ''
  524. }
  525. switch(this.menuIndex){
  526. case '1':
  527. this.alibabaList.push(info);
  528. break;
  529. case '2':
  530. this.jdList.push(info);
  531. break;
  532. case '3':
  533. this.tmallList.push(info);
  534. break;
  535. case '4':
  536. this.tbList.push(info);
  537. break;
  538. case '10':
  539. this.commonList.push(info);
  540. break;
  541. }
  542. }
  543. }
  544. });
  545. if(errMsg){
  546. this.$notify({
  547. title: '请检查文件以下问题',
  548. dangerouslyUseHTMLString: true,
  549. message: errMsg,
  550. type: 'warning'
  551. });
  552. }
  553. }else{
  554. this.$notify({
  555. title: '提示',
  556. message: '格式有误,请重新导入',
  557. type: 'warning'
  558. });
  559. }
  560. }
  561. });
  562. },
  563. // 清除缓存
  564. clearCache(){
  565. this.jdStatus = 3;
  566. this.tbStatus = 3;
  567. },
  568. // 去登录
  569. loginUrl(url){
  570. (async () => {
  571. try{
  572. if (!fs.existsSync(os.tmpdir() + separator + 'chrome-data-capture')) {
  573. fs.mkdirSync(os.tmpdir() + separator + 'chrome-data-capture');
  574. }
  575. if (!fs.existsSync(os.tmpdir() + separator + 'chrome-data-capture-jd')) {
  576. fs.mkdirSync(os.tmpdir() + separator + 'chrome-data-capture-jd');
  577. }
  578. let userDataDir = os.tmpdir() + separator + 'chrome-data-capture';
  579. if(url.indexOf('.jd.com/') > -1){
  580. userDataDir = os.tmpdir() + separator + 'chrome-data-capture-jd';
  581. }
  582. const tbBrowser = await puppeteer.launch({
  583. headless: false,
  584. executablePath: puppeteer.executablePath().replace('win32-1', 'win64-1'),
  585. args: ['--window-size=1280,800'],
  586. userDataDir: userDataDir,
  587. });
  588. const page = await tbBrowser.newPage();
  589. await page.setViewport({ width: 1280, height: 800 });
  590. await page.evaluateOnNewDocument(() => {
  591. const newProto = navigator.__proto__;
  592. delete newProto.webdriver;
  593. navigator.__proto__ = newProto;
  594. });
  595. await page.goto(url, {waitUntil : 'networkidle2'});
  596. }catch(e){
  597. this.showError(e);
  598. }
  599. })();
  600. },
  601. // 开始下载
  602. async exportFile(flag) {
  603. let authority = this.$refs.headerRef.authority.isAuthority;
  604. if (!fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName)) {
  605. fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName);
  606. }
  607. let fileList = [];
  608. switch(this.menuIndex){
  609. case '1': // 阿里巴巴
  610. fileList = this.alibabaList;
  611. break;
  612. case '2': // 京东
  613. fileList = this.jdList;
  614. break;
  615. case '3': // 天猫
  616. fileList = this.tmallList;
  617. break;
  618. case '4': // 淘宝
  619. fileList = this.tbList;
  620. break;
  621. case '10': // 普通网址
  622. fileList = this.commonList;
  623. break;
  624. }
  625. if(fileList.length > 0){
  626. if (!authority && !flag) { // 非会员点击转换弹出提示框
  627. this.$refs.headerRef.memberModel = true;
  628. this.$refs.headerRef.isClick = true;
  629. return false;
  630. } else {
  631. this.$refs.headerRef.memberModel = false;
  632. this.loading = true;
  633. setTimeout(() => {
  634. this.loading = false;
  635. }, 60000);
  636. }
  637. if (!fs.existsSync(os.tmpdir() + separator + 'chrome-data-capture')) {
  638. fs.mkdirSync(os.tmpdir() + separator + 'chrome-data-capture');
  639. }
  640. if(this.menuIndex == '2'){ // 京东
  641. if(this.jdStatus == 1 || this.jdStatus == 3){ // 未检测登录状态/或提示未登录状态,开始检测
  642. await this.checkJdLogin().then((data) => {
  643. if(data != 2){ // 未登录
  644. this.jdStatus = 3;
  645. this.loginVisible = true;
  646. }
  647. }).catch(err => {
  648. this.jdStatus = 3;
  649. });
  650. if(this.jdStatus == 3){
  651. this.loading = false;
  652. return false;
  653. }
  654. }
  655. }
  656. if(this.menuIndex == '3' || this.menuIndex == '4'){ // 天猫/淘宝
  657. if(this.tbStatus == 1 || this.tbStatus == 3){ // 未检测登录状态/或提示未登录状态,开始检测
  658. await this.checkLogin().then((data) => {
  659. if(data != 2){ // 未登录
  660. this.tbStatus = 3;
  661. this.loginVisible = true;
  662. }
  663. }).catch(err => {
  664. this.tbStatus = 3;
  665. });
  666. }
  667. if(this.tbStatus == 3){
  668. this.loading = false;
  669. return false;
  670. }
  671. }
  672. let taskArr = [];
  673. let task = "";
  674. for(let i = 0; i < fileList.length; i++){
  675. let item = fileList[i];
  676. switch(this.menuIndex){
  677. case '1': // 阿里巴巴
  678. task = this.alibabaDownload(item);
  679. break;
  680. case '2': // 京东
  681. task = this.jdScanImg(item);
  682. break;
  683. case '3': // 天猫
  684. case '4': // 淘宝
  685. task = this.tbScanImg(item);
  686. break;
  687. case '10': // 普通网址
  688. task = this.normalDownload(item);
  689. break;
  690. }
  691. if(task){
  692. taskArr.push(task);
  693. }
  694. if((i+1) % this.execLimit == 0){
  695. await Promise.all(taskArr).then(result => {
  696. taskArr = [];
  697. }).catch(err => {
  698. console.log('err'+i, err)
  699. })
  700. }
  701. }
  702. if(taskArr.length > 0){
  703. await Promise.all(taskArr).then(result => {
  704. taskArr = [];
  705. }).catch(err => {
  706. // 错误文件添加到服务中
  707. console.log('err',err)
  708. })
  709. }
  710. let tbLogin = true,jdLogin = true;
  711. if((this.menuIndex == '3' || this.menuIndex == '4') && this.tbStatus == 3){
  712. tbLogin = false;
  713. }
  714. if(this.menuIndex == '2' && this.jdStatus == 3){
  715. jdLogin = false;
  716. }
  717. this.loading = false;
  718. // 打开文件夹
  719. if(fileList.length > 0 && tbLogin && jdLogin){
  720. this.$message({message: '恭喜你,任务已完成!', type: 'success'});
  721. electronApi.call('showItemInfolder',[this.downloadDir + separator + pjson.softInfo.softName +'\\tty.tty'])
  722. }
  723. }
  724. },
  725. // 10-普通网址下载
  726. async normalDownload(urlInfo){
  727. let task = await new Promise((resolve,reject) =>{
  728. (async () => {
  729. try{
  730. let authority = this.$refs.headerRef.authority.isAuthority;
  731. let number = 0;
  732. urlInfo.status = '2';
  733. urlInfo.num = 0;
  734. const browser = await puppeteer.launch({
  735. executablePath: puppeteer.executablePath().replace('win32-1', 'win64-1'),
  736. userDataDir: os.tmpdir() + separator + 'chrome-data-capture',
  737. });
  738. const page = await browser.newPage();
  739. page.on('response', async(response) => {
  740. // 检查响应的 MIME 类型是否以 'image/' 开头
  741. if (response.headers()['content-type'] && response.headers()['content-type'].startsWith('image/') && response.headers()['content-length']) {
  742. let imgArr = ['gif', 'jpeg', 'png', 'webp', 'svg', 'tiff', 'bmp', 'ico', 'avif'];
  743. let imgType = 'jpg';
  744. let isBase = false;
  745. imgArr.map((item, index) => {
  746. if(response.headers()['content-type'].indexOf(item) > -1){
  747. imgType = item;
  748. if(item == 'jpeg'){
  749. imgType = 'jpg';
  750. }else if(item == 'avif'){
  751. imgType = 'png';
  752. }
  753. }
  754. });
  755. let url = response.url();
  756. let regex = /^data:image\/[\w|+|-]+;base64,/;
  757. if(regex.exec(url)){
  758. url = response.url().replace(/^data:image\/[\w|+|-]+;base64,/, '');
  759. isBase = true;
  760. }
  761. let imgInfo = {
  762. url: url,
  763. contentType: response.headers()['content-type'],
  764. status: response.status(),
  765. imgType: imgType,
  766. isBase: isBase
  767. }
  768. if(urlInfo.title){
  769. if (fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title)) {
  770. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title;
  771. } else {
  772. fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title);
  773. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title;
  774. }
  775. }else{
  776. await this.getTitle(page, urlInfo); // 生成页面标题对应的文件夹
  777. }
  778. let outputPath = urlInfo.newPath + '\\' + this.randomString(35) + '.' + imgInfo.imgType;
  779. urlInfo.status = '3';
  780. number++;
  781. if(!authority && number <= this.execNum){
  782. if(imgInfo.isBase){ //base64位图片下载
  783. this.downloadBaseImage(imgInfo.url, outputPath, urlInfo)
  784. }else{
  785. this.downloadImage(imgInfo.url, outputPath, urlInfo);
  786. }
  787. }
  788. if(authority){
  789. if(imgInfo.isBase){ //base64位图片下载
  790. this.downloadBaseImage(imgInfo.url, outputPath, urlInfo)
  791. }else{
  792. this.downloadImage(imgInfo.url, outputPath, urlInfo);
  793. }
  794. }
  795. }
  796. });
  797. await page.goto(urlInfo.url, {waitUntil : 'networkidle2'});
  798. let pageInfo = await page.evaluate(() => {
  799. let cHeight = document.documentElement.clientHeight;
  800. let scrollHeight = document.body.scrollHeight;
  801. return {'scrollHeight': scrollHeight, 'cHeight': cHeight}
  802. });
  803. let scrollHeight = pageInfo.scrollHeight;
  804. let cHeight = pageInfo.cHeight;
  805. let num = Math.ceil(scrollHeight / cHeight);
  806. let start = -1;
  807. let scrollInt = setInterval(async() => {
  808. start ++;
  809. await page.evaluate((start) => {
  810. let cHeight = document.documentElement.clientHeight;
  811. window.scrollTo({
  812. top: cHeight * start,
  813. behavior: "smooth"
  814. });
  815. }, start);
  816. if(start > num || start > 200){ // 防止页面过长,滚动200次自动停止
  817. clearInterval(scrollInt);
  818. await browser.close();
  819. urlInfo.status = '4';
  820. resolve(true);
  821. this.loading = false;
  822. }
  823. }, 500);
  824. }catch(e){
  825. urlInfo.status = '5';
  826. reject(e);
  827. this.showError(e);
  828. }
  829. })();
  830. });
  831. },
  832. // 1 - 阿里巴巴下载
  833. async alibabaDownload(urlInfo){
  834. let task = await new Promise((resolve,reject) =>{
  835. (async () => {
  836. try{
  837. let authority = this.$refs.headerRef.authority.isAuthority;
  838. urlInfo.status = '2';
  839. urlInfo.num = 0;
  840. const browser = await puppeteer.launch({
  841. executablePath: puppeteer.executablePath().replace('win32-1', 'win64-1'),
  842. userDataDir: os.tmpdir() + separator + 'chrome-data-capture',
  843. });
  844. const page = await browser.newPage();
  845. await page.goto(urlInfo.url, {waitUntil : 'networkidle2'});
  846. if(urlInfo.title){
  847. if (fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title)) {
  848. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title;
  849. } else {
  850. fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title);
  851. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title;
  852. }
  853. }else{
  854. await this.getTitle(page, urlInfo); // 生成页面标题对应的文件夹
  855. }
  856. let pageInfo = await page.evaluate(() => {
  857. let cHeight = document.documentElement.clientHeight;
  858. let scrollHeight = document.body.scrollHeight;
  859. return {'scrollHeight': scrollHeight, 'cHeight': cHeight}
  860. });
  861. let scrollHeight = pageInfo.scrollHeight;
  862. let cHeight = pageInfo.cHeight;
  863. let num = Math.ceil(scrollHeight / cHeight);
  864. let start = -1;
  865. urlInfo.status = '3';
  866. let scrollInt = setInterval(async() => {
  867. start ++;
  868. await page.evaluate((start) => {
  869. let cHeight = document.documentElement.clientHeight;
  870. window.scrollTo({
  871. top: cHeight * start,
  872. behavior: "smooth"
  873. });
  874. }, start);
  875. if(start > num || start > 200){ // 防止页面过长,滚动200次自动停止
  876. clearInterval(scrollInt);
  877. await this.alibabaScanImg(browser, page, urlInfo, authority);
  878. this.loading = false;
  879. urlInfo.status = '4';
  880. resolve(true);
  881. }
  882. }, 500);
  883. }catch(e){
  884. urlInfo.status = '5';
  885. reject(e);
  886. this.showError(e);
  887. }
  888. })();
  889. });
  890. },
  891. // 检查京东登录状态
  892. checkJdLogin(){
  893. this.checkLoading = true;
  894. this.jdStatus = 1;
  895. return new Promise((resolve, reject) => {
  896. (async () => {
  897. try{
  898. const jdBrowser = await puppeteer.launch({
  899. executablePath: puppeteer.executablePath().replace('win32-1', 'win64-1'),
  900. userDataDir: os.tmpdir() + separator + 'chrome-data-capture-jd',
  901. });
  902. const page = await jdBrowser.newPage();
  903. let testUrl = 'https://www.jd.com';
  904. await page.goto(testUrl, {waitUntil : 'networkidle2'});
  905. let loginInfo = await page.evaluate(() => {
  906. let navTags = document.querySelector('.link-login');
  907. let userTags = document.querySelector('.nickname');
  908. let userTags2 = document.querySelector('.nick'); //企业账号
  909. if(navTags && navTags.innerHTML.indexOf('请登录') > -1){
  910. return false;
  911. }else if(userTags || userTags2){
  912. return true;
  913. }else{
  914. return false;
  915. }
  916. });
  917. if(loginInfo){
  918. this.jdStatus = 2;
  919. }else{
  920. this.jdStatus = 3;
  921. }
  922. resolve(this.jdStatus);
  923. this.checkLoading = false;
  924. jdBrowser.close();
  925. }catch(e){
  926. reject(3);
  927. this.showError(e);
  928. }
  929. })();
  930. });
  931. },
  932. // 京东下载
  933. async jdScanImg(urlInfo){
  934. let task = await new Promise((resolve,reject) =>{
  935. (async () => {
  936. try{
  937. let authority = this.$refs.headerRef.authority.isAuthority;
  938. urlInfo.status = '2';
  939. urlInfo.num = 0;
  940. const jdBrowser = await puppeteer.launch({
  941. executablePath: puppeteer.executablePath().replace('win32-1', 'win64-1'),
  942. userDataDir: os.tmpdir() + separator + 'chrome-data-capture-jd',
  943. defaultViewport: null,
  944. args: [
  945. '--start-maximized',
  946. '--no-sandbox',
  947. '--disable-setuid-sandbox',
  948. '--disable-blink-features=AutomationControlled',
  949. ]
  950. });
  951. const page = await jdBrowser.newPage();
  952. await page.evaluateOnNewDocument(() => {
  953. const newProto = navigator.__proto__;
  954. delete newProto.webdriver;
  955. navigator.__proto__ = newProto;
  956. });
  957. let jdImgInfo = {
  958. mainImg: [],
  959. skuImg: [],
  960. commentImg: [],
  961. detailImg: [],
  962. video: []
  963. };
  964. let titleFlag = true;
  965. let skuNum = 0, mainNum = 0, videoNum = 0;
  966. page.on('response', async(response) => {
  967. if(titleFlag){ // 第一次生成页面标题
  968. if(urlInfo.title){
  969. if (fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title)) {
  970. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title;
  971. } else {
  972. fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title);
  973. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title;
  974. }
  975. }else{
  976. await this.getTitle(page, urlInfo); // 生成页面标题对应的文件夹
  977. }
  978. titleFlag = false;
  979. }
  980. // 检查响应的 MIME 类型是否以 'image/' 开头
  981. if (response.headers()['content-type'] && response.headers()['content-type'].startsWith('application/json')) {
  982. let detailUrl = '';
  983. let videoUrl = '';
  984. if(response.url().indexOf('/description/') > -1){ // 商品详情接口
  985. detailUrl = response.url();
  986. }
  987. if(response.url().indexOf('/tencent/video_v3') > -1){ // 商品视频接口
  988. videoUrl = response.url();
  989. }
  990. urlInfo.status = '3';
  991. if(detailUrl && this.settingData.detailImg){ // jd商品详情
  992. try {
  993. let data = await response.text();
  994. if(data.indexOf('showdesc(') > -1){
  995. data = data.slice(0, -1).replace('showdesc(', '');
  996. }
  997. let detailInfo = JSON.parse(data);
  998. let regex = /\/\/img[0-9]+.360buyimg.com\S+.(jpg|jpeg|png|gif|avif|tif|tiff)/g;
  999. let detailImgArr = [];
  1000. if(detailInfo && detailInfo.content){
  1001. detailImgArr = detailInfo.content.match(regex);
  1002. }
  1003. jdImgInfo.detailImg = detailImgArr;
  1004. for(let i = 0; i < detailImgArr.length; i++){
  1005. let imgUrl = 'https:' + detailImgArr[i];
  1006. let fileName = imgUrl.split('/').pop();
  1007. if(fileName){
  1008. let queryIndex = fileName.indexOf('?');
  1009. if (queryIndex !== -1) {
  1010. fileName = fileName.substr(0, queryIndex);
  1011. }
  1012. if (!fs.existsSync(urlInfo.newPath + '\\详情图')) {
  1013. fs.mkdirSync(urlInfo.newPath + '\\详情图');
  1014. }
  1015. let outputPath = urlInfo.newPath + '\\详情图\\' + fileName;
  1016. if(!authority && i < this.execNum){
  1017. await this.downloadImage(imgUrl, outputPath, urlInfo);
  1018. }
  1019. if(authority){
  1020. await this.downloadImage(imgUrl, outputPath, urlInfo);
  1021. }
  1022. }
  1023. }
  1024. } catch (error) {
  1025. console.error('详情图片解析失败:', error);
  1026. }
  1027. }
  1028. if(videoUrl && this.settingData.video){ // jd商品视频
  1029. try {
  1030. let data = await response.text();
  1031. data = data.slice(0, -1).replace(/^jQuery[0-9]+\(/, '');
  1032. let videoInfo = JSON.parse(data);
  1033. let videoUrl = videoInfo.playUrl;
  1034. jdImgInfo.video.push(videoUrl);
  1035. let fileName = videoUrl.split('/').pop();
  1036. if(fileName){
  1037. let queryIndex = fileName.indexOf('?');
  1038. if (queryIndex !== -1) {
  1039. fileName = fileName.substr(0, queryIndex);
  1040. }
  1041. if (!fs.existsSync(urlInfo.newPath + '\\视频')) {
  1042. fs.mkdirSync(urlInfo.newPath + '\\视频');
  1043. }
  1044. let outputPath = urlInfo.newPath + '\\视频\\' + fileName;
  1045. await this.downloadImage(videoUrl, outputPath, urlInfo);
  1046. }
  1047. } catch (error) {
  1048. console.error('视频解析失败:', error);
  1049. }
  1050. }
  1051. }
  1052. if (response.headers()['content-type'] && response.headers()['content-type'].startsWith('image/')) {
  1053. let regex = /^https:\/\/img[0-9]+.360buyimg.com\/n/;
  1054. if(regex.exec(response.url())){ // 匹配符合规则的图片路径
  1055. let skuReg = /\/n[0-9]+\/s(60|40)x(80|40)_jfs\//;
  1056. if(response.url().match(skuReg) && this.settingData.skuImg){ // sku图片
  1057. let replaceStr = '/n1/s800x800_jfs/';
  1058. if(response.url().match(/\/n[0-9]+\/s60x80_jfs\//)){
  1059. replaceStr = '/n1/s750x1000_jfs/';
  1060. }
  1061. let skuImgUrl = response.url().replace(skuReg, replaceStr).replace('.avif', '');
  1062. if(response.url().match(skuReg))
  1063. jdImgInfo.skuImg.push(skuImgUrl);
  1064. let fileName = skuImgUrl.split('/').pop();
  1065. if(fileName){
  1066. let queryIndex = fileName.indexOf('?');
  1067. if (queryIndex !== -1) {
  1068. fileName = fileName.substr(0, queryIndex);
  1069. }
  1070. if (!fs.existsSync(urlInfo.newPath + '\\sku图')) {
  1071. fs.mkdirSync(urlInfo.newPath + '\\sku图');
  1072. }
  1073. let outputPath = urlInfo.newPath + '\\sku图\\' + fileName;
  1074. skuNum ++;
  1075. if(!authority && skuNum <= this.execNum){
  1076. await this.downloadImage(skuImgUrl, outputPath, urlInfo);
  1077. }
  1078. if(authority){
  1079. await this.downloadImage(skuImgUrl, outputPath, urlInfo);
  1080. }
  1081. }
  1082. }
  1083. let reg = /\/n[0-9]+\/jfs\//;
  1084. let reg2 = /\/n[0-9]+\/s(54|50)x(54|66)_jfs\//;
  1085. if(response.url().match(reg) || response.url().match(reg2)){ // 主图
  1086. let replaceStr = '/n1/s800x800_jfs/';
  1087. if(response.url().match(/\/n[0-9]+\/s50x66_jfs\//)){
  1088. replaceStr = '/n1/s750x1000_jfs/';
  1089. }
  1090. let mainImgUrl = response.url().replace(reg, replaceStr).replace(reg2, replaceStr).replace('.avif', '');
  1091. jdImgInfo.mainImg.push(mainImgUrl);
  1092. let fileName = mainImgUrl.split('/').pop();
  1093. if(fileName){
  1094. let queryIndex = fileName.indexOf('?');
  1095. let queryIndex2 = fileName.indexOf('!');
  1096. if (queryIndex !== -1) {
  1097. fileName = fileName.substr(0, queryIndex);
  1098. }
  1099. if (queryIndex2 !== -1) {
  1100. fileName = fileName.substr(0, queryIndex2);
  1101. }
  1102. if (!fs.existsSync(urlInfo.newPath + '\\主图')) {
  1103. fs.mkdirSync(urlInfo.newPath + '\\主图');
  1104. }
  1105. let outputPath = urlInfo.newPath + '\\主图\\' + fileName;
  1106. mainNum ++;
  1107. if(!authority && mainNum <= this.execNum){
  1108. await this.downloadImage(mainImgUrl, outputPath, urlInfo);
  1109. }
  1110. if(authority){
  1111. await this.downloadImage(mainImgUrl, outputPath, urlInfo);
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. });
  1118. await page.goto(urlInfo.url, {waitUntil : 'networkidle2'});
  1119. await jdBrowser.close();
  1120. urlInfo.status = '4';
  1121. resolve(true);
  1122. }catch(e){
  1123. reject(e);
  1124. this.showError(e);
  1125. }
  1126. })();
  1127. });
  1128. },
  1129. // 检查天猫淘宝登录状态
  1130. checkLogin(){
  1131. this.checkLoading = true;
  1132. this.tbStatus = 1;
  1133. return new Promise((resolve, reject) => {
  1134. (async () => {
  1135. try{
  1136. const tbBrowser = await puppeteer.launch({
  1137. executablePath: puppeteer.executablePath().replace('win32-1', 'win64-1'),
  1138. userDataDir: os.tmpdir() + separator + 'chrome-data-capture',
  1139. });
  1140. const page = await tbBrowser.newPage();
  1141. let testUrl = 'https://www.taobao.com';
  1142. await page.goto(testUrl, {waitUntil : 'networkidle2'});
  1143. let loginInfo = await page.evaluate(() => {
  1144. let navTags = document.querySelector('.site-nav-sign a');
  1145. let userTags = document.querySelector('.site-nav-user a');
  1146. if(navTags && navTags.innerHTML.indexOf('登录') > -1){
  1147. return false;
  1148. }else if(userTags){
  1149. return true;
  1150. }else{
  1151. return false;
  1152. }
  1153. });
  1154. if(loginInfo){
  1155. this.tbStatus = 2;
  1156. }else{
  1157. this.tbStatus = 3;
  1158. }
  1159. resolve(this.tbStatus);
  1160. this.checkLoading = false;
  1161. tbBrowser.close();
  1162. }catch(e){
  1163. reject(3);
  1164. this.showError(e);
  1165. }
  1166. })();
  1167. });
  1168. },
  1169. // 淘宝天猫扫描下载图片
  1170. async tbScanImg(urlInfo){
  1171. let task = await new Promise((resolve,reject) =>{
  1172. (async () => {
  1173. try{
  1174. let authority = this.$refs.headerRef.authority.isAuthority;
  1175. urlInfo.status = '2';
  1176. urlInfo.num = 0;
  1177. const tbBrowser = await puppeteer.launch({
  1178. executablePath: puppeteer.executablePath().replace('win32-1', 'win64-1'),
  1179. userDataDir: os.tmpdir() + separator + 'chrome-data-capture',
  1180. });
  1181. const page = await tbBrowser.newPage();
  1182. await page.goto(urlInfo.url, {waitUntil : 'networkidle0'});
  1183. if(urlInfo.title){
  1184. if (fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title)) {
  1185. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title;
  1186. } else {
  1187. fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title);
  1188. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + urlInfo.title;
  1189. }
  1190. }else{
  1191. await this.getTitle(page, urlInfo); // 生成页面标题对应的文件夹
  1192. }
  1193. let responseVideo = [];
  1194. page.on('response', async(response) => {
  1195. // 检查响应的 MIME 类型是否以 'image/' 开头
  1196. if (response.headers()['content-type'] && response.headers()['content-type'].startsWith('video/')) {
  1197. responseVideo.push(response.url());
  1198. }
  1199. });
  1200. let pageInfo = await page.evaluate(() => {
  1201. let cHeight = document.documentElement.clientHeight;
  1202. let scrollHeight = document.body.scrollHeight;
  1203. return {'scrollHeight': scrollHeight, 'cHeight': cHeight}
  1204. });
  1205. //鼠标放在主图第一张,生成视频
  1206. const elementHandle = await page.$('li[class*=thumbnail--]');
  1207. if(elementHandle){
  1208. const classListProperty = await elementHandle.getProperty('classList');
  1209. const classList = await classListProperty.jsonValue();
  1210. const classesArray = Object.values(classList);
  1211. const allClasses = classesArray.join('.');
  1212. if(allClasses && allClasses.indexOf('active-') < 0){
  1213. await page.hover('li.'+allClasses);
  1214. }
  1215. }
  1216. let scrollHeight = pageInfo.scrollHeight;
  1217. let cHeight = pageInfo.cHeight;
  1218. let num = Math.ceil(scrollHeight / cHeight);
  1219. let start = -1;
  1220. let scrollInt = setInterval(async() => {
  1221. start ++;
  1222. await page.evaluate((start) => {
  1223. let cHeight = document.documentElement.clientHeight;
  1224. window.scrollTo({
  1225. top: cHeight * start,
  1226. behavior: "smooth"
  1227. });
  1228. }, start);
  1229. if(start > num || start > 200){ // 防止页面过长,滚动200次自动停止
  1230. urlInfo.status = '3';
  1231. clearInterval(scrollInt);
  1232. //detailImg:详情图;skuImg:sku图片;commentImg: 评论图;video: 视频
  1233. const imgInfo = await page.evaluate((authority, execNum) => {
  1234. let outObj = {
  1235. mainImg: [],
  1236. detailImg: [],
  1237. skuImg: [],
  1238. commentImg: [],
  1239. video: []
  1240. };
  1241. // 正则表达式匹配字符 重写图片路径
  1242. let regex = /\.(.{3,4})_[0-9a-zA-z]+\.(.{3,4})_\.(.{3,4})/;
  1243. //主图
  1244. let arr1 = document.querySelectorAll('img[class^=PicGallery--thumbnailPic--]');
  1245. if(arr1.length === 0){
  1246. arr1 = document.querySelectorAll('img[class*=thumbnailPic--]');
  1247. }
  1248. for(let i=0; i< arr1.length; i++){
  1249. let mainImgUrl = arr1[i].src;
  1250. let result = regex.exec(mainImgUrl);
  1251. if(result){
  1252. mainImgUrl = mainImgUrl.replace(result[0], '.'+result[1]);
  1253. }
  1254. if(!authority && i < execNum){
  1255. outObj.mainImg.push(mainImgUrl);
  1256. }
  1257. if(authority){
  1258. outObj.mainImg.push(mainImgUrl);
  1259. }
  1260. }
  1261. //sku图片
  1262. let arr2 = document.querySelectorAll('img[class^=SkuContent--valueItemImg--]');
  1263. if(arr2.length === 0){
  1264. arr2 = document.querySelectorAll('img[class*=valueItemImg--]');
  1265. }
  1266. for(let i=0; i< arr2.length; i++){
  1267. let skuImgUrl = arr2[i].src;
  1268. let result = regex.exec(skuImgUrl);
  1269. if(result){
  1270. skuImgUrl = skuImgUrl.replace(result[0], '.'+result[1]);
  1271. }
  1272. if(!authority && i < execNum){
  1273. outObj.skuImg.push(skuImgUrl);
  1274. }
  1275. if(authority){
  1276. outObj.skuImg.push(skuImgUrl);
  1277. }
  1278. }
  1279. //详情图片
  1280. let arr3 = document.querySelectorAll('#content img');
  1281. for(let i=0; i< arr3.length; i++){
  1282. let detailImgUrl = arr3[i].src;
  1283. let lazyUrl = arr3[i].getAttribute('data-src');
  1284. if(arr3[i].src.indexOf('/s.gif') > -1 && lazyUrl){
  1285. detailImgUrl = lazyUrl;
  1286. if(!/^http/.exec(lazyUrl)){
  1287. detailImgUrl = 'https:' + lazyUrl;
  1288. }
  1289. }
  1290. let result = regex.exec(detailImgUrl);
  1291. if(result){
  1292. detailImgUrl = detailImgUrl.replace(result[0], '.'+result[1]);
  1293. }
  1294. if(!authority && i < execNum){
  1295. outObj.detailImg.push(detailImgUrl);
  1296. }
  1297. if(authority){
  1298. outObj.detailImg.push(detailImgUrl);
  1299. }
  1300. }
  1301. //评论图片
  1302. let arr4 = document.querySelectorAll('div[class^=Comments--comments--] img');
  1303. if(arr4.length === 0){
  1304. arr4 = document.querySelectorAll('div[class^=comments--] img');
  1305. }
  1306. for(let i=0; i< arr4.length; i++){
  1307. if(arr4[i].src.indexOf('/avatar/sns/user/flag/sns_logo') == -1){ //过滤淘宝用户头像
  1308. if(!authority && i < execNum){
  1309. outObj.commentImg.push(arr4[i].src);
  1310. }
  1311. if(authority){
  1312. outObj.commentImg.push(arr4[i].src);
  1313. }
  1314. }
  1315. }
  1316. // 视频
  1317. let arr5 = document.querySelectorAll('video.lib-video');
  1318. for(let i=0; i< arr5.length; i++){
  1319. if(outObj.video.indexOf(arr5[i].src) == -1){
  1320. if(!authority && i < execNum){
  1321. outObj.video.push(arr5[i].src);
  1322. }
  1323. if(authority){
  1324. outObj.video.push(arr5[i].src);
  1325. }
  1326. }
  1327. }
  1328. return outObj;
  1329. }, authority, this.execNum);
  1330. if(imgInfo.video.length == 0 && responseVideo.length > 0){
  1331. imgInfo.video = responseVideo;
  1332. }
  1333. // 主图下载
  1334. for(let j = 0; j < imgInfo.mainImg.length; j++){
  1335. let fileName = imgInfo.mainImg[j].split('/').pop();
  1336. if(fileName){
  1337. let queryIndex = fileName.indexOf('?');
  1338. if (queryIndex !== -1) {
  1339. fileName = fileName.substr(0, queryIndex);
  1340. }
  1341. if (!fs.existsSync(urlInfo.newPath + '\\主图')) {
  1342. fs.mkdirSync(urlInfo.newPath + '\\主图');
  1343. }
  1344. let outputPath = urlInfo.newPath + '\\主图\\' + fileName;
  1345. await this.downloadImage(imgInfo.mainImg[j], outputPath, urlInfo);
  1346. }
  1347. }
  1348. // sku图片下载
  1349. if(this.settingData.skuImg){
  1350. for(let j = 0; j < imgInfo.skuImg.length; j++){
  1351. let fileName = imgInfo.skuImg[j].split('/').pop();
  1352. if(fileName){
  1353. let queryIndex = fileName.indexOf('?');
  1354. if (queryIndex !== -1) {
  1355. fileName = fileName.substr(0, queryIndex);
  1356. }
  1357. if (!fs.existsSync(urlInfo.newPath + '\\sku图')) {
  1358. fs.mkdirSync(urlInfo.newPath + '\\sku图');
  1359. }
  1360. let outputPath = urlInfo.newPath + '\\sku图\\' + fileName;
  1361. await this.downloadImage(imgInfo.skuImg[j], outputPath, urlInfo);
  1362. }
  1363. }
  1364. }
  1365. //详情图下载
  1366. if(this.settingData.detailImg){
  1367. for(let j = 0; j < imgInfo.detailImg.length; j++){
  1368. let fileName = imgInfo.detailImg[j].split('/').pop();
  1369. if(fileName){
  1370. let queryIndex = fileName.indexOf('?');
  1371. if (queryIndex !== -1) {
  1372. fileName = fileName.substr(0, queryIndex);
  1373. }
  1374. if (!fs.existsSync(urlInfo.newPath + '\\详情图')) {
  1375. fs.mkdirSync(urlInfo.newPath + '\\详情图');
  1376. }
  1377. let outputPath = urlInfo.newPath + '\\详情图\\' + fileName;
  1378. await this.downloadImage(imgInfo.detailImg[j], outputPath, urlInfo);
  1379. }
  1380. }
  1381. }
  1382. //评论图下载
  1383. if(this.settingData.commentImg){
  1384. for(let j = 0; j < imgInfo.commentImg.length; j++){
  1385. let fileName = imgInfo.commentImg[j].split('/').pop();
  1386. if(fileName){
  1387. let queryIndex = fileName.indexOf('?');
  1388. if (queryIndex !== -1) {
  1389. fileName = fileName.substr(0, queryIndex);
  1390. }
  1391. if (!fs.existsSync(urlInfo.newPath + '\\评论图')) {
  1392. fs.mkdirSync(urlInfo.newPath + '\\评论图');
  1393. }
  1394. let outputPath = urlInfo.newPath + '\\评论图\\' + fileName;
  1395. await this.downloadImage(imgInfo.commentImg[j], outputPath, urlInfo);
  1396. }
  1397. }
  1398. }
  1399. //视频下载
  1400. if(this.settingData.video){
  1401. for(let j = 0; j < imgInfo.video.length; j++){
  1402. let fileName = imgInfo.video[j].split('/').pop();
  1403. if(fileName){
  1404. let queryIndex = fileName.indexOf('?');
  1405. if (queryIndex !== -1) {
  1406. fileName = fileName.substr(0, queryIndex);
  1407. }
  1408. if (!fs.existsSync(urlInfo.newPath + '\\视频')) {
  1409. fs.mkdirSync(urlInfo.newPath + '\\视频');
  1410. }
  1411. let outputPath = urlInfo.newPath + '\\视频\\' + fileName;
  1412. await this.downloadImage(imgInfo.video[j], outputPath, urlInfo);
  1413. }
  1414. }
  1415. }
  1416. await tbBrowser.close();
  1417. urlInfo.status = '4';
  1418. resolve(true);
  1419. this.loading = false;
  1420. }
  1421. }, 300);
  1422. }catch(e){
  1423. reject(e);
  1424. this.showError(e);
  1425. }
  1426. })();
  1427. });
  1428. },
  1429. // 阿里巴巴 - 扫描并下载图片
  1430. async alibabaScanImg(browser, page, urlInfo, authority){
  1431. //detailImg:详情图;skuImg:sku图片;commentImg: 评论图;video: 视频
  1432. const imgInfo = await page.evaluate((authority, execNum) => {
  1433. let outObj = {
  1434. mainImg: [],
  1435. detailImg: [],
  1436. skuImg: [],
  1437. commentImg: [],
  1438. video: []
  1439. };
  1440. //主图
  1441. let arr1 = document.querySelectorAll('img.detail-gallery-img');
  1442. for(let i=0; i< arr1.length; i++){
  1443. if(!authority && i < execNum){
  1444. outObj.mainImg.push(arr1[i].src);
  1445. }
  1446. if(authority){
  1447. outObj.mainImg.push(arr1[i].src);
  1448. }
  1449. }
  1450. //sku图片
  1451. let arr2 = document.querySelectorAll('.prop-img');
  1452. if(arr2.length == 0){
  1453. arr2 = document.querySelectorAll('.sku-item-image');
  1454. }
  1455. for(let i=0; i< arr2.length; i++){
  1456. let src = window.getComputedStyle(arr2[i]).backgroundImage.replace(/url\(["']?(.+?)["']?\)/i, '$1');
  1457. if(!authority && i < execNum){
  1458. outObj.skuImg.push(src);
  1459. }
  1460. if(authority){
  1461. outObj.skuImg.push(src);
  1462. }
  1463. }
  1464. //详情图片
  1465. let arr3 = document.querySelectorAll('img.desc-img-loaded');
  1466. for(let i=0; i< arr3.length; i++){
  1467. if(!authority && i < execNum){
  1468. outObj.detailImg.push(arr3[i].src);
  1469. }
  1470. if(authority){
  1471. outObj.detailImg.push(arr3[i].src);
  1472. }
  1473. }
  1474. //评论图片 -- 需要点击切换
  1475. // let arr4 = document.querySelectorAll('img.image-box');
  1476. // for(let i=0; i< arr4.length; i++){
  1477. // if(!authority && i < execNum){
  1478. // outObj.commentImg.push(arr4[i].src);
  1479. // }
  1480. // if(authority){
  1481. // outObj.commentImg.push(arr4[i].src);
  1482. // }
  1483. // }
  1484. // 视频
  1485. let arr5 = document.querySelectorAll('video.lib-video');
  1486. for(let i=0; i< arr5.length; i++){
  1487. if(outObj.video.indexOf(arr5[i].src) == -1){
  1488. if(!authority && i < execNum){
  1489. outObj.video.push(arr5[i].src);
  1490. }
  1491. if(authority){
  1492. outObj.video.push(arr5[i].src);
  1493. }
  1494. }
  1495. }
  1496. return outObj;
  1497. }, authority, this.execNum);
  1498. // 主图下载
  1499. for(let j = 0; j < imgInfo.mainImg.length; j++){
  1500. let fileName = imgInfo.mainImg[j].split('/').pop();
  1501. if(fileName){
  1502. let queryIndex = fileName.indexOf('?');
  1503. if (queryIndex !== -1) {
  1504. fileName = fileName.substr(0, queryIndex);
  1505. }
  1506. if (!fs.existsSync(urlInfo.newPath + '\\主图')) {
  1507. fs.mkdirSync(urlInfo.newPath + '\\主图');
  1508. }
  1509. let outputPath = urlInfo.newPath + '\\主图\\' + fileName;
  1510. await this.downloadImage(imgInfo.mainImg[j], outputPath, urlInfo);
  1511. }
  1512. }
  1513. // sku图片下载
  1514. if(this.settingData.skuImg){
  1515. for(let j = 0; j < imgInfo.skuImg.length; j++){
  1516. let fileName = imgInfo.skuImg[j].split('/').pop();
  1517. if(fileName){
  1518. let queryIndex = fileName.indexOf('?');
  1519. if (queryIndex !== -1) {
  1520. fileName = fileName.substr(0, queryIndex);
  1521. }
  1522. if (!fs.existsSync(urlInfo.newPath + '\\sku图')) {
  1523. fs.mkdirSync(urlInfo.newPath + '\\sku图');
  1524. }
  1525. let outputPath = urlInfo.newPath + '\\sku图\\' + fileName;
  1526. await this.downloadImage(imgInfo.skuImg[j], outputPath, urlInfo);
  1527. }
  1528. }
  1529. }
  1530. //详情图下载
  1531. if(this.settingData.detailImg){
  1532. for(let j = 0; j < imgInfo.detailImg.length; j++){
  1533. let fileName = imgInfo.detailImg[j].split('/').pop();
  1534. if(fileName){
  1535. let queryIndex = fileName.indexOf('?');
  1536. if (queryIndex !== -1) {
  1537. fileName = fileName.substr(0, queryIndex);
  1538. }
  1539. if (!fs.existsSync(urlInfo.newPath + '\\详情图')) {
  1540. fs.mkdirSync(urlInfo.newPath + '\\详情图');
  1541. }
  1542. let outputPath = urlInfo.newPath + '\\详情图\\' + fileName;
  1543. await this.downloadImage(imgInfo.detailImg[j], outputPath, urlInfo);
  1544. }
  1545. }
  1546. }
  1547. //评论图下载
  1548. // if(this.settingData.commentImg){
  1549. // for(let j = 0; j < imgInfo.commentImg.length; j++){
  1550. // let fileName = imgInfo.commentImg[j].split('/').pop();
  1551. // if(fileName){
  1552. // let queryIndex = fileName.indexOf('?');
  1553. // if (queryIndex !== -1) {
  1554. // fileName = fileName.substr(0, queryIndex);
  1555. // }
  1556. // if (!fs.existsSync(urlInfo.newPath + '\\评论图')) {
  1557. // fs.mkdirSync(urlInfo.newPath + '\\评论图');
  1558. // }
  1559. // let outputPath = urlInfo.newPath + '\\评论图\\' + fileName;
  1560. // await this.downloadImage(imgInfo.commentImg[j], outputPath, urlInfo);
  1561. // }
  1562. // }
  1563. // }
  1564. //视频下载
  1565. if(this.settingData.video){
  1566. for(let j = 0; j < imgInfo.video.length; j++){
  1567. let fileName = imgInfo.video[j].split('/').pop();
  1568. if(fileName){
  1569. let queryIndex = fileName.indexOf('?');
  1570. if (queryIndex !== -1) {
  1571. fileName = fileName.substr(0, queryIndex);
  1572. }
  1573. if (!fs.existsSync(urlInfo.newPath + '\\视频')) {
  1574. fs.mkdirSync(urlInfo.newPath + '\\视频');
  1575. }
  1576. let outputPath = urlInfo.newPath + '\\视频\\' + fileName;
  1577. await this.downloadImage(imgInfo.video[j], outputPath, urlInfo);
  1578. }
  1579. }
  1580. }
  1581. await browser.close();
  1582. },
  1583. //
  1584. downloadExample(){
  1585. let url = 'https://www.xingyousoft.com/soft/XYCapture/example.xlsx';
  1586. if (!fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName)) {
  1587. fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName);
  1588. }
  1589. let path = this.downloadDir + separator + pjson.softInfo.softName + '\\链接模板下载.xlsx';
  1590. this.downloadImage(url, path, 'example');
  1591. },
  1592. // 下载网址链接的图片
  1593. async downloadImage(imageUrl, outputPath, urlInfo) {
  1594. let _this = this;
  1595. let received_bytes = 0;
  1596. let total_bytes = 0;
  1597. try {
  1598. let req = request({
  1599. method: 'GET', uri: imageUrl, strictSSL: false
  1600. });
  1601. let out = fs.createWriteStream(outputPath);
  1602. req.pipe(out);
  1603. return new Promise((resolve, reject) => {
  1604. req.on('response', (data) => {
  1605. total_bytes = parseInt(data.headers['content-length']);
  1606. const status = data.statusCode;
  1607. if (status < 200 || status >= 300) {
  1608. //reject(false);
  1609. this.$notify.error({
  1610. title: '网络图片访问异常!- 1',
  1611. message: imageUrl.slice(0,50)
  1612. });
  1613. }else if(isNaN(total_bytes)){
  1614. //reject(false);
  1615. this.$notify.error({
  1616. title: '网络图片访问异常!- 2',
  1617. message: imageUrl.slice(0,50)
  1618. });
  1619. }else{
  1620. // console.log('下载中...')
  1621. }
  1622. });
  1623. req.on('data', (chunk) => {
  1624. received_bytes += chunk.length;
  1625. });
  1626. req.on('end', ()=> {
  1627. if(urlInfo != 'example'){
  1628. urlInfo.num += 1;
  1629. }else{
  1630. this.$msgbox({
  1631. title: '消息',
  1632. message: '模板下载成功,保存位置:' + this.downloadDir + separator + pjson.softInfo.softName,
  1633. showCancelButton: true,
  1634. confirmButtonText: '去查看',
  1635. cancelButtonText: '取消',
  1636. }).then(action => {
  1637. this.openFolder();
  1638. }).catch(action => {
  1639. });
  1640. }
  1641. //console.log('下载完成', outputPath)
  1642. resolve(true);
  1643. });
  1644. });
  1645. } catch (error) {
  1646. console.error(imageUrl, `Failed to download image: ${error.message}`);
  1647. throw error;
  1648. }
  1649. },
  1650. // 获取页面标题 - 生成对应的文件夹
  1651. async getTitle(page, urlInfo){
  1652. // 已页面标题作为新建文件夹,保留前20个字
  1653. let title = await page.title();
  1654. if(title){
  1655. title = title.substring(0, 20);
  1656. if(this.containsAnyChar(title, ['\\', '/', ':', '*', '?', '"', '<', '>', '|'])){ //判断是否含有特殊字符
  1657. title = title.replace(/[\\|/|:|*|?|"|<|>||]/g, "");
  1658. }
  1659. if (fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName + separator + title)) {
  1660. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + title;
  1661. } else {
  1662. fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName + separator + title);
  1663. urlInfo.newPath = this.downloadDir + separator + pjson.softInfo.softName + separator + title;
  1664. }
  1665. }
  1666. },
  1667. // 下载base64位的图片
  1668. async downloadBaseImage(base64String, outputPath, urlInfo) {
  1669. const buffer = Buffer.from(base64String, 'base64');
  1670. const writeStream = fs.createWriteStream(outputPath);
  1671. writeStream.write(buffer);
  1672. writeStream.end();
  1673. writeStream.on('finish', () => {
  1674. urlInfo.num += 1;
  1675. });
  1676. // 监听错误事件
  1677. writeStream.on('error', (err) => {
  1678. console.error('base64位写入文件时出错:', err);
  1679. });
  1680. },
  1681. // 错误提示
  1682. showError(e){
  1683. let str = '';
  1684. if(e.toString().indexOf('ERR_NAME_NOT_RESOLVE') > -1){
  1685. str = '无法解析该网址,请查看网址是否正确!-1';
  1686. }else if(e.toString().indexOf('Cannot navigate to invalid URL') > -1){
  1687. str = '无效的网址,请查看网址格式是否正确!-2';
  1688. }else if(e.toString().indexOf('ERR_CONNECTION_TIMED_OUT') > -1){
  1689. str = '链接请求超时,请查看网络状态!-3';
  1690. }else if(e.toString().indexOf('TimeoutError') > -1){
  1691. str = '链接请求超时,请查看网络状态!-4';
  1692. }else if(e.toString().indexOf('operation not permitted') > -1){
  1693. str = '权限受限,请以管理员权限运行软件!-5';
  1694. }else if(e.toString().indexOf('browser has disconnected') > -1){
  1695. str = '浏览器断开链接!-6';
  1696. }else{
  1697. str = e.toString();
  1698. //console.log(e);
  1699. }
  1700. this.loading = false;
  1701. this.$notify.error({
  1702. title: '提示',
  1703. message: str
  1704. });
  1705. },
  1706. // 是否包含特殊字符
  1707. containsAnyChar(str, charsArray) {
  1708. for (let i = 0; i < charsArray.length; i++) {
  1709. if (str.includes(charsArray[i])) {
  1710. return true;
  1711. }
  1712. }
  1713. return false;
  1714. },
  1715. // 随机生成字符
  1716. randomString(length) {
  1717. let result = '';
  1718. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  1719. const charactersLength = characters.length;
  1720. for (let i = 0; i < length; i++ ) {
  1721. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  1722. }
  1723. return result;
  1724. }
  1725. }
  1726. };
  1727. </script>
  1728. <style lang="scss">
  1729. @import "../assets/css/fontx/iconfont.css";
  1730. @import "../assets/css/home.scss";
  1731. .ivu-input-number-controls-outside-btn i {
  1732. font-weight: 800;
  1733. }
  1734. .update-point {
  1735. display: inline-block;
  1736. width: 8px;
  1737. height: 8px;
  1738. border-radius: 8px;
  1739. background: #ff0000;
  1740. top: 14px;
  1741. position: absolute;
  1742. left: -13px;
  1743. }
  1744. .menu-item {
  1745. padding: 8px 0;
  1746. font-size: 14px;
  1747. .iconfont {
  1748. font-size: 32px;
  1749. }
  1750. &:hover,
  1751. &.active {
  1752. color: #ed4014;
  1753. }
  1754. }
  1755. .ivu-progress-show-info .ivu-progress-outer {
  1756. padding-right: 40px !important;
  1757. margin-right: -40px !important;
  1758. }
  1759. .tips {
  1760. text-align: center;
  1761. padding: 10px 0;
  1762. color: #ed4014;
  1763. font-size: 12px;
  1764. }
  1765. .handle-desc {
  1766. display: inline-block;
  1767. width: calc(100% - 100px);
  1768. overflow: hidden;
  1769. }
  1770. .ivu-menu-submenu-title {
  1771. font-weight: 600;
  1772. }
  1773. .ivu-menu .ivu-menu-item {
  1774. line-height: 1;
  1775. }
  1776. // new-el
  1777. .el-menu {
  1778. border-right: none !important;
  1779. }
  1780. .cmenu-item {
  1781. padding: 0 20px 20px;
  1782. margin-bottom: 15px;
  1783. .cmenu-title {
  1784. font-size: 18px;
  1785. font-weight: 600;
  1786. padding-bottom: 20px;
  1787. }
  1788. .citem-nav {
  1789. text-align: center;
  1790. border-radius: 10px;
  1791. min-height: 110px;
  1792. padding: 15px 0;
  1793. background-color: #fff;
  1794. font-size: 15px;
  1795. cursor: pointer;
  1796. &.bg-linear1 {
  1797. color: #fff;
  1798. font-size: 20px;
  1799. background: linear-gradient(to right bottom, #2A56CA, #5795F4);
  1800. }
  1801. &.bg-linear2 {
  1802. color: #fff;
  1803. font-size: 20px;
  1804. background: linear-gradient(to right top, #147FBB, #5EB3E3);
  1805. }
  1806. &.bg-linear3 {
  1807. color: #fff;
  1808. font-size: 20px;
  1809. background: linear-gradient(to right bottom, #2F9E8A, #56CDB1);
  1810. }
  1811. &:hover {
  1812. margin-top: -5px;
  1813. box-shadow: 3px 3px 6px #ccc, -3px -3px 6px #ccc;
  1814. }
  1815. .citem-img {
  1816. width: 50px;
  1817. margin-bottom: 10px;
  1818. }
  1819. }
  1820. }
  1821. .popper-open{
  1822. text-align: center !important;
  1823. padding: 10px !important;
  1824. background: #303133 !important;
  1825. color: #fff !important;
  1826. min-width: 120px !important;
  1827. opacity: 0.8;
  1828. }
  1829. .popper-open[x-placement^=bottom] .popper__arrow::after{
  1830. border-bottom-color: #303133 !important;
  1831. }
  1832. textarea.el-textarea__inner{
  1833. height: 100% !important;
  1834. font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif;
  1835. }
  1836. .set-item{
  1837. margin-right: 15px;
  1838. .set-title{
  1839. font-size: 13px;
  1840. }
  1841. }
  1842. .outarea{
  1843. height: 100%;
  1844. border: 1px solid #DCDFE6;
  1845. background-color: #4851a415;
  1846. padding: 15px;
  1847. font-size: 20px;
  1848. overflow: hidden auto;
  1849. }
  1850. .outtext{
  1851. height: 100%;
  1852. font-size: 20px;
  1853. overflow: hidden auto;
  1854. textarea{
  1855. background-color: #4851a415;
  1856. }
  1857. }
  1858. .pin-tips{
  1859. font-size: 14px;
  1860. position: absolute;
  1861. bottom: 10px;
  1862. color: #ff0000;
  1863. left: 0;
  1864. right: 0;
  1865. margin: auto;
  1866. text-align: center;
  1867. }
  1868. .outarea.red-border{
  1869. border: 1px solid #F56C6C;
  1870. }
  1871. .outtext.red-border textarea{
  1872. border: 1px solid #F56C6C;
  1873. }
  1874. h3{
  1875. margin: 0;
  1876. }
  1877. .m-image{
  1878. width: 20px;
  1879. margin-right: 5px;
  1880. }
  1881. .dialog-footer-center{
  1882. text-align: center;
  1883. }
  1884. </style>