send_memos.html 17 KB

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