memos.html 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta
  6. name="viewport"
  7. content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"
  8. />
  9. <title>memos记录</title>
  10. <script src="js/util.js"></script>
  11. <script src="js/login.js"></script>
  12. <link rel="stylesheet" href="memos.css"/>
  13. <script src="js/vue.min.js"></script>
  14. <script src="elementUI/index.js"></script>
  15. <link rel="stylesheet" href="elementUI/index.css"/>
  16. <link rel="stylesheet" href="css/vditor.css"/>
  17. <script src="js/vditor.js"></script>
  18. </head>
  19. <body>
  20. <div id="app">
  21. <div id="all" class="main">
  22. <div class="myTop">
  23. <div class="main">
  24. <el-link href="https://memos.tianyunperfect.cn/">memos主页</el-link>
  25. <el-button @click="page = page - 1;search()" :disabled="page === 1">上一页</el-button>
  26. <el-input
  27. type="number"
  28. style="width: 80px"
  29. v-model="page"
  30. @change="search"
  31. ></el-input>
  32. <el-button @click="page = page + 1;search()">下一页</el-button>
  33. <el-select
  34. v-model="size"
  35. placeholder="请选择"
  36. style="width: 80px"
  37. @change="search"
  38. >
  39. <el-option
  40. v-for="item in [10, 20, 50, 100]"
  41. :key="item"
  42. :label="item"
  43. :value="item"
  44. >
  45. </el-option>
  46. </el-select>
  47. <!--输入框,绑定回车事件,触发查询-->
  48. <el-input
  49. style="margin-left: 30px; width: 20%; padding-left: 10px"
  50. placeholder="关键字"
  51. v-model="searchStr"
  52. @change="searchStrChange"
  53. ></el-input>
  54. <el-input
  55. style="margin-left: 30px; width: 30%; padding-left: 10px"
  56. placeholder="AI 问答"
  57. v-model="searchStr2"
  58. @change="searchStrChange2"
  59. ></el-input>
  60. <br/><br/>
  61. <div></div>
  62. <div style="display: flex; align-items: stretch; width: 90%">
  63. <!-- 创建一个用于编辑的容器 -->
  64. <el-input
  65. @paste.native="handlePaste"
  66. @keydown.enter.native="keyDown"
  67. type="textarea"
  68. :autosize="{minRows:2}"
  69. placeholder="记录内容"
  70. @input="localStorage.setItem('contentStr', contentStr)"
  71. v-model="contentStr"
  72. >
  73. </el-input>
  74. <!-- 添加发送按钮 -->
  75. <el-button
  76. style="margin-left: 20px; width: 10%; font-size: 2rem"
  77. @click="sendData"
  78. :disabled="!sendBtnAble"
  79. >记录
  80. </el-button
  81. >
  82. <el-button
  83. style="margin-left: 20px; width: 10%; font-size: 1rem"
  84. @click="mergeByIds"
  85. :disabled="selectedIds.length<=1"
  86. >合并
  87. </el-button
  88. >
  89. <div
  90. @click="dialogVisible2=true"
  91. style="font-size: 0.3em; margin-left: 10px;margin-top: 10px"
  92. >
  93. 替换
  94. </div>
  95. </div>
  96. <div style="display: flex">
  97. <div v-for="item in tmpFileIds">
  98. <el-image
  99. v-if="item['type'].startsWith('image')"
  100. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  101. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  102. class="img"
  103. ></el-image>
  104. <audio controls v-if="item['type'].startsWith('audio')">
  105. <source
  106. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  107. type="audio/ogg"
  108. />
  109. <source
  110. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  111. type="audio/mpeg"
  112. />
  113. 您的浏览器不支持 audio 元素。
  114. </audio>
  115. <video
  116. width="320"
  117. height="240"
  118. controls
  119. v-if="item['type'].startsWith('video')"
  120. >
  121. <source
  122. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  123. type="video/mp4"
  124. />
  125. <source
  126. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  127. type="video/ogg"
  128. />
  129. 您的浏览器不支持 HTML5 video 元素。
  130. </video>
  131. <el-link
  132. v-if="item['type'].startsWith('application')"
  133. :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  134. target="_blank"
  135. >{{item['filename']}}
  136. </el-link
  137. >
  138. <div @click="removeTmp(item)">删除</div>
  139. </div>
  140. </div>
  141. <!-- 添加多选按钮 -->
  142. <div style="display: flex">
  143. <div style="max-width: 55vw; height: 2.5em; display: flex;align-items: center">
  144. <el-checkbox-group v-model="tagChecked" @change="checkChange1()">
  145. <el-checkbox-button
  146. v-for="tag in allTags"
  147. :label="tag"
  148. ></el-checkbox-button>
  149. </el-checkbox-group>
  150. </div>
  151. <div style="margin-left: 20px; display: flex; align-items: center">
  152. <el-switch
  153. @change="duoxuanChange"
  154. v-model="duoxuan"
  155. active-text="多选"
  156. inactive-text="单选"
  157. >
  158. </el-switch>
  159. </div>
  160. </div>
  161. <div id="tags"></div>
  162. </div>
  163. </div>
  164. <div style="margin-top: 180px"></div>
  165. <div v-if="searchStr2" style="margin: 10px; padding: 10px">
  166. <el-input
  167. v-model="searchResStr"
  168. type="textarea"
  169. :autosize="{minRows: 1}"
  170. readonly="true"
  171. ></el-input>
  172. </div>
  173. <div>
  174. <div v-for="item in contentList" class="myLine">
  175. <el-row style="margin: 10px; padding: 10px" class="border">
  176. <el-col :span="18" class="contentLine">
  177. <div style="display: flex">
  178. <!-- 日期字符串-->
  179. <div style="font-family: ui-sans-serif, system-ui; display: flex;align-items: center;color: grey;font-size: 0.9em">
  180. {{ item.created_ts.replace('T', ' ')}}
  181. </div>
  182. <div style="margin-left: 20px">
  183. <el-checkbox-button
  184. v-for="tag in commonTags"
  185. :label="tag"
  186. :key="item"
  187. @change="checked=>checkChange(checked,item,tag)"
  188. :checked="isChooseTag(item,tag)"
  189. >{{tag}}
  190. </el-checkbox-button>
  191. </div>
  192. </div>
  193. <div class="myText" @dblclick="edit(item)">
  194. <!-- <el-input v-model="item.content" type="textarea" :autosize="{minRows: 1}" readonly="true"></el-input>-->
  195. <div :id="item.resource_name"></div>
  196. </div>
  197. <div style="display: flex">
  198. <div v-for="item in resourceMap[item.id]">
  199. <el-image
  200. v-if="item['type'].startsWith('image')"
  201. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  202. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  203. fit="cover"
  204. class="img"
  205. >
  206. </el-image>
  207. <audio controls v-if="item['type'].startsWith('audio')">
  208. <source
  209. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  210. type="audio/ogg"
  211. />
  212. <source
  213. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  214. type="audio/mpeg"
  215. />
  216. 您的浏览器不支持 audio 元素。
  217. </audio>
  218. <video
  219. width="320"
  220. height="240"
  221. controls
  222. v-if="item['type'].startsWith('video')"
  223. >
  224. <source
  225. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  226. type="video/mp4"
  227. />
  228. <source
  229. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  230. type="video/ogg"
  231. />
  232. 您的浏览器不支持 HTML5 video 元素。
  233. </video>
  234. <el-link
  235. v-if="item['type'].startsWith('application')"
  236. :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  237. target="_blank"
  238. >{{item['filename']}}
  239. </el-link
  240. >
  241. </div>
  242. </div>
  243. </el-col>
  244. <el-col :span="5" style="display: flex; justify-content: flex-end">
  245. <div style="display: flex; flex-direction: column; gap: 8px">
  246. <el-button
  247. size="mini"
  248. type="info"
  249. icon="el-icon-link"
  250. @click="window.open('https://memos.tianyunperfect.cn/m/' + item.resource_name)"
  251. style="width: 80px"
  252. >跳转
  253. </el-button>
  254. <el-button
  255. size="mini"
  256. type="info"
  257. icon="el-icon-folder-opened"
  258. @click="archive(item)"
  259. style="width: 80px; margin-left: 0"
  260. >归档
  261. </el-button>
  262. <el-button
  263. size="mini"
  264. :type="item.pendingDelete ? 'success' : 'danger'"
  265. :icon="item.pendingDelete ? 'el-icon-refresh' : 'el-icon-delete'"
  266. @click="del(item)"
  267. style="width: 80px; margin-left: 0"
  268. >
  269. {{ item.pendingDelete ? `(${item.countdown}s)` : '删除' }}
  270. </el-button>
  271. </div>
  272. <el-checkbox
  273. :value="selectedIds.includes(item.id)"
  274. @change="handleCheckboxChange(item.id, $event)"
  275. style="margin-top: 6px; margin-left: 20px"
  276. >合并
  277. </el-checkbox>
  278. </el-col>
  279. </el-row>
  280. </div>
  281. </div>
  282. </div>
  283. <el-dialog title="提示" :visible.sync="dialogVisible" width="60%">
  284. <div v-if="tmpItem.id" class="contentLine">
  285. <div>
  286. <el-date-picker
  287. v-model="tmpItem.created_ts"
  288. type="datetime"
  289. value-format="yyyy-MM-ddTHH:mm:ss"
  290. placeholder="选择日期时间"
  291. >
  292. </el-date-picker>
  293. <!-- el-input 只处理粘贴事件 -->
  294. <el-input
  295. @paste.native="handlePaste2"
  296. placeholder="粘贴图片"
  297. style="width: 50%"
  298. >
  299. </el-input>
  300. </div>
  301. <div>
  302. <div id="vditor"></div>
  303. <!-- <el-input
  304. ref="contentEdit"
  305. v-model="tmpItem.content"
  306. type="textarea"
  307. :autosize="{minRows: 2}"
  308. ></el-input> -->
  309. </div>
  310. <div style="display: flex">
  311. <div v-for="item in cloneFileIds">
  312. <el-image
  313. v-if="item['type'].startsWith('image')"
  314. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  315. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  316. class="img"
  317. >
  318. </el-image>
  319. <audio controls v-if="item['type'].startsWith('audio')">
  320. <source
  321. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  322. type="audio/ogg"
  323. />
  324. <source
  325. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  326. type="audio/mpeg"
  327. />
  328. 您的浏览器不支持 audio 元素。
  329. </audio>
  330. <video
  331. width="320"
  332. height="240"
  333. controls
  334. v-if="item['type'].startsWith('video')"
  335. >
  336. <source
  337. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  338. type="video/mp4"
  339. />
  340. <source
  341. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  342. type="video/ogg"
  343. />
  344. 您的浏览器不支持 HTML5 video 元素。
  345. </video>
  346. <el-link
  347. v-if="item['type'].startsWith('application')"
  348. :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  349. target="_blank"
  350. >{{item['filename']}}
  351. </el-link
  352. >
  353. <div @click="delPic(item)">删除</div>
  354. </div>
  355. </div>
  356. </div>
  357. <span slot="footer" class="dialog-footer">
  358. <el-button @click="dialogVisible = false">取 消</el-button>
  359. <el-button type="primary" @click="confirmSubmit">确 定</el-button>
  360. </span>
  361. </el-dialog>
  362. <el-dialog
  363. title="替换(危险操作,不可撤销)"
  364. :visible.sync="dialogVisible2"
  365. width="60%"
  366. >
  367. <div class="contentLine">
  368. <div>
  369. <el-input v-model="old_text" placeholder="old"></el-input>
  370. <el-input v-model="new_text" placeholder="new"></el-input>
  371. </div>
  372. </div>
  373. <span slot="footer" class="dialog-footer">
  374. <el-button @click="dialogVisible2 = false">取 消</el-button>
  375. <el-button type="primary" @click="confirmSubmit2">确 定</el-button>
  376. </span>
  377. </el-dialog>
  378. </div>
  379. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  380. <script>
  381. let myHeaders = {
  382. "Content-type": "application/json",
  383. Authorization: "",
  384. };
  385. function getRandomId() {
  386. return Math.random().toString(36).substr(2);
  387. }
  388. let vm = new Vue({
  389. el: "#app",
  390. data: {
  391. contentEditor: "", // vditor实例
  392. dialogVisible2: false,
  393. old_text: "",
  394. new_text: "",
  395. searchStr2: "",
  396. searchResStr: "",
  397. selectedIds: [],
  398. duoxuan: false,
  399. tmpItem: {},
  400. dialogVisible: false,
  401. page: 1,
  402. size: 20,
  403. searchStr: "",
  404. contentStr: "",
  405. tagChecked: [],
  406. allTags: [],
  407. contentList: [],
  408. sendBtnAble: true,
  409. resourceMap: {}, // 资源map,
  410. commonTags: ["todo"],
  411. upload_url: "https://memos.tianyunperfect.cn/api/v1/resource/blob",
  412. tmpFileIds: [],
  413. cloneFileIds: [],
  414. },
  415. watch: {
  416. page: function () {
  417. this.search();
  418. },
  419. size: function () {
  420. this.search();
  421. },
  422. contentList: function () {
  423. console.log(1);
  424. this.$nextTick(() => {
  425. console.log(2);
  426. this.contentList.forEach((item) => {
  427. this.resetView(item);
  428. });
  429. });
  430. },
  431. },
  432. mounted() {
  433. if (!checkLogin()) {
  434. // 警告并刷新页面
  435. alert("登录失败,请先登录");
  436. location.reload();
  437. }
  438. myHeaders.Authorization = "bearer " + getCookie("token");
  439. if (this.getQueryStr()) {
  440. this.searchStr = this.getQueryStr();
  441. }
  442. this.duoxuan = this.getDuoXuan();
  443. // 初始化contentStr,如果本地有缓存
  444. let item = localStorage.getItem("contentStr");
  445. if (item) {
  446. this.contentStr = item;
  447. }
  448. let queryStr = this.getQueryStr();
  449. if (queryStr) {
  450. // queryStr 解码
  451. queryStr = decodeURIComponent(queryStr);
  452. this.searchStr = queryStr;
  453. }
  454. // 解码
  455. let tagsStr = decodeURIComponent(getQueryString("tags"));
  456. if (tagsStr) {
  457. this.tagChecked = tagsStr.split(",");
  458. }
  459. // this.getAllTags();
  460. this.search();
  461. requestUtil
  462. .async(
  463. "https://web_history.tianyunperfect.cn/memos/getCustomMemoTags",
  464. "get"
  465. )
  466. .then((res) => {
  467. this.commonTags = JSON.parse(res["res"][0]["content"]);
  468. this.allTags = this.commonTags;
  469. });
  470. },
  471. methods: {
  472. resetView(item) {
  473. Vditor.preview(
  474. document.getElementById(item.resource_name),
  475. item.content,
  476. {
  477. cdn: "https://ld246.com/js/lib/vditor",
  478. speech: {
  479. enable: false,
  480. },
  481. anchor: 1,
  482. after() {
  483. },
  484. }
  485. );
  486. },
  487. mergeByIds() {
  488. if (this.selectedIds.length < 2) {
  489. return;
  490. }
  491. requestUtil.sync(
  492. "https://web_history.tianyunperfect.cn/memos/mergeMemo",
  493. "post",
  494. {ids: this.selectedIds},
  495. {}
  496. );
  497. location.reload();
  498. },
  499. handleCheckboxChange(id, checked) {
  500. if (checked) {
  501. this.selectedIds.push(id);
  502. } else {
  503. this.selectedIds = this.selectedIds.filter(
  504. (selectedId) => selectedId !== id
  505. );
  506. }
  507. console.log(this.selectedIds);
  508. },
  509. searchStrChange() {
  510. let queryStr = this.searchStr;
  511. // url编码
  512. queryStr = encodeURIComponent(queryStr);
  513. location.href = requestUtil.buildUrl(location.href, {
  514. queryStr: queryStr,
  515. });
  516. },
  517. searchStrChange2() {
  518. this.searchResStr = "";
  519. requestUtil
  520. .async(
  521. "https://wx.tianyunperfect.cn/full_search?",
  522. "post",
  523. {search: this.searchStr2},
  524. {}
  525. )
  526. .then((res) => {
  527. if (res["list"]) {
  528. let resList = res["list"];
  529. this.searchResStr = res["res"];
  530. this.contentList = resList;
  531. this.searchByContent();
  532. }
  533. });
  534. },
  535. duoxuanChange() {
  536. if (this.duoxuan) {
  537. this.setDuoXuan("ok");
  538. } else {
  539. this.setDuoXuan("");
  540. }
  541. },
  542. checkChange1(newVal) {
  543. console.log(this.tagChecked);
  544. if (!this.getDuoXuan()) {
  545. if (this.tagChecked.length > 1) {
  546. // 保留最后一个
  547. this.tagChecked = [this.tagChecked[this.tagChecked.length - 1]];
  548. }
  549. }
  550. location.href = requestUtil.buildUrl(location.href, {
  551. tags: this.tagChecked.join(","),
  552. });
  553. },
  554. getQueryStr() {
  555. return getQueryString("queryStr");
  556. },
  557. getDuoXuan() {
  558. return getQueryString("duoxuan") == "ok";
  559. },
  560. setDuoXuan(ok) {
  561. location.href = requestUtil.buildUrl(location.href, {
  562. duoxuan: ok,
  563. });
  564. },
  565. removeTmp(item) {
  566. // tmpFileIds 根据id删除
  567. let index = this.tmpFileIds.findIndex(
  568. (item) => item.id === item.id
  569. );
  570. if (index !== -1) {
  571. this.tmpFileIds.splice(index, 1);
  572. }
  573. },
  574. delPic(img) {
  575. // 根据id从 cloneFileIds 删除
  576. let index = this.cloneFileIds.findIndex(
  577. (item) => item.id === img.id
  578. );
  579. if (index !== -1) {
  580. this.cloneFileIds.splice(index, 1);
  581. }
  582. },
  583. getCloneResourceIds(id) {
  584. let resourceMapElement = this.resourceMap[id];
  585. if (resourceMapElement) {
  586. return JSON.parse(JSON.stringify(resourceMapElement));
  587. }
  588. return [];
  589. },
  590. uploadFile(url, file, headers = {}) {
  591. return new Promise((resolve, reject) => {
  592. const xhr = new XMLHttpRequest();
  593. xhr.open("POST", url, true); // 使用异步的方式
  594. const formData = new FormData();
  595. formData.append("file", file);
  596. for (const [key, value] of Object.entries(headers)) {
  597. xhr.setRequestHeader(key, value);
  598. }
  599. xhr.onload = function () {
  600. if (xhr.status === 200) {
  601. resolve(JSON.parse(xhr.responseText));
  602. } else {
  603. reject(new Error(xhr.statusText));
  604. }
  605. };
  606. xhr.onerror = function () {
  607. reject(new Error("Network Error"));
  608. };
  609. xhr.send(formData);
  610. });
  611. },
  612. getFileFromPaste(e) {
  613. let clipboardData = e.clipboardData; // IE
  614. if (!clipboardData) {
  615. //chrome
  616. clipboardData = e.originalEvent.clipboardData;
  617. }
  618. let items = "";
  619. items = (e.clipboardData || window.clipboardData).items;
  620. let file = null;
  621. if (!items || items.length === 0) {
  622. this.$message.error(
  623. "当前浏览器不支持粘贴本地图片,请打开图片复制后再粘贴!"
  624. );
  625. return;
  626. }
  627. // 搜索剪切板items
  628. for (let i = 0; i < items.length; i++) {
  629. // 限制上传文件类型
  630. if (items[i].type.indexOf("image") !== -1) {
  631. file = items[i].getAsFile();
  632. break;
  633. }
  634. }
  635. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  636. if (file) {
  637. return file;
  638. }
  639. },
  640. handlePaste(e) {
  641. let file = this.getFileFromPaste(e);
  642. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  643. if (file) {
  644. this.uploadFile(this.upload_url, file, {
  645. Authorization: myHeaders["Authorization"],
  646. }).then((res) => {
  647. let fileId = res["id"];
  648. if (fileId) {
  649. let sourceRes = requestUtil.sync(
  650. "https://web_history.tianyunperfect.cn/memos/resourceById",
  651. "post",
  652. {ids: fileId},
  653. {}
  654. );
  655. let sourceRe = sourceRes["res"];
  656. this.tmpFileIds.push(...sourceRe);
  657. }
  658. });
  659. }
  660. },
  661. handlePaste2(e) {
  662. let file = this.getFileFromPaste(e);
  663. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  664. if (file) {
  665. this.uploadFile(this.upload_url, file, {
  666. Authorization: myHeaders["Authorization"],
  667. }).then((res) => {
  668. let fileId = res["id"];
  669. if (fileId) {
  670. let sourceRes = requestUtil.sync(
  671. "https://web_history.tianyunperfect.cn/memos/resourceById",
  672. "post",
  673. {ids: fileId},
  674. {}
  675. );
  676. let sourceRe = sourceRes["res"];
  677. this.cloneFileIds.push(...sourceRe);
  678. }
  679. });
  680. }
  681. },
  682. keyDown() {
  683. // 如果是ctrl + enter \ meta + enter 则发送
  684. if (event.ctrlKey || event.metaKey) {
  685. this.sendData();
  686. }
  687. },
  688. msg(content) {
  689. this.$message({
  690. message: content,
  691. type: "success",
  692. });
  693. },
  694. isChooseTag(item, tag) {
  695. return item.content.indexOf("#" + tag + " ") !== -1;
  696. },
  697. checkChange(val, item, tag) {
  698. if (val) {
  699. this.addTag(item, tag);
  700. } else {
  701. this.delTag(item, tag);
  702. }
  703. this.resetView(item);
  704. },
  705. addTag(item, cTag) {
  706. let content = item.content;
  707. // 如果已经有了,就不添加
  708. let tmpTag = "#" + cTag + " ";
  709. if (content.indexOf(tmpTag) !== -1) {
  710. return;
  711. }
  712. item.content = tmpTag + content;
  713. // 提交记录
  714. let res = requestUtil.sync(
  715. "https://web_history.tianyunperfect.cn/memos/updateContent",
  716. "post",
  717. {
  718. id: item.id,
  719. content: item.content,
  720. created_ts: item.created_ts,
  721. },
  722. {}
  723. );
  724. if (res.code === 200) {
  725. this.msg("更新成功");
  726. // this.search();
  727. }
  728. },
  729. delTag(item, cTag) {
  730. let content = item.content;
  731. // 如果没有,就不删除
  732. if (content.indexOf(cTag) === -1) {
  733. return;
  734. }
  735. item.content = content.replaceAll("#" + cTag + " ", "");
  736. // 最少有一个标签
  737. if (!item.content.includes("#")) {
  738. item.content = "#todo " + item.content;
  739. }
  740. // 提交记录
  741. let res = requestUtil.sync(
  742. "https://web_history.tianyunperfect.cn/memos/updateContent",
  743. "post",
  744. {
  745. id: item.id,
  746. content: item.content,
  747. created_ts: item.created_ts,
  748. },
  749. {}
  750. );
  751. if (res.code === 200) {
  752. this.msg("更新成功");
  753. // this.search();
  754. }
  755. },
  756. // 修正后的删除逻辑,总计时3秒支持撤销
  757. del(item) {
  758. // 如果正在等待删除,则取消删除
  759. if (item.pendingDelete) {
  760. clearInterval(item.countdownInterval);
  761. this.$set(item, "pendingDelete", false);
  762. this.$set(item, "countdown", 0);
  763. this.$notify({
  764. type: "info",
  765. title: "已取消",
  766. message: "已取消删除操作",
  767. duration: 1500,
  768. });
  769. return;
  770. }
  771. // 开始删除倒计时
  772. this.$set(item, "pendingDelete", true);
  773. this.$set(item, "countdown", 5);
  774. // 设置倒计时通知
  775. item.countdownInterval = setInterval(() => {
  776. this.$set(item, "countdown", item.countdown - 1);
  777. if (item.countdown <= 0) {
  778. clearInterval(item.countdownInterval);
  779. // 执行删除请求
  780. requestUtil
  781. .async(
  782. `https://web_history.tianyunperfect.cn/memos/delete`,
  783. "post",
  784. {id: item.id},
  785. {}
  786. )
  787. .then((res) => {
  788. if (res.code === 200) {
  789. this.msg("删除成功");
  790. this.contentList = this.contentList.filter(
  791. (it) => it.id !== item.id
  792. );
  793. }
  794. });
  795. }
  796. }, 1000);
  797. },
  798. archive(item) {
  799. requestUtil
  800. .async(
  801. `https://web_history.tianyunperfect.cn/memos/archive`,
  802. "post",
  803. {id: item.id},
  804. {}
  805. )
  806. .then((res) => {
  807. this.msg("归档成功");
  808. // 本地缓存更新
  809. this.contentList = this.contentList.filter(
  810. (it) => it.id !== item.id
  811. );
  812. });
  813. },
  814. handleClose(done) {
  815. this.dialogVisible = false;
  816. done();
  817. },
  818. confirmSubmit(done) {
  819. // 提交记录
  820. let res = requestUtil.sync(
  821. "https://web_history.tianyunperfect.cn/memos/update",
  822. "post",
  823. {
  824. id: this.tmpItem.id,
  825. content: this.contentEditor.getValue(),
  826. created_ts: this.tmpItem.created_ts,
  827. resourceIdList: this.cloneFileIds.map((it) => it.id),
  828. },
  829. {}
  830. );
  831. if (res.code === 200) {
  832. this.msg("更新成功");
  833. this.tmpItem.content = this.contentEditor.getValue();
  834. this.resetView(this.tmpItem);
  835. // 更新主页面的图片
  836. this.resourceMap[this.tmpItem.id] = this.cloneFileIds;
  837. // this.search();
  838. }
  839. this.dialogVisible = false;
  840. done();
  841. },
  842. edit(item) {
  843. this.tmpItem = item;
  844. this.cloneFileIds = this.getCloneResourceIds(item.id);
  845. this.dialogVisible = true;
  846. this.$nextTick(() => {
  847. this.contentEditor = new Vditor("vditor", {
  848. height: 360,
  849. toolbarConfig: {
  850. // pin: true,
  851. },
  852. cache: {
  853. enable: false,
  854. },
  855. mode: "ir",
  856. // 其他配置...
  857. after: () => {
  858. this.contentEditor.setValue(this.tmpItem.content);
  859. this.contentEditor.focus();
  860. },
  861. });
  862. });
  863. },
  864. isDisabled(item) {
  865. // 默认不可编辑,这个时候 没有disable属性
  866. return item["disable"] !== false;
  867. },
  868. sendData: function () {
  869. this.sendBtnAble = false;
  870. let content = this.contentStr;
  871. let categories = this.tagChecked;
  872. // 循环拼接类似 #tag #tag2 content
  873. let updatedContent = content;
  874. if (categories.length > 0) {
  875. // 循环拼接类似 #tag #tag2 content
  876. updatedContent =
  877. categories.map((tag) => `#${tag}`).join(" ") + " " + content;
  878. }
  879. // updatedContent 去除尾部的\n,可能有多个
  880. updatedContent = updatedContent.replace(/\n+$/, "");
  881. // updatedContent 最少有一个标签,如果没有就添加一个标签 #todo,检测是否有 #标签
  882. if (!updatedContent.includes("#")) {
  883. updatedContent = "#todo " + updatedContent;
  884. }
  885. let res = requestUtil.sync(
  886. "https://memos.tianyunperfect.cn/api/v1/memo",
  887. "post",
  888. {
  889. content: updatedContent,
  890. resourceIdList: this.tmpFileIds.map((it) => it.id),
  891. },
  892. myHeaders
  893. );
  894. if (res["name"]) {
  895. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  896. this.msg("记录成功");
  897. localStorage.setItem("contentStr", "");
  898. this.contentStr = "";
  899. this.tmpFileIds = [];
  900. this.search();
  901. }
  902. this.sendBtnAble = true;
  903. },
  904. search: function () {
  905. document.documentElement.scrollTo(0, 0);
  906. console.log(this.tagChecked);
  907. let params = {
  908. search: this.searchStr,
  909. tag_str: this.tagChecked.join(","),
  910. page: this.page,
  911. page_size: this.size,
  912. };
  913. let url = "https://web_history.tianyunperfect.cn/memos/list";
  914. let urlWithParams = requestUtil.buildUrl(url, params);
  915. let res = requestUtil.sync(urlWithParams, "get", null, myHeaders);
  916. if (res.code !== 200) {
  917. return;
  918. }
  919. this.contentList = res.res;
  920. this.searchByContent();
  921. },
  922. getAllTags: function () {
  923. this.allTags = requestUtil
  924. .async(
  925. "https://memos.tianyunperfect.cn/api/v1/tag",
  926. "get",
  927. null,
  928. myHeaders
  929. )
  930. .then((res) => {
  931. this.allTags = res;
  932. });
  933. },
  934. confirmSubmit2() {
  935. res = requestUtil.sync(
  936. "https://web_history.tianyunperfect.cn/memos/replace",
  937. "post",
  938. {
  939. old_text: this.old_text,
  940. new_text: this.new_text,
  941. },
  942. {}
  943. );
  944. if (res.code === 200) {
  945. this.msg("替换成功");
  946. this.search();
  947. }
  948. this.dialogVisible2 = false;
  949. },
  950. searchByContent: function () {
  951. this.resourceMap = {};
  952. // contentList 循环获取里面所有的id数组
  953. let idArr = this.contentList.map((item) => item["id"]);
  954. // 获取资源
  955. let ids = idArr.join(",");
  956. if (!ids) {
  957. return;
  958. }
  959. let sourceRes = requestUtil.sync(
  960. "https://web_history.tianyunperfect.cn/memos/resource",
  961. "post",
  962. {ids: ids},
  963. {}
  964. );
  965. // {'code': 200, 'res': [{'memo_id': 1, 'resource_name': 'EfdmvRsodBviDQWRmDiTaV'}]}
  966. // 循环遍历,以memo_id为key,resource_name数组为value
  967. sourceRes["res"].forEach((item) => {
  968. if (this.resourceMap[item["memo_id"]]) {
  969. this.resourceMap[item["memo_id"]].push(item);
  970. } else {
  971. this.resourceMap[item["memo_id"]] = [item];
  972. }
  973. });
  974. },
  975. },
  976. });
  977. // 如果处于编辑状态,则执行esc取消
  978. window.onkeydown = function (event) {
  979. let edit = vm.dialogVisible;
  980. if (!edit) {
  981. // 如果是右箭头,则翻页
  982. if (event.keyCode === 39) {
  983. vm.page++;
  984. // 提示翻页
  985. vm.$message({
  986. message: "翻页",
  987. type: "success",
  988. });
  989. }
  990. // 如果是左箭头,则翻页
  991. if (event.keyCode === 37 && vm.page > 1) {
  992. vm.page--;
  993. vm.$message({
  994. message: "翻页",
  995. type: "success",
  996. });
  997. }
  998. return;
  999. }
  1000. // esc
  1001. if (event.keyCode === 27) {
  1002. vm.dialogVisible = false;
  1003. }
  1004. // 如果是meta + enter 则保存
  1005. if (event.metaKey && event.keyCode === 13) {
  1006. vm.confirmSubmit();
  1007. }
  1008. // 如果是ctrl + enter 则保存
  1009. if (event.ctrlKey && event.keyCode === 13) {
  1010. vm.confirmSubmit();
  1011. }
  1012. };
  1013. </script>
  1014. </body>
  1015. </html>