memos.html 31 KB

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