send_memos.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. type="textarea"
  89. :autosize={minRows:2}
  90. placeholder="请输入内容"
  91. @input="localStorage.setItem(contentStr, getEditContent())"
  92. v-model="contentStr">
  93. </el-input>
  94. <!-- 添加发送按钮 -->
  95. <el-button style="margin-left: 20px; width: 10%; font-size: 2rem" @click="sendData" :disabled="!sendBtnAble">记录</el-button>
  96. </div>
  97. <!-- 添加多选按钮 -->
  98. <el-checkbox-group v-model="tagChecked">
  99. <el-checkbox-button v-for="tag in allTags" :label="tag"></el-checkbox-button>
  100. </el-checkbox-group>
  101. <div id="tags"></div>
  102. </div>
  103. <div style="margin-top: 180px"></div>
  104. <div v-for="item in contentList">
  105. <el-row style="margin: 10px; padding: 10px" class="border">
  106. <el-col :span="16" class="contentLine">
  107. <div style="display: flex">
  108. <el-date-picker
  109. v-model="item.created_ts"
  110. type="datetime"
  111. readonly="true"
  112. placeholder="选择日期时间">
  113. </el-date-picker>
  114. <div style="margin-left: 20px">
  115. <el-checkbox-button v-for="tag in commonTags" :label="tag" :key="item"
  116. @change="checked=>checkChange(checked,item,tag)"
  117. :checked="isChooseTag(item,tag)">{{tag}}
  118. </el-checkbox-button>
  119. </div>
  120. </div>
  121. <div class="myText" @dblclick="edit(item)">
  122. <el-input v-model="item.content" type="textarea" :autosize="{minRows: 1}" readonly="true"></el-input>
  123. </div>
  124. <div>
  125. <el-image v-for="img in resourceMap[item.id]"
  126. :src="'https://memos_assert.tianyunperfect.cn/' + img + '?width=150'"
  127. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + img]"
  128. style="height: 200px; margin: 10px 10px 0 0; cursor: pointer">
  129. </el-image>
  130. </div>
  131. </el-col>
  132. <el-col :span="4" style="display: flex">
  133. <div class="editClass">
  134. <el-button size="small" @click="window.open('https://memos.tianyunperfect.cn/m/' + item.resource_name)">跳转</el-button>
  135. <br>
  136. <el-button size="small" @click="edit(item)">编辑</el-button>
  137. </div>
  138. <div class="editClass">
  139. <el-button size="small" @click="archive(item)">归档</el-button>
  140. <br>
  141. <el-button size="small" type="warning" @click="del(item)">删除</el-button>
  142. </div>
  143. </el-col>
  144. </el-row>
  145. </div>
  146. </div>
  147. <el-dialog
  148. title="提示"
  149. :visible.sync="dialogVisible"
  150. width="60%"
  151. >
  152. <div v-if="tmpItem.id" class="contentLine">
  153. <div>
  154. <el-date-picker
  155. v-model="tmpItem.created_ts"
  156. type="datetime"
  157. value-format="yyyy-MM-ddTHH:mm:ss"
  158. placeholder="选择日期时间">
  159. </el-date-picker>
  160. </div>
  161. <div>
  162. <el-input ref="contentEdit" v-model="tmpItem.content" type="textarea" :autosize="{minRows: 2}"></el-input>
  163. </div>
  164. <div style="display: flex">
  165. <div v-for="img in resourceMap[tmpItem.id]">
  166. <el-image :src="'https://memos_assert.tianyunperfect.cn/' + img + '?width=150'"
  167. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + img]"
  168. style="height: 200px; margin: 10px 10px 0 0; cursor: pointer">
  169. </el-image>
  170. </div>
  171. </div>
  172. </div>
  173. <span slot="footer" class="dialog-footer">
  174. <el-button @click="dialogVisible = false">取 消</el-button>
  175. <el-button type="primary" @click="confirmSubmit">确 定</el-button>
  176. </span>
  177. </el-dialog>
  178. </div>
  179. <!-- 引入Quill库 -->
  180. <!--<script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>-->
  181. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  182. <script>
  183. let authStr = "bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E";
  184. let myHeaders = {
  185. 'Content-type': 'application/json',
  186. 'Authorization': authStr
  187. };
  188. function getRandomId() {
  189. return Math.random().toString(36).substr(2);
  190. }
  191. let vm = new Vue({
  192. el: '#app',
  193. data: {
  194. tmpItem: {},
  195. dialogVisible: false,
  196. page: 1,
  197. size: 10,
  198. searchStr: '',
  199. contentStr: '',
  200. tagChecked: [],
  201. allTags: [],
  202. contentList: [],
  203. sendBtnAble: true,
  204. resourceMap: {}, // 资源map,
  205. commonTags: ['todo', '日记', '梦记', '美食', '备份', '歌曲', '电影', '随笔','社会','我的'],
  206. },
  207. watch: {
  208. tagChecked: function () {
  209. localStorage.setItem('tags', JSON.stringify(this.tagChecked));
  210. this.search();
  211. },
  212. page: function () {
  213. this.search();
  214. },
  215. size: function () {
  216. this.search();
  217. }
  218. },
  219. mounted() {
  220. this.getAllTags();
  221. this.search();
  222. },
  223. methods: {
  224. msg(content) {
  225. this.$message({
  226. message: content,
  227. type: 'success'
  228. });
  229. },
  230. isChooseTag(item, tag) {
  231. return item.content.indexOf("#" + tag + " ") !== -1;
  232. },
  233. checkChange(val, item, tag) {
  234. if (val) {
  235. this.addTag(item, tag);
  236. } else {
  237. this.delTag(item, tag);
  238. }
  239. },
  240. addTag(item, cTag) {
  241. let content = item.content;
  242. // 如果已经有了,就不添加
  243. if (content.indexOf(cTag) !== -1) {
  244. return;
  245. }
  246. item.content = '#' + cTag + ' ' + content;
  247. // 提交记录
  248. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  249. id: item.id,
  250. content: item.content,
  251. created_ts: item.created_ts
  252. }, {});
  253. if (res.code === 200) {
  254. this.msg('更新成功');
  255. // this.search();
  256. }
  257. },
  258. delTag(item, cTag) {
  259. let content = item.content;
  260. // 如果没有,就不删除
  261. if (content.indexOf(cTag) === -1) {
  262. return;
  263. }
  264. item.content = content.replace('#' + cTag + " ", '');
  265. // 提交记录
  266. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  267. id: item.id,
  268. content: item.content,
  269. created_ts: item.created_ts
  270. }, {});
  271. if (res.code === 200) {
  272. this.msg('更新成功');
  273. // this.search();
  274. }
  275. },
  276. del(item) {
  277. // 提醒是否删除
  278. this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(() => {
  279. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/delete`, 'post', {id: item.id}, {}).then(res => {
  280. if (res.code === 200) {
  281. this.msg('删除成功');
  282. // 在 this.contentList 上直接去除
  283. this.contentList = this.contentList.filter(it => it.id !== item.id);
  284. }
  285. });
  286. })
  287. },
  288. archive(item) {
  289. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/archive`, 'post', {id: item.id}, {}).then(res => {
  290. this.msg('归档成功');
  291. this.search();
  292. });
  293. },
  294. handleClose(done) {
  295. this.dialogVisible = false;
  296. done();
  297. },
  298. confirmSubmit(done) {
  299. // 提交记录
  300. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  301. id: this.tmpItem.id,
  302. content: this.tmpItem.content,
  303. created_ts: this.tmpItem.created_ts
  304. }, {});
  305. if (res.code === 200) {
  306. this.msg('更新成功');
  307. // this.search();
  308. }
  309. this.dialogVisible = false;
  310. done();
  311. },
  312. edit(item) {
  313. this.tmpItem = item;
  314. this.dialogVisible = true;
  315. // 获取焦点
  316. this.$nextTick(() => {
  317. this.$refs.contentEdit.focus()
  318. })
  319. },
  320. isDisabled(item) {
  321. // 默认不可编辑,这个时候 没有disable属性
  322. return item['disable'] !== false;
  323. },
  324. sendData: function () {
  325. this.sendBtnAble = false;
  326. let content = this.contentStr;
  327. let categories = this.tagChecked;
  328. // 循环拼接类似 #tag #tag2 content
  329. let updatedContent = content;
  330. if (categories.length > 0) {
  331. // 循环拼接类似 #tag #tag2 content
  332. updatedContent = categories.map(tag => `#${tag}`).join(' ') + ' ' + content;
  333. }
  334. // updatedContent 去除尾部的\n,可能有多个
  335. updatedContent = updatedContent.replace(/\n+$/, '');
  336. let res = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/memo', 'post', {content: updatedContent}, myHeaders);
  337. if (res['name']) {
  338. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  339. this.msg('记录成功');
  340. this.contentStr = '';
  341. this.search();
  342. }
  343. this.sendBtnAble = true;
  344. },
  345. search: function () {
  346. document.documentElement.scrollTo(0, 0);
  347. this.resourceMap = {};
  348. let params = {'search': this.searchStr, tag_str: this.tagChecked.join(','), page: this.page, page_size: this.size};
  349. let url = 'https://web_history.tianyunperfect.cn/memos/list';
  350. let urlWithParams = requestUtil.buildUrl(url, params);
  351. let res = requestUtil.sync(urlWithParams, 'get', null, myHeaders);
  352. if (res.code !== 200) {
  353. return;
  354. }
  355. this.contentList = res.res;
  356. // contentList 循环获取里面所有的id数组
  357. let idArr = this.contentList.map(item => item['id']);
  358. // 获取资源
  359. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resource", 'post', {ids: idArr.join(',')}, {});
  360. // {'code': 200, 'res': [{'memo_id': 1, 'resource_name': 'EfdmvRsodBviDQWRmDiTaV'}, {'memo_id': 4, 'resource_name': '2rY5kmn3AHsy23vK4vSskV'}, {'memo_id': 7, 'resource_name': 'FMhzJXjoTa7fdtbbCBjXnt'}]}
  361. // 循环遍历,以memo_id为key,resource_name数组为value
  362. sourceRes['res'].forEach(item => {
  363. if (this.resourceMap[item['memo_id']]) {
  364. this.resourceMap[item['memo_id']].push(item['internal_path']);
  365. } else {
  366. this.resourceMap[item['memo_id']] = [item['internal_path']];
  367. }
  368. });
  369. },
  370. getAllTags: function () {
  371. this.allTags = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/tag', 'get', null, myHeaders);
  372. let item1 = localStorage.getItem('tags'); // 获取本地存储的标签
  373. if (item1) {
  374. this.tagChecked = JSON.parse(item1);
  375. }
  376. }
  377. }
  378. });
  379. // 如果处于编辑状态,则执行esc取消
  380. window.onkeydown = function (event) {
  381. let edit = vm.dialogVisible;
  382. if (!edit) {
  383. return;
  384. }
  385. // esc
  386. if (event.keyCode === 27) {
  387. vm.dialogVisible = false;
  388. }
  389. // 如果是meta + enter 则保存
  390. if (event.metaKey && event.keyCode === 13) {
  391. vm.confirmSubmit();
  392. }
  393. // 如果是ctrl + enter 则保存
  394. if (event.ctrlKey && event.keyCode === 13) {
  395. vm.confirmSubmit();
  396. }
  397. };
  398. </script>
  399. </body>
  400. </html>