注:此篇代码混乱,纯属个人笔记
<?php function createUser($verifier, $name, $password, $citycode) { if(checkv($verifier)) { //插入 $token = md5($name.$password.time()); echo $token.'<hr/>'; //生成token $token = getaccessToken($token,'a01',$citycode); print_r($token); } else throw new api\ApiException(array('code'=>$this->getErrorCode('API_AUTH_EXPIRED'))); } function login($verifier, $name, $password) { if(checkv($verifier)) { $table = $this->getTable('Ogcommon_Member'); $password = strlen($password)==32?$password:md5($password); $result = $table->get(array(Ogcommon_Member::name.'=?'=>$name,Ogcommon_Member::password.'=?'=>$password)); if(!empty($result)) { if(empty($result['token'])) { //数据库字段 1>id 2>name 3>password 4>tocken (用md5($name.$password.time())保存,其中$password也是经过md5加密的) //返回的accessToken是一个数组 /* function getaccessToken($token,$id,$citycode) { $expireTime = time()+86400*7;//86400=3600*24 $token = md5($token.$id.$expireTime.$citycode); $token = base64_encode($token.'|'.$id.'|'.$expireTime.'|'.$citycode); return array('accessToken'=>$token,'oid'=>$id,'expireTime'=>$expireTime,'citycode'=>$citycode); } */ $result['token'] = md5($name.$password.time()); $table->mod($result['id'],array(Ogcommon_Member::token=>$result['token'])); } $token = $this->getaccessToken($result['token'],$result['id'],$result['citycode']); return new api\models\token($token); } else throw new api\ApiException(array('code'=>$this->getErrorCode('API_INVALID_AUTH'))); } else throw new api\ApiException(array('code'=>$this->getErrorCode('API_AUTH_EXPIRED'))); } function refresh($accessToken,$citycode) { // TODO Auto-generated method stub if($user = $this->accessToken($accessToken,$citycode)) { if(isset($citycode) && $citycode!=$user['citycode']) $user['citycode'] = $citycode; $token = $this->getaccessToken($user['token'],$user['id'],$user['citycode']); return new api\models\token($token); } else throw new api\ApiException(array('code'=>$this->getErrorCode('API_AUTH_EXPIRED'))); } function getUser($accessToken,\api\models\Query $query) { if($user = $this->accessToken($accessToken)) { $table = $this->getTable('Ogcommon_Member'); if(empty($query->where)) { $user['oid'] = $user['id']; return new api\models\userProfile($user); } $result = $table->get($query->where); if(!empty($result)) { $result['oid'] = $result['id']; return new api\models\userProfile($result); } else throw new api\ApiException(array('code'=>$this->getErrorCode('API_USER_NOT_EXIST'))); } else throw new api\ApiException(array('code'=>$this->getErrorCode('API_AUTH_EXPIRED'))); } function bindMobile($accessToken, $mobile, $code) { // TODO Auto-generated method stub if($user = $this->accessToken($accessToken)) { if(empty($user['id'])) throw new api\ApiException(array('code'=>$this->getErrorCode('API_PERMISSION_DENIED'))); $table = $this->getTable('Ogcommon_Member'); $where = array( Ogcommon_Member::id.'=?'=>$user['id'], Ogcommon_Member::mobile.'=?'=>$mobile, Ogcommon_Member::secret.'=?'=>$code ); return $table->mod($where, array(Ogcommon_Member::isBinded=>1,Ogcommon_Member::secret=>'')); } else throw new api\ApiException(array('code'=>$this->getErrorCode('API_AUTH_EXPIRED'))); } function checkv($verifier) { return TRUE; } function getaccessToken($token,$id,$citycode) { $expireTime = time()+86400*7;//86400=3600*24 $token = md5($token.$id.$expireTime.$citycode); $token = base64_encode($token.'|'.$id.'|'.$expireTime.'|'.$citycode); return array('accessToken'=>$token,'oid'=>$id,'expireTime'=>$expireTime,'citycode'=>$citycode); } function accessToken($accessToken,$citycode=NULL) { if(empty($accessToken) && strlen($citycode)==6 && $citycode[0]=='H' && $citycode[1]=='D') { return $this->getuserdata($citycode); } $token = base64_decode($accessToken); $token = explode('|', $token); if(is_array($token) && count($token)==4 && $token[2]>time()) {//0:token,1:id,2:time,3:citycode if(empty($token[1])) $data = $this->getuserdata($token[3]); else { $member = $this->getTable('Ogcommon_Member'); $data = $member->get(array('{id}=?'=>$token[1])); } if(md5($data['token'].$data['id'].$token[2].$token[3]) == $token[0]) { if(isset($citycode) && $citycode!=$data['citycode'] && $member instanceof Ogcommon_Member) { $member->mod(array('{id}=?'=>$token[1]), array(Ogcommon_Member::citycode=>$citycode)); } return $data; } else return FALSE; } else return FALSE; } function getuserdata($citycode) { $data = array( 'id' => '0', 'name' => 'xxx.com', 'password' => '', 'fullName' => '网络技术有限公司', 'nickName' => 'xx网络', 'icon' => '', 'gender' => '9', 'mobile' => '4001-55-4001', 'isBinded' => '1', 'email' => '', 'citycode' => $citycode,//'HD0001' 'credit' => '0', 'picture' => 'http://img.xxx.com/', 'token' => '179b3c4e4428d6c5t4r59573842ef1b1' ); return $data; } createUser('abc','肖红阳','321321','HD0001');