|
通过$resource实现
js端
- .factory('User', function(ENV, $resource, Storage,$rootScope) {
- var apiUrl = ENV.api ;
- var storageKey = 'user';
- var resource = $resource(ENV.api+'?a=login2');
- // var resource = $resource(ENV.api + '/accesstoken');
- // var userResource = $resource(ENV.api + '/user/:loginname', {
- // loginname: ''
- // });
- var user = Storage.get(storageKey) || {};
- return {
- login: function(username,password) {
- var $this = this;
- return resource.save({
- username: username,
- password: password
- }, function(response) {
- //console.log(response);
- user=response.result;
- $rootScope.$broadcast('User.loginUpdated');
- });
- },
- logout: function() {
- user = {};
- Storage.remove(storageKey);
- },
- getCurrentUser: function(){
- return user;
- }
- };
- });
复制代码
服务端
- function login2(){
-
- $postData=file_get_contents('php://input', true);
-
- $d=json_decode($postData);
- $username=isset($d->username)?dhtmlspecialchars($d->username):'';
- $password=isset($d->password)?dhtmlspecialchars($d->password):'';
-
- // file_put_contents('t1.txt',$username'---'.$password);
-
-
- $sql="SELECT uid,username,salt,password from pre_ucenter_members where username='{$username}'";
- $userinfo=select($sql);
- if($userinfo){
- if(md5(md5($password).$userinfo[0]['salt']) === $userinfo[0]['password'])
- {
- //如果密码正确即可授权登陆
- $rdata['result']=$userinfo[0];
- $rdata['result']['success']=true;
- }else {
- // 密码不正确
- $rdata['result']['message']='密码不正确';
- $rdata['result']['success']=false;
- }
- }else{
- // 没有此用户
- $rdata['result']['message']='此用户不存在';
- $rdata['result']['success']=false;
- }
-
- return $rdata;
- }
复制代码
看不懂的话先学学基础教程
|
|