memos.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
  6. <title>memos记录</title>
  7. <!-- 引入Quill样式 -->
  8. <!-- <link rel="stylesheet" href="./js/quill.snow.css">-->
  9. <link rel="stylesheet" href="https://registry.npmmirror.com/lxgw-wenkai-screen-web/latest/files/style.css"></link>
  10. <script src="js/util.js"></script>
  11. <script src="js/login.js"></script>
  12. <style>
  13. body {
  14. font-family: "LXGW WenKai Screen", sans-serif !important;
  15. }
  16. img {
  17. border: 1px solid #b7b6b6;
  18. }
  19. .img {
  20. border: 1px solid #b7b6b6;
  21. max-width: 200px;
  22. max-height: 200px;
  23. margin: 10px 10px 0 0 !important;
  24. cursor: pointer
  25. }
  26. .editClass button {
  27. display: inline;
  28. /*margin-top: 5px;*/
  29. margin-left: 30px;
  30. }
  31. .contentLine div {
  32. margin: 5px 0 0;
  33. }
  34. .border {
  35. /*border: 1px solid #b7b6b6;*/
  36. border-radius: 5px;
  37. padding: 10px;
  38. background-color: white;
  39. }
  40. body {
  41. background-color: #f4f4f5;
  42. }
  43. .myTop {
  44. position: fixed;
  45. top: 0;
  46. background-image: url("./img/gray.png");
  47. /* 背景铺满整个div*/
  48. background-size: 100% 100%;
  49. width: 100%;
  50. padding: 10px;
  51. z-index: 100;
  52. }
  53. </style>
  54. <script src="js/vue.min.js"></script>
  55. <script src="elementUI/index.js"></script>
  56. <link rel="stylesheet" href="elementUI/index.css">
  57. </head>
  58. <body>
  59. <div id="app">
  60. <div id="all">
  61. <div class="myTop">
  62. <el-link href="https://memos.tianyunperfect.cn/">memos主页</el-link>
  63. <el-button @click="page = page - 1;search()" :disabled="page === 1">上一页</el-button>
  64. <el-input type="number" style="width: 80px;" v-model="page" @change="search"></el-input>
  65. <el-button @click="page = page + 1;search()">下一页</el-button>
  66. <el-select v-model="size" placeholder="请选择" style="width: 80px" @change="search">
  67. <el-option
  68. v-for="item in [10, 20, 50, 100]"
  69. :key="item"
  70. :label="item"
  71. :value="item">
  72. </el-option>
  73. </el-select>
  74. <!--输入框,绑定回车事件,触发查询-->
  75. <el-input style="margin-left: 30px; width: 40vw; padding-left: 10px" placeholder="搜索" v-model="searchStr" @change="searchStrChange"></el-input>
  76. <br><br>
  77. <div></div>
  78. <div style="display: flex;align-items:stretch; width: 70vw">
  79. <!-- 创建一个用于编辑的容器 -->
  80. <el-input
  81. @paste.native="handlePaste"
  82. @keydown.enter.native="keyDown"
  83. type="textarea"
  84. :autosize={minRows:2}
  85. placeholder="请输入内容"
  86. @input="localStorage.setItem('contentStr', contentStr)"
  87. v-model="contentStr">
  88. </el-input>
  89. <!-- 添加发送按钮 -->
  90. <el-button style="margin-left: 20px; width: 10%; font-size: 2rem" @click="sendData" :disabled="!sendBtnAble">记录</el-button>
  91. <el-button style="margin-left: 20px; width: 10%; font-size: 1rem" @click="sendData">合并</el-button>
  92. </div>
  93. <div style="display: flex">
  94. <div v-for="item in tmpFileIds">
  95. <el-image v-if="item['type'].startsWith('image')"
  96. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  97. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  98. class="img"></el-image>
  99. <audio controls v-if="item['type'].startsWith('audio')">
  100. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="audio/ogg">
  101. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="audio/mpeg">
  102. 您的浏览器不支持 audio 元素。
  103. </audio>
  104. <video width="320" height="240" controls v-if="item['type'].startsWith('video')">
  105. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="video/mp4">
  106. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="video/ogg">
  107. 您的浏览器不支持 HTML5 video 元素。
  108. </video>
  109. <el-link v-if="item['type'].startsWith('application')" :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" target="_blank">{{item['filename']}}</el-link>
  110. <div @click="removeTmp(item)">删除</div>
  111. </div>
  112. </div>
  113. <!-- 添加多选按钮 -->
  114. <div style="display: flex;">
  115. <div>
  116. <el-checkbox-group v-model="tagChecked" @change="checkChange1()">
  117. <el-checkbox-button v-for="tag in allTags" :label="tag"></el-checkbox-button>
  118. </el-checkbox-group>
  119. </div>
  120. <div style="margin-left: 20px; display: flex;align-items: center">
  121. <el-switch
  122. @change="duoxuanChange"
  123. v-model="duoxuan"
  124. active-text="多选"
  125. inactive-text="单选"
  126. >
  127. </el-switch>
  128. </div>
  129. </div>
  130. <div id="tags"></div>
  131. </div>
  132. <div style="margin-top: 180px"></div>
  133. <div v-for="item in contentList">
  134. <el-row style="margin: 10px; padding: 10px" class="border">
  135. <el-col :span="16" class="contentLine">
  136. <div style="display: flex">
  137. <el-date-picker
  138. v-model="item.created_ts"
  139. type="datetime"
  140. readonly="true"
  141. placeholder="选择日期时间">
  142. </el-date-picker>
  143. <div style="margin-left: 20px">
  144. <el-checkbox-button v-for="tag in commonTags" :label="tag" :key="item"
  145. @change="checked=>checkChange(checked,item,tag)"
  146. :checked="isChooseTag(item,tag)">{{tag}}
  147. </el-checkbox-button>
  148. </div>
  149. </div>
  150. <div class="myText" @dblclick="edit(item)">
  151. <el-input v-model="item.content" type="textarea" :autosize="{minRows: 1}" readonly="true"></el-input>
  152. </div>
  153. <div>
  154. <div v-for="item in resourceMap[item.id]">
  155. <el-image v-if="item['type'].startsWith('image')"
  156. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  157. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  158. class="img">
  159. </el-image>
  160. <audio controls v-if="item['type'].startsWith('audio')">
  161. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="audio/ogg">
  162. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="audio/mpeg">
  163. 您的浏览器不支持 audio 元素。
  164. </audio>
  165. <video width="320" height="240" controls v-if="item['type'].startsWith('video')">
  166. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="video/mp4">
  167. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="video/ogg">
  168. 您的浏览器不支持 HTML5 video 元素。
  169. </video>
  170. <el-link v-if="item['type'].startsWith('application')" :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" target="_blank">{{item['filename']}}</el-link>
  171. </div>
  172. </div>
  173. </el-col>
  174. <el-col :span="4" style="display: flex">
  175. <div class="editClass">
  176. <el-button size="small" @click="window.open('https://memos.tianyunperfect.cn/m/' + item.resource_name)">跳转</el-button>
  177. <br>
  178. <br>
  179. <el-button size="small" @click="edit(item)">编辑</el-button>
  180. </div>
  181. <div class="editClass">
  182. <el-button size="small" @click="archive(item)">归档</el-button>
  183. <br>
  184. <br>
  185. <el-button size="small" type="warning" @click="del(item)">删除</el-button>
  186. </div>
  187. <div class="editClass" style="margin-left: 30px">
  188. <el-checkbox :value="selectedIds.includes(item.id)" @change="handleCheckboxChange(item.id, $event)">合并</el-checkbox>
  189. </div>
  190. </el-col>
  191. </el-row>
  192. </div>
  193. </div>
  194. <el-dialog
  195. title="提示"
  196. :visible.sync="dialogVisible"
  197. width="60%"
  198. >
  199. <div v-if="tmpItem.id" class="contentLine">
  200. <div>
  201. <el-date-picker
  202. v-model="tmpItem.created_ts"
  203. type="datetime"
  204. value-format="yyyy-MM-ddTHH:mm:ss"
  205. placeholder="选择日期时间">
  206. </el-date-picker>
  207. </div>
  208. <div>
  209. <el-input
  210. @paste.native="handlePaste2"
  211. ref="contentEdit" v-model="tmpItem.content" type="textarea" :autosize="{minRows: 2}"></el-input>
  212. </div>
  213. <div style="display: flex">
  214. <div v-for="item in cloneFileIds">
  215. <el-image v-if="item['type'].startsWith('image')"
  216. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  217. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  218. class="img">
  219. </el-image>
  220. <audio controls v-if="item['type'].startsWith('audio')">
  221. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="audio/ogg">
  222. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="audio/mpeg">
  223. 您的浏览器不支持 audio 元素。
  224. </audio>
  225. <video width="320" height="240" controls v-if="item['type'].startsWith('video')">
  226. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="video/mp4">
  227. <source :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" type="video/ogg">
  228. 您的浏览器不支持 HTML5 video 元素。
  229. </video>
  230. <el-link v-if="item['type'].startsWith('application')" :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']" target="_blank">{{item['filename']}}</el-link>
  231. <div @click="delPic(img)">删除</div>
  232. </div>
  233. </div>
  234. </div>
  235. <span slot="footer" class="dialog-footer">
  236. <el-button @click="dialogVisible = false">取 消</el-button>
  237. <el-button type="primary" @click="confirmSubmit">确 定</el-button>
  238. </span>
  239. </el-dialog>
  240. </div>
  241. <!-- 引入Quill库 -->
  242. <!--<script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>-->
  243. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  244. <script>
  245. // if (getQueryString("id") !== "tianyunperfect") {
  246. // // location.href = "http://127.0.0.1";
  247. // }
  248. let authStr = "bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E";
  249. let myHeaders = {
  250. 'Content-type': 'application/json',
  251. 'Authorization': authStr
  252. };
  253. function getRandomId() {
  254. return Math.random().toString(36).substr(2);
  255. }
  256. let vm = new Vue({
  257. el: '#app',
  258. data: {
  259. selectedIds: [],
  260. duoxuan: false,
  261. tmpItem: {},
  262. dialogVisible: false,
  263. page: 1,
  264. size: 20,
  265. searchStr: '',
  266. contentStr: '',
  267. tagChecked: [],
  268. allTags: [],
  269. contentList: [],
  270. sendBtnAble: true,
  271. resourceMap: {}, // 资源map,
  272. commonTags: ['todo'],
  273. upload_url: 'https://memos.tianyunperfect.cn/api/v1/resource/blob',
  274. tmpFileIds: [],
  275. cloneFileIds: []
  276. },
  277. watch: {
  278. page: function () {
  279. this.search();
  280. },
  281. size: function () {
  282. this.search();
  283. }
  284. },
  285. mounted() {
  286. if (!checkLogin()) {
  287. return;
  288. }
  289. if (this.getQueryStr()) {
  290. this.searchStr = this.getQueryStr();
  291. }
  292. this.duoxuan = this.getDuoXuan();
  293. // 初始化contentStr,如果本地有缓存
  294. let item = localStorage.getItem('contentStr');
  295. if (item) {
  296. this.contentStr = item;
  297. }
  298. let queryStr = this.getQueryStr();
  299. if (queryStr) {
  300. // queryStr 解码
  301. queryStr = decodeURIComponent(queryStr);
  302. this.searchStr = queryStr;
  303. }
  304. // 解码
  305. let tagsStr = decodeURIComponent(getQueryString("tags"));
  306. if (tagsStr) {
  307. this.tagChecked = tagsStr.split(',')
  308. }
  309. this.getAllTags();
  310. this.search();
  311. requestUtil.async('https://web_history.tianyunperfect.cn/memos/getCustomMemoTags', 'get').then(res => {
  312. this.commonTags = JSON.parse(res['res'][0]['content']);
  313. });
  314. },
  315. methods: {
  316. handleCheckboxChange(id, checked) {
  317. if (checked) {
  318. this.selectedIds.push(id);
  319. } else {
  320. this.selectedIds = this.selectedIds.filter(selectedId => selectedId !== id);
  321. }
  322. console.log(this.selectedIds)
  323. },
  324. searchStrChange() {
  325. let queryStr = this.searchStr;
  326. // url编码
  327. queryStr = encodeURIComponent(queryStr);
  328. location.href = requestUtil.buildUrl(location.href, {'queryStr': queryStr})
  329. },
  330. duoxuanChange() {
  331. if (this.duoxuan) {
  332. this.setDuoXuan('ok');
  333. } else {
  334. this.setDuoXuan('');
  335. }
  336. },
  337. checkChange1(newVal) {
  338. console.log(this.tagChecked)
  339. if (!this.getDuoXuan()) {
  340. if (this.tagChecked.length > 1) {
  341. // 保留最后一个
  342. this.tagChecked = [this.tagChecked[this.tagChecked.length - 1]];
  343. }
  344. }
  345. location.href = requestUtil.buildUrl(location.href, {'tags': this.tagChecked.join(',')})
  346. },
  347. getQueryStr() {
  348. return getQueryString('queryStr')
  349. },
  350. getDuoXuan() {
  351. return getQueryString('duoxuan') == "ok";
  352. },
  353. setDuoXuan(ok) {
  354. location.href = requestUtil.buildUrl(location.href, {'duoxuan': ok})
  355. },
  356. removeTmp(item) {
  357. // tmpFileIds 根据id删除
  358. let index = this.tmpFileIds.findIndex(item => item.id === item.id);
  359. if (index !== -1) {
  360. this.tmpFileIds.splice(index, 1);
  361. }
  362. },
  363. delPic(img) {
  364. // 根据id从 cloneFileIds 删除
  365. let index = this.cloneFileIds.findIndex(item => item.id === img.id);
  366. if (index !== -1) {
  367. this.cloneFileIds.splice(index, 1);
  368. }
  369. },
  370. getCloneResourceIds(id) {
  371. let resourceMapElement = this.resourceMap[id];
  372. if (resourceMapElement) {
  373. return JSON.parse(JSON.stringify(resourceMapElement))
  374. }
  375. return []
  376. },
  377. uploadFile(url, file, headers = {}) {
  378. return new Promise((resolve, reject) => {
  379. const xhr = new XMLHttpRequest();
  380. xhr.open('POST', url, true); // 使用异步的方式
  381. const formData = new FormData();
  382. formData.append('file', file);
  383. for (const [key, value] of Object.entries(headers)) {
  384. xhr.setRequestHeader(key, value);
  385. }
  386. xhr.onload = function () {
  387. if (xhr.status === 200) {
  388. resolve(JSON.parse(xhr.responseText));
  389. } else {
  390. reject(new Error(xhr.statusText));
  391. }
  392. };
  393. xhr.onerror = function () {
  394. reject(new Error("Network Error"));
  395. };
  396. xhr.send(formData);
  397. });
  398. },
  399. getFileFromPaste(e) {
  400. let clipboardData = e.clipboardData; // IE
  401. if (!clipboardData) {
  402. //chrome
  403. clipboardData = e.originalEvent.clipboardData;
  404. }
  405. let items = '';
  406. items = (e.clipboardData || window.clipboardData).items;
  407. let file = null;
  408. if (!items || items.length === 0) {
  409. this.$message.error('当前浏览器不支持粘贴本地图片,请打开图片复制后再粘贴!');
  410. return;
  411. }
  412. // 搜索剪切板items
  413. for (let i = 0; i < items.length; i++) {
  414. // 限制上传文件类型
  415. if (items[i].type.indexOf('image') !== -1) {
  416. file = items[i].getAsFile();
  417. break;
  418. }
  419. }
  420. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  421. if (file) {
  422. return file;
  423. }
  424. },
  425. handlePaste(e) {
  426. let file = this.getFileFromPaste(e);
  427. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  428. if (file) {
  429. this.uploadFile(this.upload_url, file, {'Authorization': myHeaders['Authorization']}).then(res => {
  430. let fileId = res['id'];
  431. if (fileId) {
  432. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resourceById", 'post', {ids: fileId}, {});
  433. let sourceRe = sourceRes['res'];
  434. this.tmpFileIds.push(...sourceRe)
  435. }
  436. });
  437. }
  438. },
  439. handlePaste2(e) {
  440. let file = this.getFileFromPaste(e);
  441. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  442. if (file) {
  443. this.uploadFile(this.upload_url, file, {'Authorization': myHeaders['Authorization']}).then(res => {
  444. let fileId = res['id'];
  445. if (fileId) {
  446. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resourceById", 'post', {ids: fileId}, {});
  447. let sourceRe = sourceRes['res'];
  448. this.cloneFileIds.push(...sourceRe)
  449. }
  450. });
  451. }
  452. },
  453. keyDown() {
  454. // 如果是ctrl + enter \ meta + enter 则发送
  455. if (event.ctrlKey || event.metaKey) {
  456. this.sendData();
  457. }
  458. },
  459. msg(content) {
  460. this.$message({
  461. message: content,
  462. type: 'success'
  463. });
  464. },
  465. isChooseTag(item, tag) {
  466. return item.content.indexOf("#" + tag + " ") !== -1;
  467. },
  468. checkChange(val, item, tag) {
  469. if (val) {
  470. this.addTag(item, tag);
  471. } else {
  472. this.delTag(item, tag);
  473. }
  474. },
  475. addTag(item, cTag) {
  476. let content = item.content;
  477. // 如果已经有了,就不添加
  478. let tmpTag = '#' + cTag + ' ';
  479. if (content.indexOf(tmpTag) !== -1) {
  480. return;
  481. }
  482. item.content = tmpTag + content;
  483. // 提交记录
  484. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/updateContent', 'post', {
  485. id: item.id,
  486. content: item.content,
  487. created_ts: item.created_ts
  488. }, {});
  489. if (res.code === 200) {
  490. this.msg('更新成功');
  491. // this.search();
  492. }
  493. },
  494. delTag(item, cTag) {
  495. let content = item.content;
  496. // 如果没有,就不删除
  497. if (content.indexOf(cTag) === -1) {
  498. return;
  499. }
  500. item.content = content.replace('#' + cTag + " ", '');
  501. // 提交记录
  502. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/updateContent', 'post', {
  503. id: item.id,
  504. content: item.content,
  505. created_ts: item.created_ts
  506. }, {});
  507. if (res.code === 200) {
  508. this.msg('更新成功');
  509. // this.search();
  510. }
  511. },
  512. del(item) {
  513. // 提醒是否删除
  514. this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(() => {
  515. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/delete`, 'post', {id: item.id}, {}).then(res => {
  516. if (res.code === 200) {
  517. this.msg('删除成功');
  518. // 在 this.contentList 上直接去除
  519. this.contentList = this.contentList.filter(it => it.id !== item.id);
  520. }
  521. });
  522. })
  523. },
  524. archive(item) {
  525. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/archive`, 'post', {id: item.id}, {}).then(res => {
  526. this.msg('归档成功');
  527. // 本地缓存更新
  528. this.contentList = this.contentList.filter(it => it.id !== item.id);
  529. });
  530. },
  531. handleClose(done) {
  532. this.dialogVisible = false;
  533. done();
  534. },
  535. confirmSubmit(done) {
  536. // 提交记录
  537. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  538. id: this.tmpItem.id,
  539. content: this.tmpItem.content,
  540. created_ts: this.tmpItem.created_ts,
  541. resourceIdList: this.cloneFileIds.map(it => it.id)
  542. }, {});
  543. if (res.code === 200) {
  544. this.msg('更新成功');
  545. // 更新主页面的图片
  546. this.resourceMap[this.tmpItem.id] = this.cloneFileIds;
  547. // this.search();
  548. }
  549. this.dialogVisible = false;
  550. done();
  551. },
  552. edit(item) {
  553. this.tmpItem = item;
  554. this.cloneFileIds = this.getCloneResourceIds(item.id)
  555. this.dialogVisible = true;
  556. // 获取焦点
  557. this.$nextTick(() => {
  558. this.$refs.contentEdit.focus()
  559. })
  560. },
  561. isDisabled(item) {
  562. // 默认不可编辑,这个时候 没有disable属性
  563. return item['disable'] !== false;
  564. },
  565. sendData: function () {
  566. this.sendBtnAble = false;
  567. let content = this.contentStr;
  568. let categories = this.tagChecked;
  569. // 循环拼接类似 #tag #tag2 content
  570. let updatedContent = content;
  571. if (categories.length > 0) {
  572. // 循环拼接类似 #tag #tag2 content
  573. updatedContent = categories.map(tag => `#${tag}`).join(' ') + ' ' + content;
  574. }
  575. // updatedContent 去除尾部的\n,可能有多个
  576. updatedContent = updatedContent.replace(/\n+$/, '');
  577. let res = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/memo', 'post', {content: updatedContent, "resourceIdList": this.tmpFileIds.map(it => it.id)}, myHeaders);
  578. if (res['name']) {
  579. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  580. this.msg('记录成功');
  581. localStorage.setItem('contentStr', '');
  582. this.contentStr = '';
  583. this.tmpFileIds = []
  584. this.search();
  585. }
  586. this.sendBtnAble = true;
  587. },
  588. search: function () {
  589. document.documentElement.scrollTo(0, 0);
  590. this.resourceMap = {};
  591. console.log(this.tagChecked);
  592. let params = {'search': this.searchStr, tag_str: this.tagChecked.join(','), page: this.page, page_size: this.size};
  593. let url = 'https://web_history.tianyunperfect.cn/memos/list';
  594. let urlWithParams = requestUtil.buildUrl(url, params);
  595. let res = requestUtil.sync(urlWithParams, 'get', null, myHeaders);
  596. if (res.code !== 200) {
  597. return;
  598. }
  599. this.contentList = res.res;
  600. // contentList 循环获取里面所有的id数组
  601. let idArr = this.contentList.map(item => item['id']);
  602. // 获取资源
  603. let ids = idArr.join(',');
  604. if (!ids) {
  605. return;
  606. }
  607. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resource", 'post', {ids: ids}, {});
  608. // {'code': 200, 'res': [{'memo_id': 1, 'resource_name': 'EfdmvRsodBviDQWRmDiTaV'}, {'memo_id': 4, 'resource_name': '2rY5kmn3AHsy23vK4vSskV'}, {'memo_id': 7, 'resource_name': 'FMhzJXjoTa7fdtbbCBjXnt'}]}
  609. // 循环遍历,以memo_id为key,resource_name数组为value
  610. sourceRes['res'].forEach(item => {
  611. if (this.resourceMap[item['memo_id']]) {
  612. this.resourceMap[item['memo_id']].push(item);
  613. } else {
  614. this.resourceMap[item['memo_id']] = [item];
  615. }
  616. });
  617. },
  618. getAllTags: function () {
  619. this.allTags = requestUtil.async('https://memos.tianyunperfect.cn/api/v1/tag', 'get', null, myHeaders).then(res => {
  620. this.allTags = res;
  621. })
  622. }
  623. }
  624. });
  625. // 如果处于编辑状态,则执行esc取消
  626. window.onkeydown = function (event) {
  627. let edit = vm.dialogVisible;
  628. if (!edit) {
  629. return;
  630. }
  631. // esc
  632. if (event.keyCode === 27) {
  633. vm.dialogVisible = false;
  634. }
  635. // 如果是meta + enter 则保存
  636. if (event.metaKey && event.keyCode === 13) {
  637. vm.confirmSubmit();
  638. }
  639. // 如果是ctrl + enter 则保存
  640. if (event.ctrlKey && event.keyCode === 13) {
  641. vm.confirmSubmit();
  642. }
  643. };
  644. </script>
  645. </body>
  646. </html>