完成设置的重置密码功能

This commit is contained in:
luchenqun 2017-02-04 21:18:36 +08:00
parent 79cfdc9cc0
commit 9f2d396199
10 changed files with 171 additions and 41 deletions

View File

@ -179,6 +179,20 @@ db.updateUserLastLogin = function(id) {
}); });
}; };
db.resetPassword = function(userId, password) {
console.log('updateUserLastLogin');
var sql = "UPDATE `users` SET `password` = '" + password + "' WHERE(`id` = '" + userId + "')";
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result.affectedRows);
}
});
});
}
db.register = function(user) { db.register = function(user) {
console.log('register'); console.log('register');
var sql = "INSERT INTO `users` (`username`, `password`, `email`) VALUES ('" + user.username + "', '" + user.password + "', '" + user.email + "')"; var sql = "INSERT INTO `users` (`username`, `password`, `email`) VALUES ('" + user.username + "', '" + user.password + "', '" + user.email + "')";

View File

@ -40,6 +40,7 @@
<script src="/scripts/directives/js-init-directive.js"></script> <script src="/scripts/directives/js-init-directive.js"></script>
<script src="/scripts/controllers/bookmarks-controller.js"></script> <script src="/scripts/controllers/bookmarks-controller.js"></script>
<script src="/scripts/controllers/home-controller.js"></script> <script src="/scripts/controllers/home-controller.js"></script>
<script src="/scripts/controllers/settings-controller.js"></script>
<script src="/scripts/controllers/login-controller.js"></script> <script src="/scripts/controllers/login-controller.js"></script>
<script src="/scripts/controllers/tags-controller.js"></script> <script src="/scripts/controllers/tags-controller.js"></script>
<script src="/scripts/controllers/menus-controller.js"></script> <script src="/scripts/controllers/menus-controller.js"></script>

View File

@ -37,6 +37,7 @@ app.config(function($stateProvider, $urlRouterProvider, $httpProvider) {
.state('settings', { .state('settings', {
url: '/settings', url: '/settings',
templateUrl: '/views/settings.html', templateUrl: '/views/settings.html',
controller: 'settingsCtr'
}) })
.state('login', { .state('login', {
url: '/login', url: '/login',

View File

@ -1,41 +1,18 @@
app.controller('homeCtr', ['$scope', '$stateParams', '$filter', '$state', '$window', 'bookmarkService', 'pubSubService', function($scope, $stateParams, $filter, $state, $window, bookmarkService, pubSubService) { app.controller('homeCtr', ['$scope', '$stateParams', '$filter', '$state', '$window', 'bookmarkService', 'pubSubService', function($scope, $stateParams, $filter, $state, $window, bookmarkService, pubSubService) {
console.log('Hello homeCtr......'); console.log('Hello homeCtr......');
var debug = false;
if (debug) {
var params = {
username: 'luchenqun',
password: 'fendoubuxi1',
autoLogin: true,
};
bookmarkService.login(params) bookmarkService.autoLogin()
.then((data) => { .then((data) => {
if (data.logined) { if (data.logined) {
pubSubService.publish('loginCtr.login', { pubSubService.publish('loginCtr.login', {
'login': data.logined, 'login': data.logined,
}); });
$state.go('bookmarks', { $state.go('bookmarks', {
showStyle: 'navigate', showStyle: 'navigate',
}) })
} else { } else {
console.log('login failed......................') console.log('autoLogin failed......................')
} }
}) })
.catch((err) => console.log('login err', err)); .catch((err) => console.log('autoLogin err', err));
} else {
bookmarkService.autoLogin()
.then((data) => {
if (data.logined || debug) {
pubSubService.publish('loginCtr.login', {
'login': debug || data.logined,
});
$state.go('bookmarks', {
showStyle: 'navigate',
})
} else {
console.log('autoLogin failed......................')
}
})
.catch((err) => console.log('autoLogin err', err));
}
}]); }]);

View File

@ -41,7 +41,7 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi
if ($scope.searchWord) { if ($scope.searchWord) {
searchBookmarks(searchParams); searchBookmarks(searchParams);
} else { } else {
$state.go('/', {})
} }
$scope.jumpToUrl = function(url, id) { $scope.jumpToUrl = function(url, id) {

View File

@ -0,0 +1,50 @@
app.controller('settingsCtr', ['$scope', '$stateParams', '$filter', '$state', '$window', 'bookmarkService', 'pubSubService', function($scope, $stateParams, $filter, $state, $window, bookmarkService, pubSubService) {
console.log('Hello settingsCtr......');
$scope.form = [true, false];
$scope.passwordOrgin = "";
$scope.passwordNew1 = "";
$scope.passwordNew2 = "";
$scope.changeForm = function(index) {
$scope.form = $scope.form.map(() => false);
$scope.form[index] = true;
}
$scope.resetPassword = function() {
if (!$scope.passwordOrgin || !$scope.passwordNew1 || !$scope.passwordNew2) {
toastr.error('原密码跟新密码不能为空', "错误");
return;
}
if ($scope.passwordNew1 == $scope.passwordNew2) {
var parmes = {
passwordNew: $scope.passwordNew1,
passwordOrgin: $scope.passwordOrgin,
};
bookmarkService.resetPassword(parmes)
.then((data) => {
if (data.retCode == 0) {
toastr.success('密码更新成功', "提示");
pubSubService.publish('Common.menuActive', {
login: false,
index: 0
});
$state.go('/', {});
} else {
toastr.error('密码更新失败。错误信息:' + data.msg, "错误");
}
})
.catch((err) => {
toastr.error('密码更新失败。错误信息:' + JSON.stringify(err), "错误");
});
} else {
toastr.error('新密码两次输入不一致', "错误");
}
}
pubSubService.publish('Common.menuActive', {
login: true,
index: 2
});
}]);

View File

@ -40,6 +40,19 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) {
}); });
return def.promise; return def.promise;
}, },
resetPassword: function(params) {
var def = $q.defer();
$http.post('/api/resetPassword/', {
params: params
})
.success(function(data) {
def.resolve(data);
})
.error(function(data) {
def.reject('register error');
});
return def.promise;
},
clickBookmark: function(params) { clickBookmark: function(params) {
var def = $q.defer(); var def = $q.defer();
$http.post('/api/clickBookmark/', { $http.post('/api/clickBookmark/', {

View File

@ -21,7 +21,7 @@
<img class="ui ui middle aligned tiny image" ng-src=" http://favicon.byi.pw/?url={{ bookmark.url }}" style="width:16px;height:16px" ng-if="!edit"> <img class="ui ui middle aligned tiny image" ng-src=" http://favicon.byi.pw/?url={{ bookmark.url }}" style="width:16px;height:16px" ng-if="!edit">
<span>{{ bookmark.title}}</span> <span>{{ bookmark.title}}</span>
</div> </div>
<div class="two wide column js-more-bookmark" ng-class="" ng-mouseover="" ng-mouseleave="" title="查看更多" ng-if="tag.bookmarks.length >= 4" ng-click="jumpToTags(tag.id)"> <div class="two wide column js-more-bookmark" ng-class="" ng-mouseover="" ng-mouseleave="" title="查看更多" ng-if="tag.bookmarks.length >= 31" ng-click="jumpToTags(tag.id)">
<img class="ui ui middle aligned tiny image" ng-src="./images/more.png" style="width:16px;height:16px"> <img class="ui ui middle aligned tiny image" ng-src="./images/more.png" style="width:16px;height:16px">
</div> </div>
</div> </div>

View File

@ -1 +1,37 @@
<p> 账号设置页面 </p> <div class="ui segment">
<div class="ui grid">
<div class="four wide stretched column">
<div class="ui secondary vertical pointing menu">
<a class="item" ng-class="{active:form[0]}" ng-click="changeForm(0)">重置密码
</a>
<a class="item" ng-class="{active:form[1]}" ng-click="changeForm(1)">书签显示设置
</a>
<a class="item" ng-class="{active:form[2]}" ng-click="changeForm(2)">菜单设置
</a>
</div>
</div>
<div class="twelve wide stretched column">
<form class="ui form" ng-show="form[0]">
<div class="required field">
<label>原密码</label>
<input type="password" placeholder="" ng-model="passwordOrgin">
</div>
<div class="required field">
<label>新密码</label>
<input type="password" placeholder="" ng-model="passwordNew1">
</div>
<div class="required field">
<label>确认密码</label>
<input type="password" placeholder="" ng-model="passwordNew2">
</div>
<button class="ui button" type="submit" ng-click="resetPassword()">重置密码</button>
</form>
<form class="ui form" ng-show="form[1]">
书签显示设置
</form>
<form class="ui form" ng-show="form[2]">
菜单设置
</form>
</div>
</div>
</div>

View File

@ -67,6 +67,44 @@ api.post('/register', function(req, res) {
}); });
}); });
api.post('/resetPassword', function(req, res) {
console.log("resetPassword");
if (!req.session.user) {
res.send(401);
return;
}
var params = req.body.params;
var passwordOrigin = md5(params.passwordOrgin); // 进行密码加密
var passwordNew = md5(params.passwordNew); // 进行密码加密
db.getUser(req.session.user.username)
.then((user) => {
if (user && user.password === passwordOrigin) {
return db.resetPassword(req.session.userId, passwordNew)
} else {
return Promise.resolve(0)
}
})
.then((affectedRows) => {
res.json({
retCode: (affectedRows == 1 ? 0 : 1),
msg: req.session.username + " 更新密码失败,可能原密码不正确!",
})
if (affectedRows) {
req.session.destroy();
}
})
.catch((err) => {
console.log('resetPassword error', err);
res.json({
retCode: 2,
msg: req.session.username + " 更新密码失败: " + JSON.stringify(err),
})
});
});
api.get('/autoLogin', function(req, res) { api.get('/autoLogin', function(req, res) {
var ret = { var ret = {
logined: false, logined: false,
@ -182,7 +220,7 @@ api.get('/bookmarks', function(req, res) {
tag.bookmarks = []; tag.bookmarks = [];
} }
tag.click += bookmark.click_count; tag.click += bookmark.click_count;
if (bookmark.id) { if (bookmark.id && tag.bookmarks.length < 31) {
tag.bookmarks.push(bookmark); tag.bookmarks.push(bookmark);
} }
}); });