send_memos.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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{font-family: "LXGW WenKai Screen", sans-serif !important;}
  13. .border {
  14. border: 1px solid #b7b6b6;
  15. border-radius: 5px;
  16. }
  17. .editor {
  18. height: 130px;
  19. padding: 10px;
  20. }
  21. #mask {
  22. position: fixed;
  23. top: 0;
  24. left: 0;
  25. width: 100%;
  26. height: 100%;
  27. background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色背景 */
  28. display: flex;
  29. justify-content: center;
  30. align-items: center;
  31. }
  32. #model {
  33. position: absolute;
  34. top: 50%;
  35. left: 50%;
  36. transform: translate(-50%, -50%);
  37. background-color: white;
  38. width: 50%;
  39. padding: 20px;
  40. border-radius: 5px;
  41. }
  42. </style>
  43. <script src="js/markdown.min.js"></script>
  44. </head>
  45. <body>
  46. <div id="all">
  47. <div style="position: fixed;top: 10px;">
  48. <a href="https://memos.tianyunperfect.cn/">memos主页</a>
  49. <!-- 页码输入框,默认为1,修改触发查询-->
  50. <span style="margin-left: 20px">页码:</span>
  51. <input style="width: 50px; margin-left: 20px;padding-left: 10px" type="number" class="border" id="page" value="1" onkeydown="if(event.keyCode===13) {search()}">
  52. <!--输入框,绑定回车事件,触发查询-->
  53. <input style="margin-left: 30px; width: 70vw; padding-left: 10px" placeholder="搜索" type="text" id="search" class="border" onkeydown="if(event.keyCode===13) {search()}">
  54. </div>
  55. <br><br>
  56. <div></div>
  57. <!-- 创建一个用于编辑的容器 -->
  58. <textarea id="editor" class="editor border" rows="4" style="width: 95%" onchange="localStorage.setItem(contentStr, getEditContent())"></textarea>
  59. <!-- 添加多选按钮 -->
  60. <div id="tags"></div>
  61. <br>
  62. <br>
  63. <!-- 添加发送按钮 -->
  64. <button id="log" style="float: right; height: 100px;width: 200px; font-size: 40px" onclick="sendData()">记录</button>
  65. <div id="memos_list">
  66. </div>
  67. </div>
  68. <div id="mask" style="display: none">
  69. <div id="model">
  70. <!-- 创建时间的input-->
  71. <div style="margin: 10px; height: 20px">
  72. <label for="created_ts">创建时间</label>
  73. <input id="created_ts"></input>
  74. </div>
  75. <textarea id="editorModel" class="editor" rows="4" style="width: 95%"></textarea>
  76. <br>
  77. <!-- 取消 和 确认按钮-->
  78. <button onclick="hideModel()" style="margin-right: 50px">取消</button>
  79. <button onclick="updateById()">确认</button>
  80. </div>
  81. </div>
  82. <!-- 引入Quill库 -->
  83. <!--<script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>-->
  84. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  85. <script>
  86. let authStr = "bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E";
  87. let myHeaders = {
  88. 'Content-type': 'application/json',
  89. 'Authorization': authStr
  90. };
  91. // console.log(location.href)
  92. // console.log(location.href.indexOf(tag))
  93. // 设置多选按钮 标签
  94. requestUtil.async('https://memos.tianyunperfect.cn/api/v1/tag', 'get', null, {'Authorization': authStr}).then(res => {
  95. // console.log(res); ['tag1','tag2']
  96. // 在 id tags 里面拼接res数组 ,示例todo标签 <input type="checkbox" id="todo" name="category" value="todo" checked> <label for="todo">todo</label>
  97. let tags = document.getElementById('tags');
  98. res.forEach(tag => {
  99. let input = document.createElement('input');
  100. let tagsFromLocal = getTagsFromLocal();
  101. // 如果是location.href 包含tag,则设置为选中状态, href 要反编译以识别中文
  102. if (decodeURI(location.href).indexOf(tag) !== -1) {
  103. input.checked = true;
  104. } else if (tagsFromLocal.indexOf(tag) !== -1) {
  105. input.checked = true;
  106. }
  107. input.type = 'checkbox';
  108. input.classList.add('border');
  109. input.id = tag;
  110. input.name = 'category';
  111. input.value = tag;
  112. let label = document.createElement('label');
  113. // 设置margin-right 20
  114. label.style.marginRight = '10px';
  115. label.htmlFor = tag;
  116. label.innerText = tag;
  117. tags.appendChild(input);
  118. tags.appendChild(label);
  119. });
  120. search();
  121. // id="tags" 里面的多选按钮,点击后触发search
  122. let checkboxes = document.querySelectorAll('input[name="category"]');
  123. checkboxes.forEach(checkbox => {
  124. checkbox.onclick = function () {
  125. search();
  126. setTagsToLocal();
  127. }
  128. });
  129. myEdit.focus();
  130. });
  131. function setTagsToLocal() {
  132. // 如果是location.href 包含tag,则return,否则存储到localStorage
  133. if (decodeURI(location.href).indexOf("tag") !== -1) {
  134. return;
  135. }
  136. // 存储到localStorage
  137. let tags = getSelectedCategories();
  138. localStorage.setItem('tags', JSON.stringify(tags));
  139. }
  140. function getTagsFromLocal() {
  141. if (decodeURI(location.href).indexOf("tag") !== -1) {
  142. return [];
  143. }
  144. let tags = localStorage.getItem('tags');
  145. if (tags) {
  146. tags = JSON.parse(tags);
  147. return tags;
  148. }
  149. return [];
  150. }
  151. function showModel() {
  152. document.getElementById('mask').style.display = 'block';
  153. }
  154. function hideModel() {
  155. document.getElementById('mask').style.display = 'none';
  156. }
  157. // 获取编辑器
  158. const myEdit = document.querySelector('#editor');
  159. const model = document.querySelector('#editorModel');
  160. function getEditContent() {
  161. return myEdit.value;
  162. }
  163. function getModelContent() {
  164. return model.value;
  165. }
  166. let contentStr = 'content';
  167. // 从localStorage中获取内容
  168. let item = localStorage.getItem(contentStr);
  169. if (item != null) {
  170. myEdit.value = item;
  171. }
  172. // 如果处于编辑状态,则执行esc取消
  173. window.onkeydown = function (event) {
  174. let edit = document.getElementById('mask').style.display === 'block';
  175. if (!edit) {
  176. return;
  177. }
  178. // esc
  179. if (event.keyCode === 27) {
  180. hideModel();
  181. }
  182. // 如果是meta + enter 则保存
  183. if (event.metaKey && event.keyCode === 13) {
  184. updateById();
  185. }
  186. // 如果是ctrl + enter 则保存
  187. if (event.ctrlKey && event.keyCode === 13) {
  188. updateById();
  189. }
  190. };
  191. // 发送数据
  192. async function sendData() {
  193. // 使id=log 不可用
  194. let logBtn = document.getElementById('log');
  195. logBtn.disabled = true;
  196. logBtn.innerText = '正在发送...';
  197. const content = getEditContent();
  198. const categories = getSelectedCategories();
  199. // 添加多选按钮选择的分类到内容前面
  200. let updatedContent = content;
  201. if (categories.length > 0) {
  202. // 循环拼接类似 #tag #tag2 content
  203. updatedContent = categories.map(tag => `#${tag}`).join(' ') + ' ' + content;
  204. }
  205. // updatedContent 去除尾部的\n,可能有多个
  206. updatedContent = updatedContent.replace(/\n+$/, '');
  207. // 调用数据备份接口
  208. const data = {
  209. content: updatedContent
  210. };
  211. // 发送异步请求
  212. requestUtil.async('https://memos.tianyunperfect.cn/api/v1/memo', 'post', data, myHeaders).then(res => {
  213. // alert(JSON.stringify(res));
  214. if (res['name']) {
  215. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  216. showMsg('记录成功', 1);
  217. }
  218. logBtn.disabled = false;
  219. logBtn.innerText = '记录';
  220. myEdit.value = '';
  221. localStorage.removeItem(contentStr);
  222. search();
  223. });
  224. }
  225. // 获取选择的分类
  226. function getSelectedCategories() {
  227. const checkboxes = document.querySelectorAll('input[name="category"]:checked');
  228. const categories = [];
  229. checkboxes.forEach(checkbox => {
  230. categories.push(checkbox.value);
  231. });
  232. return categories;
  233. }
  234. let tmpId;
  235. function formatTimeStr(timeStr) {
  236. // "2024-04-25T08:25:54" 转为 "2024-04-25 08:25:54"
  237. return timeStr.replace('T', ' ').substring(0, 19);
  238. }
  239. let resourceMap = {};
  240. // 更新数据
  241. async function updateById() {
  242. let content = getModelContent();
  243. content = content.replace(/\n+$/, '');
  244. let data = {
  245. id: tmpId,
  246. content: content,
  247. created_ts: document.getElementById('created_ts').value
  248. };
  249. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/update`, 'post', data, {}).then(res => {
  250. showMsg('保存成功', 1);
  251. hideModel();
  252. // 根据id ,更新当前页面
  253. document.getElementById('time_' + tmpId).innerText = document.getElementById('created_ts').value;
  254. let contentDom = document.getElementById(tmpId);
  255. contentDom.innerText = content;
  256. if (resourceMap[tmpId]) {
  257. appendImg(contentDom, tmpId);
  258. }
  259. });
  260. }
  261. function appendImg(tmpDiv, id) {
  262. // tmpDiv 新增一个换行
  263. tmpDiv.appendChild(document.createElement('br'));
  264. // 循环遍历,以memo_id为key,resource_name数组为value
  265. resourceMap[id].forEach(internal_path => {
  266. // 创建img
  267. let img = document.createElement('img');
  268. img.src = `https://memos_assert.tianyunperfect.cn/${internal_path}?width=150`;
  269. img.style.maxHeight = '200px';
  270. img.style.maxWidth = '200px';
  271. img.style.margin = '10px 10px 0 0';
  272. img.style.cursor = 'pointer';
  273. // 添加到div
  274. tmpDiv.appendChild(img);
  275. });
  276. }
  277. function search() {
  278. // 页面滚动到顶部
  279. document.documentElement.scrollTo(0, 0);
  280. let searchStr = document.getElementById('search').value;
  281. // 如果标签不为空,则 拼接成 tag_str ,逗号分隔
  282. let tag_str = '';
  283. let checkboxes = document.querySelectorAll('input[name="category"]:checked');
  284. checkboxes.forEach(checkbox => {
  285. tag_str += checkbox.value + ',';
  286. });
  287. // https://web_history.tianyunperfect.cn/memos/list
  288. // get
  289. // 入参:search tag_str
  290. //
  291. // {'code': 200, 'res': [{'id': 11, 'resource_name': 'g2cyE6nXNCdqsm4ZH2R3Bs', 'creator_id': 1, 'created_ts': '2023-07-04T13:35:18', 'updated_ts': '2024-04-22T13:55:25', 'row_status': 'NORMAL', 'content': '康宁的女儿 易安 \n小名:气气\n#备份', 'visibility': 'PRIVATE'}]}
  292. let params = {'search': searchStr, tag_str: tag_str, page: document.getElementById('page').value, page_size: 100};
  293. let url = 'https://web_history.tianyunperfect.cn/memos/list';
  294. // 拼接url
  295. let urlWithParams = requestUtil.buildUrl(url, params);
  296. requestUtil.async(urlWithParams, 'get', null, null).then(res => {
  297. // console.log(res);
  298. res = res['res'];
  299. let div = document.querySelector('#memos_list');
  300. // 如果res为空,则直接返回
  301. if (res.length === 0) {
  302. div.innerHTML = '';
  303. return;
  304. }
  305. // 循环获取里面所有的id数组
  306. let idArr = [];
  307. res.forEach(item => {
  308. idArr.push(item['id']);
  309. });
  310. let sourceUrl = "https://web_history.tianyunperfect.cn/memos/resource"
  311. let sourceData = {
  312. ids: idArr.join(',') // 逗号分隔的字符串
  313. }
  314. // 获取资源
  315. let sourceRes = requestUtil.sync(sourceUrl, 'post', sourceData, {});
  316. // {'code': 200, 'res': [{'memo_id': 1, 'resource_name': 'EfdmvRsodBviDQWRmDiTaV'}, {'memo_id': 4, 'resource_name': '2rY5kmn3AHsy23vK4vSskV'}, {'memo_id': 7, 'resource_name': 'FMhzJXjoTa7fdtbbCBjXnt'}]}
  317. // 循环遍历,以memo_id为key,resource_name数组为value
  318. sourceRes['res'].forEach(item => {
  319. if (resourceMap[item['memo_id']]) {
  320. resourceMap[item['memo_id']].push(item['internal_path']);
  321. } else {
  322. resourceMap[item['memo_id']] = [item['internal_path']];
  323. }
  324. });
  325. // flex 定位
  326. div.innerHTML = '';
  327. res.forEach(item => {
  328. let lineDiv = document.createElement('div');
  329. lineDiv.style.display = 'flex';
  330. lineDiv.style.alignItems = 'flex-end';
  331. div.appendChild(lineDiv);
  332. let contentDiv = document.createElement('div');
  333. contentDiv.className = 'border';
  334. contentDiv.style.width = '70vw';
  335. contentDiv.style.margin = '10px';
  336. contentDiv.style.padding = '10px';
  337. contentDiv.style.cursor = 'pointer';
  338. lineDiv.appendChild(contentDiv);
  339. let timeStr = formatTimeStr(item['created_ts']);
  340. // 创建一个span 标签,显示时间
  341. let timeSpan = document.createElement('div');
  342. let id = item['id'];
  343. timeSpan.id = 'time_' + id;
  344. timeSpan.innerText = timeStr;
  345. timeSpan.style.marginBottom = '5px';
  346. contentDiv.appendChild(timeSpan);
  347. let tmpDiv = document.createElement('div');
  348. tmpDiv.id = id;
  349. tmpDiv.innerText = item['content'];
  350. // 宽度100%
  351. tmpDiv.style.width = '100%';
  352. // 样式加上border,点击跳转到对应的url https://memos.tianyunperfect.cn/m/ + item['resource_name']
  353. // inline
  354. tmpDiv.style.display = 'inline-block';
  355. tmpDiv.ondblclick = function () {
  356. tmpId = id;
  357. model.value = document.getElementById(id).innerText;
  358. document.getElementById('created_ts').value = document.getElementById('time_' + id).innerText;
  359. showModel();
  360. model.focus();
  361. };
  362. // 根据id,查找是否存在资源
  363. if (resourceMap[id]) {
  364. appendImg(tmpDiv, id);
  365. }
  366. contentDiv.appendChild(tmpDiv);
  367. // 右侧添加一个编辑按钮
  368. let editBtn = document.createElement('button');
  369. editBtn.innerText = '跳转查看';
  370. // margin 10
  371. editBtn.style.margin = '10px';
  372. // inline
  373. editBtn.style.display = 'inline-block';
  374. editBtn.onclick = function () {
  375. window.open(`https://memos.tianyunperfect.cn/m/${item['resource_name']}`);
  376. };
  377. lineDiv.appendChild(editBtn);
  378. // 归档按钮
  379. let archiveBtn = document.createElement('button');
  380. archiveBtn.innerText = '归档';
  381. archiveBtn.style.margin = '10px';
  382. // inline
  383. archiveBtn.style.display = 'inline-block';
  384. archiveBtn.onclick = function () {
  385. let data = {
  386. id: id
  387. };
  388. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/archive`, 'post', data, {}).then(res => {
  389. showMsg('归档成功', 1);
  390. lineDiv.style.display = 'none';
  391. });
  392. };
  393. lineDiv.appendChild(archiveBtn);
  394. // 删除按钮
  395. let delBtn = document.createElement('button');
  396. delBtn.innerText = '删除';
  397. delBtn.style.margin = '10px';
  398. // inline
  399. delBtn.style.display = 'inline-block';
  400. delBtn.onclick = function () {
  401. // 确认是否删除
  402. if (!confirm('确认删除吗?')) {
  403. return;
  404. }
  405. let data = {
  406. id: id
  407. };
  408. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/delete`, 'post', data, {}).then(res => {
  409. showMsg('删除成功', 1);
  410. lineDiv.style.display = 'none';
  411. });
  412. };
  413. lineDiv.appendChild(delBtn);
  414. });
  415. });
  416. }
  417. </script>
  418. </body>
  419. </html>