memos.html 33 KB

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