From df29e3e1d58d36ce9f5a5cfc06d53cb159eeb5d9 Mon Sep 17 00:00:00 2001 From: HelloWorld Date: Mon, 13 Apr 2020 10:22:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=80=E4=B8=8B=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- www/scripts/controllers/advice-controller.js | 32 +++--- .../controllers/bookmarks-controller.js | 1 + www/scripts/controllers/home-controller.js | 17 +-- www/scripts/controllers/login-controller.js | 12 +- www/scripts/controllers/menus-controller.js | 106 ++++++++---------- www/scripts/controllers/note-controller.js | 6 +- www/scripts/controllers/search-controller.js | 11 +- .../controllers/settings-controller.js | 14 +-- www/scripts/controllers/tags-controller.js | 18 +-- .../controllers/weixin-article-controller.js | 14 +-- www/scripts/directives/js-init-directive.js | 25 ----- www/scripts/services/data-service.js | 2 - www/views/advice.html | 5 +- www/views/menus.html | 45 ++++---- www/views/tags.html | 79 +++++++------ 16 files changed, 149 insertions(+), 242 deletions(-) diff --git a/README.md b/README.md index d9bf957..5f22c05 100644 --- a/README.md +++ b/README.md @@ -116,11 +116,11 @@ source /home/lcq/schema.sql; // 执行schema.sql文件创建数据库表格。 8、在浏览器里面输入:127.0.0.1:2000。 9、如果需要域名部署的话,推荐使用nginx作为HTTP和反向代理服务器,根目录有一份`nginx.conf`文件,你只需要更新`root`项即可使用。相关知识,请自行百度。 -8 其他说明 +7 其他说明 --------- 1、我没有做浏览器兼容测试,只在Google Chrome下面进行了测试开发。 -9 开源许可证 +8 开源许可证 ----------- [MIT License](http://www.opensource.org/licenses/MIT) 你可以随意使用此项目,无需通知我,因为我可能很忙没时间。注意,手机版当前没开源 diff --git a/www/scripts/controllers/advice-controller.js b/www/scripts/controllers/advice-controller.js index b1da3d1..89d02f3 100644 --- a/www/scripts/controllers/advice-controller.js +++ b/www/scripts/controllers/advice-controller.js @@ -4,44 +4,40 @@ app.controller('adviceCtr', ['$scope', '$state', '$timeout', 'pubSubService', 'd $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); $scope.comment = ''; $scope.advices = []; $scope.user = {}; + $scope.loading = false; - get('user').then(user => { + pubSubService.subscribe('Common.user', $scope, function (event, user) { $scope.user = user; - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexAdvice - }); }); + getAdvices(); $scope.ok = async function () { if ($scope.comment == '') { toastr.error('留言失败内容不能为空', "错误"); - return; - } - if ($scope.user.username == 'test') { + } else if ($scope.user.username == 'test') { toastr.error('test用户不允许留言!', "错误"); - return; + } else { + await post('adviceAdd', { comment: $scope.comment }); + await getAdvices(); } - - await post('adviceAdd', { - comment: $scope.comment, - }); - await getAdvices(); } async function getAdvices() { - let data = await get("advices"); - data.forEach(element => { + $scope.loading = true; + $scope.comment = ""; + let advices = await get("advices"); + advices.forEach(element => { element.imgData = new Identicon(md5(element.userId)).toString(); }); - $scope.comment = ""; $timeout(function () { - $scope.advices = data; + $scope.advices = advices; }); + $scope.loading = false; } }]); diff --git a/www/scripts/controllers/bookmarks-controller.js b/www/scripts/controllers/bookmarks-controller.js index 7a9116d..86e583a 100644 --- a/www/scripts/controllers/bookmarks-controller.js +++ b/www/scripts/controllers/bookmarks-controller.js @@ -4,4 +4,5 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); }]); diff --git a/www/scripts/controllers/home-controller.js b/www/scripts/controllers/home-controller.js index 34cdc4d..2200713 100644 --- a/www/scripts/controllers/home-controller.js +++ b/www/scripts/controllers/home-controller.js @@ -4,18 +4,9 @@ app.controller('homeCtr', ['$scope', '$stateParams', '$filter', '$state', '$wind $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); - (async () => { - try { - await get('user'); - pubSubService.publish('loginCtr.login', { 'login': true }); - $state.go('tags'); - } catch (error) { - pubSubService.publish('Common.menuActive', { - login: false, - index: dataService.NotLoginIndexHome - }); - } - })(); - + pubSubService.subscribe('Common.user', $scope, function (event, user) { + user.id && $state.go('tags'); + }); }]); diff --git a/www/scripts/controllers/login-controller.js b/www/scripts/controllers/login-controller.js index 10b09d9..69a43ff 100644 --- a/www/scripts/controllers/login-controller.js +++ b/www/scripts/controllers/login-controller.js @@ -1,13 +1,13 @@ app.controller('loginCtr', ['$scope', '$filter', '$state', '$http', '$cookieStore', '$window', 'pubSubService', 'dataService', function ($scope, $filter, $state, $http, $cookieStore, $window, pubSubService, dataService) { - console.log("Hello loginCtr...", $cookieStore.get("username")); + console.log("Hello loginCtr..."); if (dataService.smallDevice()) { $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); - pubSubService.publish('Common.menuActive', { - login: false, - index: dataService.NotLoginIndexLogin + pubSubService.subscribe('Common.user', $scope, function (event, user) { + user.id && $state.go('tags'); }); $scope.username = $cookieStore.get("username") || ""; @@ -39,12 +39,12 @@ app.controller('loginCtr', ['$scope', '$filter', '$state', '$http', '$cookieStor $cookieStore.put("username", $scope.username); let data = await post('userLogin', params); + pubSubService.publish('Login', true); // 更新token信息 localStorage.setItem("authorization", data.token); - pubSubService.publish('loginCtr.login', { login: true }); - $state.go('tags') + $state.go('tags'); } $scope.showRegister = async function () { diff --git a/www/scripts/controllers/menus-controller.js b/www/scripts/controllers/menus-controller.js index 76092ae..b620883 100644 --- a/www/scripts/controllers/menus-controller.js +++ b/www/scripts/controllers/menus-controller.js @@ -1,43 +1,48 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$timeout', '$document', 'pubSubService', 'dataService', function ($scope, $stateParams, $state, $window, $timeout, $document, pubSubService, dataService) { console.log("Hello menuCtr") - $scope.login = false; /**< 是否登陆 */ - $scope.selectLoginIndex = 0; /**< 默认登陆之后的选择的菜单索引,下表从 0 开始 */ - $scope.selectNotLoginIndex = 0; /**< 默认未登陆之后的选择的菜单索引,下表从 0 开始 */ - $scope.keyword = ''; /**< 搜索关键字 */ + $scope.login = false; + $scope.selectLoginIndex = 0; // 默认登陆之后的选择的菜单索引,下表从 0 开始 + $scope.selectNotLoginIndex = 0; // 默认未登陆之后的选择的菜单索引,下表从 0 开始 + $scope.keyword = ''; $scope.searchHistory = []; $scope.historyTypes = dataService.historyTypes; $scope.quickUrl = {}; $scope.longPress = false; $scope.user = {}; + $scope.loaded = false; // 是否加载完毕 - // 防止在登陆的情况下,在浏览器里面直接输入url,这时候要更新菜单选项 - pubSubService.subscribe('Common.menuActive', $scope, function (event, params) { - console.log("subscribe Common.menuActive, login = " + params.login + ", index = " + params.index); - $scope.login = (params && params.login) || false; - var index = $scope.login ? ($scope.selectLoginIndex = (params && params.index) || 0) : ($scope.selectNotLoginIndex = (params && params.index) || 0); - updateMenuActive(index); + pubSubService.subscribe('Login', $scope, function (event, login) { + $scope.login = login; }); - pubSubService.subscribe('Settings.quickUrl', $scope, function (event, params) { - $scope.quickUrl = params.quickUrl; + function sleep(time) { return new Promise((resolve) => setTimeout(resolve, time)); } + + pubSubService.subscribe('Menus.active', $scope, async function () { + while (!$scope.loaded) { await sleep(10); } + updateMenuActive(); }); $scope.loginMenus = dataService.loginMenus; // 登陆之后显示的菜单数据。uiSerf:内部跳转链接。 $scope.notLoginMenus = dataService.notLoginMenus; // 未登陆显示的菜单数据 - get('user', { full: true }).then(user => { - $timeout(() => { - $scope.user = user; - $scope.searchHistory = JSON.parse(user.searchHistory || '[]'); - $scope.quickUrl = JSON.parse(user.quickUrl || '{}'); - $scope.searchHistory.forEach((item, index) => { - $scope.searchIcon(item); - }) - // if ($scope.user.username === 'lcq') { - // $scope.loginMenus[dataService.LoginIndexHot].show = false; - // } + (async () => { + $timeout(async () => { + try { + let user = await get('user', { full: true }); + $scope.login = true; + $scope.user = user; + $scope.searchHistory = JSON.parse(user.searchHistory || '[]'); + $scope.quickUrl = JSON.parse(user.quickUrl || '{}'); + for (const item of $scope.searchHistory) { + $scope.searchIcon(item); + } + } catch (error) { + $scope.login = false; + } + pubSubService.publish('Common.user', $scope.user); + $scope.loaded = true; }) - }); + })() $scope.toggleReady = function (ready) { if (ready) { @@ -84,7 +89,6 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time }, { reload: true, }) - updateMenuActive($scope.selectLoginIndex = 0); } else if (searchOption == 1) { $window.open('https://www.google.com.hk/#newwindow=1&safe=strict&q=' + encodeURIComponent(keyword), '_blank'); } else if (searchOption == 2) { @@ -95,12 +99,7 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time $window.open('http://www.baidu.com/s?tn=mybookmark.cn&ch=3&ie=utf-8&wd=' + encodeURIComponent(keyword), '_blank'); } else if (searchOption == 5) { console.log('search note, word = ', keyword); - $state.go('note', { - keyword: keyword, - }, { - reload: true, - }) - updateMenuActive($scope.selectLoginIndex = dataService.LoginIndexNote); + $state.go('note', { keyword }, { reload: true }) } if (!keyword) { @@ -186,29 +185,31 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time $window.open(url, '_blank'); } - $scope.showUpdate = function () { - $state.go('settings', { - formIndex: 5, - }); - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexSettings - }); - } - $scope.coffee = function () { $state.go('settings', { formIndex: 6, }); - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexSettings - }); } - function updateMenuActive(index) { + async function updateMenuActive() { + await sleep(10); // 阻塞 10ms 是因为 document.location.hash 还没切换过来等待切换 + let mapIndex = { + "#/bookmarks": dataService.LoginIndexBookmarks, + "#/tags": dataService.LoginIndexTags, + "#/note": dataService.LoginIndexNote, + "#/weixin-article": $scope.login ? dataService.LoginIndexHot : dataService.NotLoginIndexHot, + "#/settings": dataService.LoginIndexSettings, + "#/advice": dataService.LoginIndexAdvice, + "#/": $scope.login ? dataService.LoginIndexBookmarks : dataService.NotLoginIndexHome, + "#/login": dataService.NotLoginIndexLogin, + } + + console.log('updateMenuActive', $scope.login, document.location.hash, mapIndex[document.location.hash]); + + $scope.selectLoginIndex = mapIndex[document.location.hash]; + $scope.selectNotLoginIndex = mapIndex[document.location.hash]; $('.ui.menu a.item').removeClass('selected'); - $('.ui.menu a.item:eq(' + index + ')').addClass('selected'); + $('.ui.menu a.item:eq(' + mapIndex[document.location.hash] + ')').addClass('selected'); } async function saveHistory() { @@ -249,7 +250,6 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time // 全局处理添加备忘录 if (key == 'A') { if ($scope.selectLoginIndex !== dataService.LoginIndexNote) { - updateMenuActive($scope.selectLoginIndex = dataService.LoginIndexNote); $state.go('note', { key: key }, { reload: true }) } return; @@ -269,10 +269,6 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time } if (key == ',' || key == '.' || key == '/') { - pubSubService.publish('Common.menuActive', { - login: $scope.login, - index: dataService.LoginIndexTags - }); var stateParams = { tagId: -1, orderIndex: (key == ',' ? 0 : (key == '.' ? 1 : 2)), @@ -283,11 +279,7 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time // 数字键用来切换菜单 if (!isNaN(key)) { var num = parseInt(key); - if (num < 0 || num > 6) return; - pubSubService.publish('Common.menuActive', { - login: $scope.login, - index: num - 1 - }); + if (num < 0 || num > 6 || (!$scope.login)) return; $state.go(dataService.loginMenus[num - 1].uiSref, {}, { reload: true, }) diff --git a/www/scripts/controllers/note-controller.js b/www/scripts/controllers/note-controller.js index 09cad54..b965235 100644 --- a/www/scripts/controllers/note-controller.js +++ b/www/scripts/controllers/note-controller.js @@ -4,6 +4,7 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); var dialog = null; $scope.hoverNote = null; @@ -25,11 +26,6 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind var timeagoInstance = timeago(); - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexNote - }); - getTags(); $scope.changeCurrentPage = function (currentPage) { diff --git a/www/scripts/controllers/search-controller.js b/www/scripts/controllers/search-controller.js index 4c70079..b289e16 100644 --- a/www/scripts/controllers/search-controller.js +++ b/www/scripts/controllers/search-controller.js @@ -4,6 +4,7 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); $scope.hoverBookmark = null; $scope.bookmarks = []; // 书签数据 @@ -36,15 +37,11 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi $scope.search(); } } + pubSubService.subscribe('Common.user', $scope, function (event, user) { + $scope.user = user; + }); get('tags').then((tags) => $scope.tags = tags) - get('user').then((user) => $scope.user = user) - - // 默认登陆 - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexBookmarks - }); $scope.jumpToUrl = async function (url, id) { if (!$scope.edit) { diff --git a/www/scripts/controllers/settings-controller.js b/www/scripts/controllers/settings-controller.js index 5a7fa9e..152c135 100644 --- a/www/scripts/controllers/settings-controller.js +++ b/www/scripts/controllers/settings-controller.js @@ -4,6 +4,7 @@ app.controller('settingsCtr', ['$scope', '$stateParams', '$filter', '$state', '$ $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); $scope.forbidQuickKey = dataService.forbidQuickKey $scope.form = [false, false, false, false, false, false, false]; @@ -50,10 +51,6 @@ app.controller('settingsCtr', ['$scope', '$stateParams', '$filter', '$state', '$ await post('userLogout'); localStorage.setItem("authorization", ""); - pubSubService.publish('Common.menuActive', { - login: false, - index: dataService.NotLoginIndexLogin - }); $state.go('login', {}) } else { toastr.error('新密码两次输入不一致', "错误"); @@ -135,10 +132,6 @@ app.controller('settingsCtr', ['$scope', '$stateParams', '$filter', '$state', '$ console.log(files, response); if (response.code == 0) { setTimeout(function () { - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexBookmarks - }); $state.go('tags', {}) }, 3000); toastr.success(response.msg, "提示"); @@ -150,11 +143,6 @@ app.controller('settingsCtr', ['$scope', '$stateParams', '$filter', '$state', '$ $(".ui.pointing.menu .item").removeClass("selected"); }, 500); - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexSettings - }); - async function saveQuickUrl() { await post("userUpdate", { quickUrl: JSON.stringify($scope.quickUrl) }); toastr.success('全局快捷键更新成功', "提示"); diff --git a/www/scripts/controllers/tags-controller.js b/www/scripts/controllers/tags-controller.js index aa7e29e..dd2a737 100644 --- a/www/scripts/controllers/tags-controller.js +++ b/www/scripts/controllers/tags-controller.js @@ -4,6 +4,7 @@ app.controller('tagsCtr', ['$scope', '$filter', '$state', '$window', '$statePara $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); (async () => { await getTags(); @@ -30,11 +31,6 @@ app.controller('tagsCtr', ['$scope', '$filter', '$state', '$window', '$statePara $scope.waitDelBookmark = {}; $scope.bookmarkNormalHover = false; - pubSubService.subscribe('MenuCtr.tags', $scope, function (event, data) { - console.log('subscribe MenuCtr.tags', data); - getTags(); - }); - $scope.getBookmarks = async function (tagId, page, showType) { console.log(tagId, page, showType); @@ -86,11 +82,6 @@ app.controller('tagsCtr', ['$scope', '$filter', '$state', '$window', '$statePara }, 10); } - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexTags - }); - $timeout(function () { dataService.transition('#' + addBookmarkId, { duration: 1000, @@ -365,7 +356,7 @@ app.controller('tagsCtr', ['$scope', '$filter', '$state', '$window', '$statePara id: -1, bookmarkCount: '...', bookmarkClicked: false, - name: '个人定制', + name: '全部', show: 1 }) @@ -384,11 +375,6 @@ app.controller('tagsCtr', ['$scope', '$filter', '$state', '$window', '$statePara tags[0].bookmarkClicked = true; } - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexTags - }); - $timeout(() => { $scope.loading = false; $scope.tags = tags; diff --git a/www/scripts/controllers/weixin-article-controller.js b/www/scripts/controllers/weixin-article-controller.js index 6b97919..b7a411e 100644 --- a/www/scripts/controllers/weixin-article-controller.js +++ b/www/scripts/controllers/weixin-article-controller.js @@ -4,6 +4,7 @@ app.controller('weixinArticleCtr', ['$scope', '$state', '$sce', '$filter', '$win $window.location = "http://m.mybookmark.cn/#/tags"; return; } + pubSubService.publish('Menus.active'); $scope.hoverBookmark = null; $scope.bookmarks = []; // 书签数据 @@ -24,13 +25,6 @@ app.controller('weixinArticleCtr', ['$scope', '$state', '$sce', '$filter', '$win $scope.channels = JSON.parse(`[{"id":1,"name":"热门", "clicked": true},{"id":2,"name":"搞笑"},{"id":3,"name":"养生堂"},{"id":4,"name":"私房话"},{"id":5,"name":"八卦精"},{"id":6,"name":"科技咖"},{"id":7,"name":"财经迷"},{"id":8,"name":"汽车控"},{"id":9,"name":"生活家"},{"id":10,"name":"时尚圈"},{"id":11,"name":"育儿"},{"id":12,"name":"旅游"},{"id":13,"name":"职场"},{"id":14,"name":"美食"},{"id":15,"name":"历史"},{"id":16,"name":"教育"},{"id":17,"name":"星座"},{"id":18,"name":"体育"},{"id":19,"name":"军事"},{"id":20,"name":"游戏"},{"id":21,"name":"萌宠"}]`); var timeagoInstance = timeago(); - get('user').then((user) => { - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexHot - }); - }) - $scope.jumpToUrl = async function (url) { $window.open(url, '_blank'); } @@ -116,12 +110,6 @@ app.controller('weixinArticleCtr', ['$scope', '$state', '$sce', '$filter', '$win $scope.getWeixinArticles = async function (channelId, page) { var menusScope = $('div[ng-controller="menuCtr"]').scope(); - var login = (menusScope && menusScope.login) || false; - var index = login ? dataService.LoginIndexHot : dataService.NotLoginIndexHot; - pubSubService.publish('Common.menuActive', { - login: login, - index: index - }); $scope.bookmarks = [] $scope.bookmark = {} $scope.loadBusy = true; diff --git a/www/scripts/directives/js-init-directive.js b/www/scripts/directives/js-init-directive.js index 4ab1978..4a4f500 100644 --- a/www/scripts/directives/js-init-directive.js +++ b/www/scripts/directives/js-init-directive.js @@ -150,34 +150,9 @@ app.directive('jsMenuInit', function ($compile) { link: function ($scope, $element, $attrs) { if ($scope.$last === true) { console.log('jsMenuInit......') - $('.js-bookmark-dropdown').dropdown({ - action: 'hide', - on: 'hover', - }); - - $('.js-bookmark-dropdown .ui.checkbox').checkbox(); - $('.ui.checkbox.js-radio-navigate').checkbox('check'); $('.ui.menu a.item').on('click', function () { $(this).addClass('selected').siblings().removeClass('selected'); }); - - $(".ui.menu a.item:first").hover( - function () { - $('.js-bookmark-dropdown').dropdown('show'); - }, - function () { - setTimeout(() => { - if ($('.js-menu-option:hover').length === 0) { - $('.js-bookmark-dropdown').dropdown('hide'); - } - }, 100) - } - ); - - $('.ui.menu a.item').on('click', function () { - $(this).addClass('selected').siblings().removeClass('selected'); - }); - $('.search-item').popup({ on: 'focus', inline: true diff --git a/www/scripts/services/data-service.js b/www/scripts/services/data-service.js index 5002a7c..51058cc 100644 --- a/www/scripts/services/data-service.js +++ b/www/scripts/services/data-service.js @@ -7,13 +7,11 @@ app.factory('dataService', [function () { LoginIndexHot: 3, LoginIndexSettings: 4, LoginIndexAdvice: 5, - LoginIndexPraise: 6, // 非登陆索引 NotLoginIndexHome: 0, NotLoginIndexLogin: 1, NotLoginIndexHot: 2, - NotLoginIndexPraise: 3, loginMenus: [{ uiSref: 'bookmarks', diff --git a/www/views/advice.html b/www/views/advice.html index fc6daf8..10f19d7 100644 --- a/www/views/advice.html +++ b/www/views/advice.html @@ -11,7 +11,7 @@
-
+

最新100条留言

+
+ 正在加载中... +
diff --git a/www/views/menus.html b/www/views/menus.html index df9fe86..028b8bd 100644 --- a/www/views/menus.html +++ b/www/views/menus.html @@ -1,56 +1,53 @@