send_memos.html 22 KB

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