send_memos.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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>
  166. </div>
  167. </div>
  168. <span slot="footer" class="dialog-footer">
  169. <el-button @click="dialogVisible = false">取 消</el-button>
  170. <el-button type="primary" @click="confirmSubmit">确 定</el-button>
  171. </span>
  172. </el-dialog>
  173. </div>
  174. <!-- 引入Quill库 -->
  175. <!--<script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>-->
  176. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  177. <script>
  178. let authStr = "bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E";
  179. let myHeaders = {
  180. 'Content-type': 'application/json',
  181. 'Authorization': authStr
  182. };
  183. function getRandomId() {
  184. return Math.random().toString(36).substr(2);
  185. }
  186. let vm = new Vue({
  187. el: '#app',
  188. data: {
  189. tmpItem: {},
  190. dialogVisible: false,
  191. page: 1,
  192. size: 20,
  193. searchStr: '',
  194. contentStr: '',
  195. tagChecked: [],
  196. allTags: [],
  197. contentList: [],
  198. sendBtnAble: true,
  199. resourceMap: {}, // 资源map,
  200. commonTags: ['todo', '日记', '梦记', '美食', '备份', '歌曲', '电影', '随笔', '社会', '人生'],
  201. upload_url: 'https://memos.tianyunperfect.cn/api/v1/resource/blob',
  202. tmpFileIds: [],
  203. cloneFileIds:[]
  204. },
  205. watch: {
  206. tagChecked: function () {
  207. localStorage.setItem('tags', JSON.stringify(this.tagChecked));
  208. this.search();
  209. },
  210. page: function () {
  211. this.search();
  212. },
  213. size: function () {
  214. this.search();
  215. }
  216. },
  217. mounted() {
  218. // 初始化contentStr,如果本地有缓存
  219. let item = localStorage.getItem('contentStr');
  220. if (item) {
  221. this.contentStr = item;
  222. }
  223. this.getAllTags();
  224. this.search();
  225. },
  226. methods: {
  227. getCloneResourceIds(id) {
  228. let resourceMapElement = this.resourceMap[id];
  229. if (resourceMapElement) {
  230. return JSON.parse(JSON.stringify(resourceMapElement))
  231. }
  232. return []
  233. },
  234. uploadFile(url, file, headers = {}) {
  235. return new Promise((resolve, reject) => {
  236. const xhr = new XMLHttpRequest();
  237. xhr.open('POST', url, true); // 使用异步的方式
  238. const formData = new FormData();
  239. formData.append('file', file);
  240. for (const [key, value] of Object.entries(headers)) {
  241. xhr.setRequestHeader(key, value);
  242. }
  243. xhr.onload = function () {
  244. if (xhr.status === 200) {
  245. resolve(JSON.parse(xhr.responseText));
  246. } else {
  247. reject(new Error(xhr.statusText));
  248. }
  249. };
  250. xhr.onerror = function () {
  251. reject(new Error("Network Error"));
  252. };
  253. xhr.send(formData);
  254. });
  255. },
  256. getFileFromPaste(e) {
  257. let clipboardData = e.clipboardData; // IE
  258. if (!clipboardData) {
  259. //chrome
  260. clipboardData = e.originalEvent.clipboardData;
  261. }
  262. let items = '';
  263. items = (e.clipboardData || window.clipboardData).items;
  264. let file = null;
  265. if (!items || items.length === 0) {
  266. this.$message.error('当前浏览器不支持粘贴本地图片,请打开图片复制后再粘贴!');
  267. return;
  268. }
  269. // 搜索剪切板items
  270. for (let i = 0; i < items.length; i++) {
  271. // 限制上传文件类型
  272. if (items[i].type.indexOf('image') !== -1) {
  273. file = items[i].getAsFile();
  274. break;
  275. }
  276. }
  277. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  278. if (file) {
  279. return file;
  280. }
  281. },
  282. handlePaste(e) {
  283. let file = this.getFileFromPaste(e);
  284. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  285. if (file) {
  286. this.uploadFile(this.upload_url, file, {'Authorization': myHeaders['Authorization']}).then(res => {
  287. let fileId = res['id'];
  288. if (fileId) {
  289. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resourceById", 'post', {ids: fileId}, {});
  290. let sourceRe = sourceRes['res'];
  291. this.tmpFileIds.push(...sourceRe)
  292. }
  293. });
  294. }
  295. },
  296. handlePaste2(e){
  297. let file = this.getFileFromPaste(e);
  298. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  299. if (file) {
  300. this.uploadFile(this.upload_url, file, {'Authorization': myHeaders['Authorization']}).then(res => {
  301. let fileId = res['id'];
  302. if (fileId) {
  303. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resourceById", 'post', {ids: fileId}, {});
  304. let sourceRe = sourceRes['res'];
  305. this.cloneFileIds.push(...sourceRe)
  306. }
  307. });
  308. }
  309. },
  310. keyDown() {
  311. // 如果是ctrl + enter \ meta + enter 则发送
  312. if (event.ctrlKey || event.metaKey) {
  313. this.sendData();
  314. }
  315. },
  316. msg(content) {
  317. this.$message({
  318. message: content,
  319. type: 'success'
  320. });
  321. },
  322. isChooseTag(item, tag) {
  323. return item.content.indexOf("#" + tag + " ") !== -1;
  324. },
  325. checkChange(val, item, tag) {
  326. if (val) {
  327. this.addTag(item, tag);
  328. } else {
  329. this.delTag(item, tag);
  330. }
  331. },
  332. addTag(item, cTag) {
  333. let content = item.content;
  334. // 如果已经有了,就不添加
  335. if (content.indexOf(cTag) !== -1) {
  336. return;
  337. }
  338. item.content = '#' + cTag + ' ' + content;
  339. // 提交记录
  340. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  341. id: item.id,
  342. content: item.content,
  343. created_ts: item.created_ts
  344. }, {});
  345. if (res.code === 200) {
  346. this.msg('更新成功');
  347. // this.search();
  348. }
  349. },
  350. delTag(item, cTag) {
  351. let content = item.content;
  352. // 如果没有,就不删除
  353. if (content.indexOf(cTag) === -1) {
  354. return;
  355. }
  356. item.content = content.replace('#' + cTag + " ", '');
  357. // 提交记录
  358. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  359. id: item.id,
  360. content: item.content,
  361. created_ts: item.created_ts
  362. }, {});
  363. if (res.code === 200) {
  364. this.msg('更新成功');
  365. // this.search();
  366. }
  367. },
  368. del(item) {
  369. // 提醒是否删除
  370. this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(() => {
  371. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/delete`, 'post', {id: item.id}, {}).then(res => {
  372. if (res.code === 200) {
  373. this.msg('删除成功');
  374. // 在 this.contentList 上直接去除
  375. this.contentList = this.contentList.filter(it => it.id !== item.id);
  376. }
  377. });
  378. })
  379. },
  380. archive(item) {
  381. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/archive`, 'post', {id: item.id}, {}).then(res => {
  382. this.msg('归档成功');
  383. this.search();
  384. });
  385. },
  386. handleClose(done) {
  387. this.dialogVisible = false;
  388. done();
  389. },
  390. confirmSubmit(done) {
  391. // 提交记录
  392. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  393. id: this.tmpItem.id,
  394. content: this.tmpItem.content,
  395. created_ts: this.tmpItem.created_ts,
  396. resourceIdList: this.cloneFileIds.map(it => it.id)
  397. }, {});
  398. if (res.code === 200) {
  399. this.msg('更新成功');
  400. // 更新主页面的图片
  401. this.resourceMap[this.tmpItem.id] = this.cloneFileIds;
  402. // this.search();
  403. }
  404. this.dialogVisible = false;
  405. done();
  406. },
  407. edit(item) {
  408. this.tmpItem = item;
  409. this.cloneFileIds=this.getCloneResourceIds(item.id)
  410. this.dialogVisible = true;
  411. // 获取焦点
  412. this.$nextTick(() => {
  413. this.$refs.contentEdit.focus()
  414. })
  415. },
  416. isDisabled(item) {
  417. // 默认不可编辑,这个时候 没有disable属性
  418. return item['disable'] !== false;
  419. },
  420. sendData: function () {
  421. this.sendBtnAble = false;
  422. let content = this.contentStr;
  423. let categories = this.tagChecked;
  424. // 循环拼接类似 #tag #tag2 content
  425. let updatedContent = content;
  426. if (categories.length > 0) {
  427. // 循环拼接类似 #tag #tag2 content
  428. updatedContent = categories.map(tag => `#${tag}`).join(' ') + ' ' + content;
  429. }
  430. // updatedContent 去除尾部的\n,可能有多个
  431. updatedContent = updatedContent.replace(/\n+$/, '');
  432. let res = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/memo', 'post', {content: updatedContent, "resourceIdList": this.tmpFileIds.map(it => it.id)}, myHeaders);
  433. if (res['name']) {
  434. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  435. this.msg('记录成功');
  436. localStorage.setItem('contentStr', '');
  437. this.contentStr = '';
  438. this.tmpFileIds = []
  439. this.search();
  440. }
  441. this.sendBtnAble = true;
  442. },
  443. search: function () {
  444. document.documentElement.scrollTo(0, 0);
  445. this.resourceMap = {};
  446. let params = {'search': this.searchStr, tag_str: this.tagChecked.join(','), page: this.page, page_size: this.size};
  447. let url = 'https://web_history.tianyunperfect.cn/memos/list';
  448. let urlWithParams = requestUtil.buildUrl(url, params);
  449. let res = requestUtil.sync(urlWithParams, 'get', null, myHeaders);
  450. if (res.code !== 200) {
  451. return;
  452. }
  453. this.contentList = res.res;
  454. // contentList 循环获取里面所有的id数组
  455. let idArr = this.contentList.map(item => item['id']);
  456. // 获取资源
  457. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resource", 'post', {ids: idArr.join(',')}, {});
  458. // {'code': 200, 'res': [{'memo_id': 1, 'resource_name': 'EfdmvRsodBviDQWRmDiTaV'}, {'memo_id': 4, 'resource_name': '2rY5kmn3AHsy23vK4vSskV'}, {'memo_id': 7, 'resource_name': 'FMhzJXjoTa7fdtbbCBjXnt'}]}
  459. // 循环遍历,以memo_id为key,resource_name数组为value
  460. sourceRes['res'].forEach(item => {
  461. if (this.resourceMap[item['memo_id']]) {
  462. this.resourceMap[item['memo_id']].push(item);
  463. } else {
  464. this.resourceMap[item['memo_id']] = [item];
  465. }
  466. });
  467. },
  468. getAllTags: function () {
  469. this.allTags = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/tag', 'get', null, myHeaders);
  470. let item1 = localStorage.getItem('tags'); // 获取本地存储的标签
  471. if (item1) {
  472. this.tagChecked = JSON.parse(item1);
  473. }
  474. }
  475. }
  476. });
  477. // 如果处于编辑状态,则执行esc取消
  478. window.onkeydown = function (event) {
  479. let edit = vm.dialogVisible;
  480. if (!edit) {
  481. return;
  482. }
  483. // esc
  484. if (event.keyCode === 27) {
  485. vm.dialogVisible = false;
  486. }
  487. // 如果是meta + enter 则保存
  488. if (event.metaKey && event.keyCode === 13) {
  489. vm.confirmSubmit();
  490. }
  491. // 如果是ctrl + enter 则保存
  492. if (event.ctrlKey && event.keyCode === 13) {
  493. vm.confirmSubmit();
  494. }
  495. };
  496. </script>
  497. </body>
  498. </html>