send_memos.html 23 KB

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