send_memos.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. background-color: white;
  31. }
  32. body {
  33. background-color: #f4f4f5;
  34. }
  35. .myTop {
  36. position: fixed;
  37. top: 0;
  38. background-image: url("./img/gray.png");
  39. /* 背景铺满整个div*/
  40. background-size: 100% 100%;
  41. width: 100%;
  42. padding: 10px;
  43. z-index: 100;
  44. }
  45. </style>
  46. <script src="js/vue.min.js"></script>
  47. <script src="elementUI/index.js"></script>
  48. <link rel="stylesheet" href="elementUI/index.css">
  49. </head>
  50. <body>
  51. <div id="app">
  52. <div id="all">
  53. <div class="myTop">
  54. <el-link href="https://memos.tianyunperfect.cn/">memos主页</el-link>
  55. <el-button @click="page = page - 1;search()" :disabled="page === 1">上一页</el-button>
  56. <el-input type="number" style="width: 80px;" v-model="page" @change="search"></el-input>
  57. <el-button @click="page = page + 1;search()">下一页</el-button>
  58. <el-select v-model="size" placeholder="请选择" style="width: 80px" @change="search">
  59. <el-option
  60. v-for="item in [10, 20, 50, 100]"
  61. :key="item"
  62. :label="item"
  63. :value="item">
  64. </el-option>
  65. </el-select>
  66. <!--输入框,绑定回车事件,触发查询-->
  67. <el-input style="margin-left: 30px; width: 40vw; padding-left: 10px" placeholder="搜索" v-model="searchStr" @change="search"></el-input>
  68. <br><br>
  69. <div></div>
  70. <div style="display: flex;align-items:stretch; width: 70vw">
  71. <!-- 创建一个用于编辑的容器 -->
  72. <el-input
  73. @paste.native="handlePaste"
  74. @keydown.enter.native="keyDown"
  75. type="textarea"
  76. :autosize={minRows:2}
  77. placeholder="请输入内容"
  78. @input="localStorage.setItem('contentStr', contentStr)"
  79. v-model="contentStr">
  80. </el-input>
  81. <!-- 添加发送按钮 -->
  82. <el-button style="margin-left: 20px; width: 10%; font-size: 2rem" @click="sendData" :disabled="!sendBtnAble">记录</el-button>
  83. </div>
  84. <el-image v-for="item in tmpFileIds"
  85. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  86. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path']]"
  87. style="height: 200px; margin: 10px 10px 0 0; cursor: pointer"></el-image>
  88. <div v-if="tmpFileIds.length>0">
  89. </div>
  90. <!-- 添加多选按钮 -->
  91. <el-checkbox-group v-model="tagChecked">
  92. <el-checkbox-button v-for="tag in allTags" :label="tag"></el-checkbox-button>
  93. </el-checkbox-group>
  94. <div id="tags"></div>
  95. </div>
  96. <div style="margin-top: 180px"></div>
  97. <div v-for="item in contentList">
  98. <el-row style="margin: 10px; padding: 10px" class="border">
  99. <el-col :span="16" class="contentLine">
  100. <div style="display: flex">
  101. <el-date-picker
  102. v-model="item.created_ts"
  103. type="datetime"
  104. readonly="true"
  105. placeholder="选择日期时间">
  106. </el-date-picker>
  107. <div style="margin-left: 20px">
  108. <el-checkbox-button v-for="tag in commonTags" :label="tag" :key="item"
  109. @change="checked=>checkChange(checked,item,tag)"
  110. :checked="isChooseTag(item,tag)">{{tag}}
  111. </el-checkbox-button>
  112. </div>
  113. </div>
  114. <div class="myText" @dblclick="edit(item)">
  115. <el-input v-model="item.content" type="textarea" :autosize="{minRows: 1}" readonly="true"></el-input>
  116. </div>
  117. <div>
  118. <el-image v-for="img in resourceMap[item.id]"
  119. :src="'https://memos_assert.tianyunperfect.cn/' + img['internal_path'] + '?width=150'"
  120. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + img['internal_path']]"
  121. style="height: 200px; margin: 10px 10px 0 0; cursor: pointer">
  122. </el-image>
  123. </div>
  124. </el-col>
  125. <el-col :span="4" style="display: flex">
  126. <div class="editClass">
  127. <el-button size="small" @click="window.open('https://memos.tianyunperfect.cn/m/' + item.resource_name)">跳转</el-button>
  128. <br>
  129. <el-button size="small" @click="edit(item)">编辑</el-button>
  130. </div>
  131. <div class="editClass">
  132. <el-button size="small" @click="archive(item)">归档</el-button>
  133. <br>
  134. <el-button size="small" type="warning" @click="del(item)">删除</el-button>
  135. </div>
  136. </el-col>
  137. </el-row>
  138. </div>
  139. </div>
  140. <el-dialog
  141. title="提示"
  142. :visible.sync="dialogVisible"
  143. width="60%"
  144. >
  145. <div v-if="tmpItem.id" class="contentLine">
  146. <div>
  147. <el-date-picker
  148. v-model="tmpItem.created_ts"
  149. type="datetime"
  150. value-format="yyyy-MM-ddTHH:mm:ss"
  151. placeholder="选择日期时间">
  152. </el-date-picker>
  153. </div>
  154. <div>
  155. <el-input ref="contentEdit" v-model="tmpItem.content" type="textarea" :autosize="{minRows: 2}"></el-input>
  156. </div>
  157. <div style="display: flex">
  158. <div v-for="img in cloneFileIds">
  159. <el-image :src="'https://memos_assert.tianyunperfect.cn/' + img['internal_path'] + '?width=150'"
  160. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + img['internal_path']]"
  161. style="height: 200px; margin: 10px 10px 0 0; cursor: pointer">
  162. </el-image>
  163. </div>
  164. </div>
  165. </div>
  166. <span slot="footer" class="dialog-footer">
  167. <el-button @click="dialogVisible = false">取 消</el-button>
  168. <el-button type="primary" @click="confirmSubmit">确 定</el-button>
  169. </span>
  170. </el-dialog>
  171. </div>
  172. <!-- 引入Quill库 -->
  173. <!--<script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>-->
  174. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  175. <script>
  176. let authStr = "bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E";
  177. let myHeaders = {
  178. 'Content-type': 'application/json',
  179. 'Authorization': authStr
  180. };
  181. function getRandomId() {
  182. return Math.random().toString(36).substr(2);
  183. }
  184. let vm = new Vue({
  185. el: '#app',
  186. data: {
  187. tmpItem: {},
  188. dialogVisible: false,
  189. page: 1,
  190. size: 20,
  191. searchStr: '',
  192. contentStr: '',
  193. tagChecked: [],
  194. allTags: [],
  195. contentList: [],
  196. sendBtnAble: true,
  197. resourceMap: {}, // 资源map,
  198. commonTags: ['todo', '日记', '梦记', '美食', '备份', '歌曲', '电影', '随笔', '社会', '人生'],
  199. upload_url: 'https://memos.tianyunperfect.cn/api/v1/resource/blob',
  200. tmpFileIds: [],
  201. cloneFileIds:[]
  202. },
  203. watch: {
  204. tagChecked: function () {
  205. localStorage.setItem('tags', JSON.stringify(this.tagChecked));
  206. this.search();
  207. },
  208. page: function () {
  209. this.search();
  210. },
  211. size: function () {
  212. this.search();
  213. }
  214. },
  215. mounted() {
  216. // 初始化contentStr,如果本地有缓存
  217. let item = localStorage.getItem('contentStr');
  218. if (item) {
  219. this.contentStr = item;
  220. }
  221. this.getAllTags();
  222. this.search();
  223. },
  224. methods: {
  225. getCloneResourceIds(id) {
  226. let resourceMapElement = this.resourceMap[id];
  227. if (resourceMapElement) {
  228. return JSON.parse(JSON.stringify(resourceMapElement))
  229. }
  230. return []
  231. },
  232. uploadFile(url, file, headers = {}) {
  233. return new Promise((resolve, reject) => {
  234. const xhr = new XMLHttpRequest();
  235. xhr.open('POST', url, true); // 使用异步的方式
  236. const formData = new FormData();
  237. formData.append('file', file);
  238. for (const [key, value] of Object.entries(headers)) {
  239. xhr.setRequestHeader(key, value);
  240. }
  241. xhr.onload = function () {
  242. if (xhr.status === 200) {
  243. resolve(JSON.parse(xhr.responseText));
  244. } else {
  245. reject(new Error(xhr.statusText));
  246. }
  247. };
  248. xhr.onerror = function () {
  249. reject(new Error("Network Error"));
  250. };
  251. xhr.send(formData);
  252. });
  253. },
  254. getFileFromPaste(e) {
  255. let clipboardData = e.clipboardData; // IE
  256. if (!clipboardData) {
  257. //chrome
  258. clipboardData = e.originalEvent.clipboardData;
  259. }
  260. let items = '';
  261. items = (e.clipboardData || window.clipboardData).items;
  262. let file = null;
  263. if (!items || items.length === 0) {
  264. this.$message.error('当前浏览器不支持粘贴本地图片,请打开图片复制后再粘贴!');
  265. return;
  266. }
  267. // 搜索剪切板items
  268. for (let i = 0; i < items.length; i++) {
  269. // 限制上传文件类型
  270. if (items[i].type.indexOf('image') !== -1) {
  271. file = items[i].getAsFile();
  272. break;
  273. }
  274. }
  275. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  276. if (file) {
  277. return file;
  278. }
  279. },
  280. handlePaste(e) {
  281. let file = this.getFileFromPaste(e);
  282. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  283. if (file) {
  284. this.uploadFile(this.upload_url, file, {'Authorization': myHeaders['Authorization']}).then(res => {
  285. let fileId = res['id'];
  286. if (fileId) {
  287. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resourceById", 'post', {ids: fileId}, {});
  288. let sourceRe = sourceRes['res'];
  289. this.tmpFileIds.push(...sourceRe)
  290. }
  291. });
  292. }
  293. },
  294. keyDown() {
  295. // 如果是ctrl + enter \ meta + enter 则发送
  296. if (event.ctrlKey || event.metaKey) {
  297. this.sendData();
  298. }
  299. },
  300. msg(content) {
  301. this.$message({
  302. message: content,
  303. type: 'success'
  304. });
  305. },
  306. isChooseTag(item, tag) {
  307. return item.content.indexOf("#" + tag + " ") !== -1;
  308. },
  309. checkChange(val, item, tag) {
  310. if (val) {
  311. this.addTag(item, tag);
  312. } else {
  313. this.delTag(item, tag);
  314. }
  315. },
  316. addTag(item, cTag) {
  317. let content = item.content;
  318. // 如果已经有了,就不添加
  319. if (content.indexOf(cTag) !== -1) {
  320. return;
  321. }
  322. item.content = '#' + cTag + ' ' + content;
  323. // 提交记录
  324. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  325. id: item.id,
  326. content: item.content,
  327. created_ts: item.created_ts
  328. }, {});
  329. if (res.code === 200) {
  330. this.msg('更新成功');
  331. // this.search();
  332. }
  333. },
  334. delTag(item, cTag) {
  335. let content = item.content;
  336. // 如果没有,就不删除
  337. if (content.indexOf(cTag) === -1) {
  338. return;
  339. }
  340. item.content = content.replace('#' + cTag + " ", '');
  341. // 提交记录
  342. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  343. id: item.id,
  344. content: item.content,
  345. created_ts: item.created_ts
  346. }, {});
  347. if (res.code === 200) {
  348. this.msg('更新成功');
  349. // this.search();
  350. }
  351. },
  352. del(item) {
  353. // 提醒是否删除
  354. this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(() => {
  355. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/delete`, 'post', {id: item.id}, {}).then(res => {
  356. if (res.code === 200) {
  357. this.msg('删除成功');
  358. // 在 this.contentList 上直接去除
  359. this.contentList = this.contentList.filter(it => it.id !== item.id);
  360. }
  361. });
  362. })
  363. },
  364. archive(item) {
  365. requestUtil.async(`https://web_history.tianyunperfect.cn/memos/archive`, 'post', {id: item.id}, {}).then(res => {
  366. this.msg('归档成功');
  367. this.search();
  368. });
  369. },
  370. handleClose(done) {
  371. this.dialogVisible = false;
  372. done();
  373. },
  374. confirmSubmit(done) {
  375. // 提交记录
  376. let res = requestUtil.sync('https://web_history.tianyunperfect.cn/memos/update', 'post', {
  377. id: this.tmpItem.id,
  378. content: this.tmpItem.content,
  379. created_ts: this.tmpItem.created_ts
  380. }, {});
  381. if (res.code === 200) {
  382. this.msg('更新成功');
  383. // this.search();
  384. }
  385. this.dialogVisible = false;
  386. done();
  387. },
  388. edit(item) {
  389. this.tmpItem = item;
  390. this.cloneFileIds=this.getCloneResourceIds(item.id)
  391. this.dialogVisible = true;
  392. // 获取焦点
  393. this.$nextTick(() => {
  394. this.$refs.contentEdit.focus()
  395. })
  396. },
  397. isDisabled(item) {
  398. // 默认不可编辑,这个时候 没有disable属性
  399. return item['disable'] !== false;
  400. },
  401. sendData: function () {
  402. this.sendBtnAble = false;
  403. let content = this.contentStr;
  404. let categories = this.tagChecked;
  405. // 循环拼接类似 #tag #tag2 content
  406. let updatedContent = content;
  407. if (categories.length > 0) {
  408. // 循环拼接类似 #tag #tag2 content
  409. updatedContent = categories.map(tag => `#${tag}`).join(' ') + ' ' + content;
  410. }
  411. // updatedContent 去除尾部的\n,可能有多个
  412. updatedContent = updatedContent.replace(/\n+$/, '');
  413. let res = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/memo', 'post', {content: updatedContent, "resourceIdList": this.tmpFileIds.map(it => it.id)}, myHeaders);
  414. if (res['name']) {
  415. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  416. this.msg('记录成功');
  417. localStorage.setItem('contentStr', '');
  418. this.contentStr = '';
  419. this.tmpFileIds = []
  420. this.search();
  421. }
  422. this.sendBtnAble = true;
  423. },
  424. search: function () {
  425. document.documentElement.scrollTo(0, 0);
  426. this.resourceMap = {};
  427. let params = {'search': this.searchStr, tag_str: this.tagChecked.join(','), page: this.page, page_size: this.size};
  428. let url = 'https://web_history.tianyunperfect.cn/memos/list';
  429. let urlWithParams = requestUtil.buildUrl(url, params);
  430. let res = requestUtil.sync(urlWithParams, 'get', null, myHeaders);
  431. if (res.code !== 200) {
  432. return;
  433. }
  434. this.contentList = res.res;
  435. // contentList 循环获取里面所有的id数组
  436. let idArr = this.contentList.map(item => item['id']);
  437. // 获取资源
  438. let sourceRes = requestUtil.sync("https://web_history.tianyunperfect.cn/memos/resource", 'post', {ids: idArr.join(',')}, {});
  439. // {'code': 200, 'res': [{'memo_id': 1, 'resource_name': 'EfdmvRsodBviDQWRmDiTaV'}, {'memo_id': 4, 'resource_name': '2rY5kmn3AHsy23vK4vSskV'}, {'memo_id': 7, 'resource_name': 'FMhzJXjoTa7fdtbbCBjXnt'}]}
  440. // 循环遍历,以memo_id为key,resource_name数组为value
  441. sourceRes['res'].forEach(item => {
  442. if (this.resourceMap[item['memo_id']]) {
  443. this.resourceMap[item['memo_id']].push(item);
  444. } else {
  445. this.resourceMap[item['memo_id']] = [item];
  446. }
  447. });
  448. },
  449. getAllTags: function () {
  450. this.allTags = requestUtil.sync('https://memos.tianyunperfect.cn/api/v1/tag', 'get', null, myHeaders);
  451. let item1 = localStorage.getItem('tags'); // 获取本地存储的标签
  452. if (item1) {
  453. this.tagChecked = JSON.parse(item1);
  454. }
  455. }
  456. }
  457. });
  458. // 如果处于编辑状态,则执行esc取消
  459. window.onkeydown = function (event) {
  460. let edit = vm.dialogVisible;
  461. if (!edit) {
  462. return;
  463. }
  464. // esc
  465. if (event.keyCode === 27) {
  466. vm.dialogVisible = false;
  467. }
  468. // 如果是meta + enter 则保存
  469. if (event.metaKey && event.keyCode === 13) {
  470. vm.confirmSubmit();
  471. }
  472. // 如果是ctrl + enter 则保存
  473. if (event.ctrlKey && event.keyCode === 13) {
  474. vm.confirmSubmit();
  475. }
  476. };
  477. </script>
  478. </body>
  479. </html>