send_memos.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. }
  31. .editor {
  32. min-height: 50px;
  33. padding: 10px;
  34. }
  35. #mask {
  36. position: fixed;
  37. top: 0;
  38. left: 0;
  39. width: 100%;
  40. height: 100%;
  41. background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色背景 */
  42. display: flex;
  43. justify-content: center;
  44. align-items: center;
  45. }
  46. #model {
  47. position: absolute;
  48. top: 50%;
  49. left: 50%;
  50. transform: translate(-50%, -50%);
  51. background-color: white;
  52. width: 50%;
  53. padding: 20px;
  54. border-radius: 5px;
  55. }
  56. .myTop {
  57. position: fixed;
  58. top: 0;
  59. background-image: url("./img/white.png");
  60. /* 背景铺满整个div*/
  61. background-size: 100% 100%;
  62. width: 100%;
  63. padding: 10px;
  64. z-index: 100;
  65. }
  66. </style>
  67. <script src="js/vue.min.js"></script>
  68. <script src="elementUI/index.js"></script>
  69. <link rel="stylesheet" href="elementUI/index.css">
  70. </head>
  71. <body>
  72. <div id="app">
  73. <div id="all">
  74. <div class="myTop">
  75. <el-link href="https://memos.tianyunperfect.cn/">memos主页</el-link>
  76. <!-- 页码输入框,默认为1,修改触发查询-->
  77. <span style="margin-left: 20px">页码:</span>
  78. <el-input type="number" style="width: 80px; margin-left: 20px" v-model="page" @change="search"></el-input>
  79. <span style="margin-left: 20px">大小:</span>
  80. <el-input type="number" style="width: 80px; margin-left: 20px" v-model="size" @change="search" step="10"></el-input>
  81. <!--输入框,绑定回车事件,触发查询-->
  82. <el-input style="margin-left: 30px; width: 70vw; padding-left: 10px" placeholder="搜索" v-model="searchStr" @change="search"></el-input>
  83. <br><br>
  84. <div></div>
  85. <div style="display: flex;align-items:stretch">
  86. <!-- 创建一个用于编辑的容器 -->
  87. <el-input
  88. @keydown.enter.native="keyDown"
  89. type="textarea"
  90. :autosize={minRows:2}
  91. placeholder="请输入内容"
  92. @input="localStorage.setItem('contentStr', contentStr)"
  93. v-model="contentStr">
  94. </el-input>
  95. <!-- 添加发送按钮 -->
  96. <el-button style="margin-left: 20px; width: 10%; font-size: 2rem" @click="sendData" :disabled="!sendBtnAble">记录</el-button>
  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 + '?width=150'"
  128. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + img]"
  129. style="height: 200px; margin: 10px 10px 0 0; cursor: pointer">
  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 ref="contentEdit" v-model="tmpItem.content" type="textarea" :autosize="{minRows: 2}"></el-input>
  164. </div>
  165. <div style="display: flex">
  166. <div v-for="img in resourceMap[tmpItem.id]">
  167. <el-image :src="'https://memos_assert.tianyunperfect.cn/' + img + '?width=150'"
  168. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + img]"
  169. style="height: 200px; margin: 10px 10px 0 0; cursor: pointer">
  170. </el-image>
  171. </div>
  172. </div>
  173. </div>
  174. <span slot="footer" class="dialog-footer">
  175. <el-button @click="dialogVisible = false">取 消</el-button>
  176. <el-button type="primary" @click="confirmSubmit">确 定</el-button>
  177. </span>
  178. </el-dialog>
  179. </div>
  180. <!-- 引入Quill库 -->
  181. <!--<script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>-->
  182. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  183. <script>
  184. let authStr = "bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E";
  185. let myHeaders = {
  186. 'Content-type': 'application/json',
  187. 'Authorization': authStr
  188. };
  189. function getRandomId() {
  190. return Math.random().toString(36).substr(2);
  191. }
  192. let vm = new Vue({
  193. el: '#app',
  194. data: {
  195. tmpItem: {},
  196. dialogVisible: false,
  197. page: 1,
  198. size: 10,
  199. searchStr: '',
  200. contentStr: '',
  201. tagChecked: [],
  202. allTags: [],
  203. contentList: [],
  204. sendBtnAble: true,
  205. resourceMap: {}, // 资源map,
  206. commonTags: ['todo', '日记', '梦记', '美食', '备份', '歌曲', '电影', '随笔', '社会', '人生'],
  207. },
  208. watch: {
  209. tagChecked: function () {
  210. localStorage.setItem('tags', JSON.stringify(this.tagChecked));
  211. this.search();
  212. },
  213. page: function () {
  214. this.search();
  215. },
  216. size: function () {
  217. this.search();
  218. }
  219. },
  220. mounted() {
  221. // 初始化contentStr,如果本地有缓存
  222. let item = localStorage.getItem('contentStr');
  223. if (item) {
  224. this.contentStr = item;
  225. }
  226. this.getAllTags();
  227. this.search();
  228. },
  229. methods: {
  230. keyDown() {
  231. // 如果是ctrl + enter \ meta + enter 则发送
  232. if (event.ctrlKey || event.metaKey) {
  233. this.sendData();
  234. }
  235. },
  236. msg(content) {
  237. this.$message({
  238. message: content,
  239. type: 'success'
  240. });
  241. },
  242. isChooseTag(item, tag) {
  243. return item.content.indexOf("#" + tag + " ") !== -1;
  244. },
  245. checkChange(val, item, tag) {
  246. if (val) {
  247. this.addTag(item, tag);
  248. } else {
  249. this.delTag(item, tag);
  250. }
  251. },
  252. addTag(item, cTag) {
  253. let content = item.content;
  254. // 如果已经有了,就不添加
  255. if (content.indexOf(cTag) !== -1) {
  256. return;
  257. }
  258. item.content = '#' + cTag + ' ' + content;
  259. // 提交记录
  260. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  261. id: item.id,
  262. content: item.content,
  263. created_ts: item.created_ts
  264. }, {});
  265. if (res.code === 200) {
  266. this.msg('更新成功');
  267. // this.search();
  268. }
  269. },
  270. delTag(item, cTag) {
  271. let content = item.content;
  272. // 如果没有,就不删除
  273. if (content.indexOf(cTag) === -1) {
  274. return;
  275. }
  276. item.content = content.replace('#' + cTag + " ", '');
  277. // 提交记录
  278. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  279. id: item.id,
  280. content: item.content,
  281. created_ts: item.created_ts
  282. }, {});
  283. if (res.code === 200) {
  284. this.msg('更新成功');
  285. // this.search();
  286. }
  287. },
  288. del(item) {
  289. // 提醒是否删除
  290. this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(() => {
  291. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/delete`, 'post', {id: item.id}, {}).then(res => {
  292. if (res.code === 200) {
  293. this.msg('删除成功');
  294. // 在 this.contentList 上直接去除
  295. this.contentList = this.contentList.filter(it => it.id !== item.id);
  296. }
  297. });
  298. })
  299. },
  300. archive(item) {
  301. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/archive`, 'post', {id: item.id}, {}).then(res => {
  302. this.msg('归档成功');
  303. this.search();
  304. });
  305. },
  306. handleClose(done) {
  307. this.dialogVisible = false;
  308. done();
  309. },
  310. confirmSubmit(done) {
  311. // 提交记录
  312. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  313. id: this.tmpItem.id,
  314. content: this.tmpItem.content,
  315. created_ts: this.tmpItem.created_ts
  316. }, {});
  317. if (res.code === 200) {
  318. this.msg('更新成功');
  319. // this.search();
  320. }
  321. this.dialogVisible = false;
  322. done();
  323. },
  324. edit(item) {
  325. this.tmpItem = item;
  326. this.dialogVisible = true;
  327. // 获取焦点
  328. this.$nextTick(() => {
  329. this.$refs.contentEdit.focus()
  330. })
  331. },
  332. isDisabled(item) {
  333. // 默认不可编辑,这个时候 没有disable属性
  334. return item['disable'] !== false;
  335. },
  336. sendData: function () {
  337. this.sendBtnAble = false;
  338. let content = this.contentStr;
  339. let categories = this.tagChecked;
  340. // 循环拼接类似 #tag #tag2 content
  341. let updatedContent = content;
  342. if (categories.length > 0) {
  343. // 循环拼接类似 #tag #tag2 content
  344. updatedContent = categories.map(tag => `#${tag}`).join(' ') + ' ' + content;
  345. }
  346. // updatedContent 去除尾部的\n,可能有多个
  347. updatedContent = updatedContent.replace(/\n+$/, '');
  348. let res = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/memo', 'post', {content: updatedContent}, myHeaders);
  349. if (res['name']) {
  350. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  351. this.msg('记录成功');
  352. localStorage.setItem('contentStr', '');
  353. this.contentStr = '';
  354. this.search();
  355. }
  356. this.sendBtnAble = true;
  357. },
  358. search: function () {
  359. document.documentElement.scrollTo(0, 0);
  360. this.resourceMap = {};
  361. let params = {'search': this.searchStr, tag_str: this.tagChecked.join(','), page: this.page, page_size: this.size};
  362. let url = 'https://web_history.tianyunperfect.cn/memos/list';
  363. let urlWithParams = requestUtil.buildUrl(url, params);
  364. let res = requestUtil.sync(urlWithParams, 'get', null, myHeaders);
  365. if (res.code !== 200) {
  366. return;
  367. }
  368. this.contentList = res.res;
  369. // contentList 循环获取里面所有的id数组
  370. let idArr = this.contentList.map(item => item['id']);
  371. // 获取资源
  372. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resource", 'post', {ids: idArr.join(',')}, {});
  373. // {'code': 200, 'res': [{'memo_id': 1, 'resource_name': 'EfdmvRsodBviDQWRmDiTaV'}, {'memo_id': 4, 'resource_name': '2rY5kmn3AHsy23vK4vSskV'}, {'memo_id': 7, 'resource_name': 'FMhzJXjoTa7fdtbbCBjXnt'}]}
  374. // 循环遍历,以memo_id为key,resource_name数组为value
  375. sourceRes['res'].forEach(item => {
  376. if (this.resourceMap[item['memo_id']]) {
  377. this.resourceMap[item['memo_id']].push(item['internal_path']);
  378. } else {
  379. this.resourceMap[item['memo_id']] = [item['internal_path']];
  380. }
  381. });
  382. },
  383. getAllTags: function () {
  384. this.allTags = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/tag', 'get', null, myHeaders);
  385. let item1 = localStorage.getItem('tags'); // 获取本地存储的标签
  386. if (item1) {
  387. this.tagChecked = JSON.parse(item1);
  388. }
  389. }
  390. }
  391. });
  392. // 如果处于编辑状态,则执行esc取消
  393. window.onkeydown = function (event) {
  394. let edit = vm.dialogVisible;
  395. if (!edit) {
  396. return;
  397. }
  398. // esc
  399. if (event.keyCode === 27) {
  400. vm.dialogVisible = false;
  401. }
  402. // 如果是meta + enter 则保存
  403. if (event.metaKey && event.keyCode === 13) {
  404. vm.confirmSubmit();
  405. }
  406. // 如果是ctrl + enter 则保存
  407. if (event.ctrlKey && event.keyCode === 13) {
  408. vm.confirmSubmit();
  409. }
  410. };
  411. </script>
  412. </body>
  413. </html>