send_memos.html 17 KB

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