完成登陆注册功能
This commit is contained in:
parent
a90cb5aff6
commit
7e78ded5b4
|
|
@ -41,7 +41,7 @@ exports.model = {
|
|||
encoding: 'utf8',
|
||||
host: '127.0.0.1',
|
||||
port: '3306',
|
||||
user: 'root',
|
||||
user: 'test',
|
||||
password: '123456',
|
||||
dateStrings: true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,59 @@
|
|||
const Base = require('./base.js');
|
||||
const crypto = require('crypto');
|
||||
|
||||
function md5(str) {
|
||||
return crypto.createHash('md5').update(str).digest('hex');
|
||||
};
|
||||
|
||||
module.exports = class extends Base {
|
||||
async __before() {
|
||||
const user = await this.session('user');
|
||||
console.log("session user", user);
|
||||
|
||||
//获取用户的 session 信息,如果为空,返回 false 阻止后续的行为继续执行
|
||||
// if (think.isEmpty(user)) {
|
||||
// return false;
|
||||
// }
|
||||
}
|
||||
|
||||
indexAction() {
|
||||
return this.display();
|
||||
}
|
||||
|
||||
async registerAction() {
|
||||
try {
|
||||
let data = this.post();
|
||||
let res = await this.model("users").add(data);
|
||||
let post = this.post();
|
||||
post.password = md5(post.password); // 进行密码加密
|
||||
|
||||
let res = await this.model("users").add(post);
|
||||
this.json({ code: 0, data: res, msg: "注册成功" });
|
||||
} catch (error) {
|
||||
this.json({ code: 1, data: '', msg: error.toString() });
|
||||
}
|
||||
}
|
||||
|
||||
async loginAction() {
|
||||
try {
|
||||
let post = this.post();
|
||||
post.password = md5(post.password); // 进行密码加密
|
||||
|
||||
let data = await this.model('users').where({ username: post.username, password: post.password }).find();
|
||||
if (think.isEmpty(data)) {
|
||||
this.json({ code: 2, msg: "账号或者密码错误" });
|
||||
} else {
|
||||
this.json({ code: 0, data, msg: "登陆成功" });
|
||||
data.password = "******";
|
||||
await this.session('user', data); // @todo 对session的maxAge进行操作(目前默认永久不过期)
|
||||
}
|
||||
} catch (error) {
|
||||
this.json({ code: 1, data: '', msg: error.toString() });
|
||||
}
|
||||
}
|
||||
|
||||
async userInfoAction() {
|
||||
this.json({ code: 1, data: '', msg: '' });
|
||||
}
|
||||
|
||||
autoLoginAction() {
|
||||
this.json({ "succ": true });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,6 +68,7 @@
|
|||
<script src="scripts/externe/angular.min.js"></script>
|
||||
<script src="scripts/externe/angular-ui-router.min.js"></script>
|
||||
<script src="scripts/externe/angular-cookies.min.js"></script>
|
||||
<script src="scripts/externe/axios.min.js"></script>
|
||||
<script src="scripts/app-angular.js"></script>
|
||||
<script src="scripts/services/bookmark-service.js"></script>
|
||||
<script src="scripts/services/data-service.js"></script>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,23 @@
|
|||
var app = angular.module('bookmarkApp', ['ui.router', 'ngCookies', 'infinite-scroll', 'angular-sortable-view', 'ngDialog']);
|
||||
|
||||
axios.defaults.baseURL = '/api/';
|
||||
// 添加响应拦截器
|
||||
axios.interceptors.response.use(function (response) {
|
||||
let data = response.data;
|
||||
if (data.code === 0) {
|
||||
if (data.msg) {
|
||||
toastr.success(data.msg, "提示");
|
||||
}
|
||||
return Promise.resolve(data.data);
|
||||
} else {
|
||||
toastr.error(`错误信息:${data.msg}(错误码:${data.code})`, '请求错误');
|
||||
return Promise.reject(data);
|
||||
}
|
||||
}, function (error) {
|
||||
toastr.error(`错误信息:${error.toString()}`, '网络错误');
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
app.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {
|
||||
$urlRouterProvider.otherwise("/");
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
app.controller('loginCtr', ['$scope', '$filter', '$state', '$cookieStore', '$window', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $filter, $state, $cookieStore, $window, bookmarkService, pubSubService, dataService) {
|
||||
app.controller('loginCtr', ['$scope', '$filter', '$state', '$http', '$cookieStore', '$window', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $filter, $state, $http, $cookieStore, $window, bookmarkService, pubSubService, dataService) {
|
||||
console.log("Hello loginCtr...", $cookieStore.get("username"));
|
||||
if (dataService.smallDevice()) {
|
||||
$window.location = "http://m.mybookmark.cn/#/tags";
|
||||
|
|
@ -20,51 +20,38 @@ app.controller('loginCtr', ['$scope', '$filter', '$state', '$cookieStore', '$win
|
|||
$scope.passwordRegister1 = "";
|
||||
$scope.passwordRegister2 = "";
|
||||
|
||||
$scope.login = function () {
|
||||
$scope.login = async function () {
|
||||
var autoLogin = $('.ui.checkbox.js-auto-login').checkbox('is checked');
|
||||
if (!$scope.username || !$scope.password) {
|
||||
$scope.showErr = true;
|
||||
$scope.errInfo = '用户名或者密码不能为空!';
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
$scope.showErr = false;
|
||||
$scope.errInfo = '';
|
||||
console.log($scope.username, $scope.password, autoLogin);
|
||||
var params = {
|
||||
username: $scope.username,
|
||||
password: $scope.password,
|
||||
autoLogin: autoLogin,
|
||||
maxAge: 7 * 24 * 3600,
|
||||
};
|
||||
$cookieStore.put("username", $scope.username);
|
||||
bookmarkService.login(params)
|
||||
.then((data) => {
|
||||
console.log(data);
|
||||
if (data.logined) {
|
||||
pubSubService.publish('loginCtr.login', {
|
||||
'login': data.logined,
|
||||
});
|
||||
|
||||
await axios.post('login', params);
|
||||
pubSubService.publish('loginCtr.login', { login: true });
|
||||
$state.go('bookmarks', {})
|
||||
} else {
|
||||
console.log('login failed......................')
|
||||
toastr.error('账号或者密码错误', "错误");
|
||||
}
|
||||
})
|
||||
.catch((err) => console.log('login err', err));
|
||||
}
|
||||
}
|
||||
|
||||
$scope.showRegister = function () {
|
||||
$('.ui.modal.js-register').modal({
|
||||
closable: false,
|
||||
}).modal('setting', 'transition', dataService.animation()).modal('show');
|
||||
|
||||
$scope.showRegister = async function () {
|
||||
$('.ui.modal.js-register').modal({ closable: false }).modal('setting', 'transition', dataService.animation()).modal('show');
|
||||
$scope.emailRegister = "";
|
||||
$scope.usernameRegister = "";
|
||||
$scope.passwordRegister1 = "";
|
||||
$scope.passwordRegister2 = "";
|
||||
|
||||
}
|
||||
|
||||
$scope.register = function () {
|
||||
$scope.register = async function () {
|
||||
if (!$scope.emailRegister || !$scope.usernameRegister || !$scope.passwordRegister1 || !$scope.passwordRegister2) {
|
||||
toastr.error('有必填项为空', "错误");
|
||||
return;
|
||||
|
|
@ -89,22 +76,11 @@ app.controller('loginCtr', ['$scope', '$filter', '$state', '$cookieStore', '$win
|
|||
email: $scope.emailRegister,
|
||||
password: $scope.passwordRegister1,
|
||||
};
|
||||
await axios.post('register', user);
|
||||
|
||||
bookmarkService.register(user)
|
||||
.then((data) => {
|
||||
if (data.retCode == 0) {
|
||||
toastr.success('注册成功', "提示");
|
||||
$('.ui.modal.js-register').modal('hide');
|
||||
$scope.username = $scope.usernameRegister;
|
||||
$scope.password = "";
|
||||
} else {
|
||||
toastr.error('注册失败,您的账号或者邮箱可能已经存在了。错误信息:' + data.msg, "错误");
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log('register err', err);
|
||||
toastr.error('注册失败:' + JSON.stringify(err), "错误");
|
||||
});
|
||||
}
|
||||
|
||||
var className = 'js-form-login';
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue