send_memos.html 22 KB

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