send_memos.html 24 KB

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