send_memos.html 23 KB

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