/**
* 会员登录
* @ApiMethod (POST)
* @ApiParams (name="code", type="string", required=true, description="code")
* @ApiReturn ({ "code": 1,"msg": "登录成功", "time": "1562726766"})
* @ApiParams (name="encryptedData", type="string", required=true, description="encryptedData")
* @ApiParams (name="iv", type="string", required=true, description="iv")
* @ApiParams (name="invite_code", type="string", required=true, description="invite_code")
* @ApiParams (name="username", type="string", required=true, description="username")
* @ApiParams (name="avatarUrl", type="string", required=true, description="avatarUrl")
*/
public function login(){
$APPID = 'wx*********';
$AppSecret = 'e1cd0a3**********c0f22f6582f';
$code = $this->request->request('code');
$encryptedData = $this->request->request('encryptedData');
$invite_code = $this->request->request('invite_code');//邀请码(不重要)
$iv = $this->request->request('iv');
$username = $this->request->request('username');//(微信名字)
$username = filterEmoji($username);//微信名字特殊符号处理
$avatarUrl = $this->request->request('avatarUrl');//微信头像
//头像处理
$path = file_get_contents($avatarUrl);
$name = md5(rand(0,9999)).'.'.'png';
// 微信头像存入数据库中
file_put_contents('./uploads/avatar/'.$name,$path);
if (empty($invite_code)){
$invite_code = '';
}
if (empty($code) || empty($encryptedData) || empty($iv) || empty($username) || empty($avatarUrl)){
$this->error('缺少参数');
}
$url='https://api.weixin.qq.com/sns/jscode2session?appid='.$APPID.'&secret='.$AppSecret.'&js_code='.$code.'&grant_type=authorization_code';
$html = file_get_contents($url);
$jsondecode = json_decode($html, true);
$array = $jsondecode;
$session_key = isset( $array['session_key'] ) ? $array['session_key'] : '';
//根据session_key,encryptedData,iv 解析字符串,获取信息
if($encryptedData && $iv){
// decryptData 解密方法
$errCode = decryptData( $APPID, $session_key,$encryptedData, $iv, $stepData );
if ($errCode == 0) { //解析成功,更新数据
$stepData = json_decode($stepData, true);
// 获取手机号
$phone = $stepData['phoneNumber'];
}
}
$openid = Db::name('user')->where('openid',$array['openid'])->find();
//判断数据空中是否与openid,如果有就是登陆,顺便更新一下数据库中的session_key字段
//session_key和open_id都在array中获得
//所有参数都获取到了,就可以写下边的逻辑了
}
上边登陆中用到的方法 微信名字特殊符号处理(filterEmoji)
微信名字特殊符号处理
function filterEmoji($str)
{
$str = preg_replace_callback(
'/./u',
function (array $match) {
return strlen($match[0]) >= 4 ? '' : $match[0];
},
$str);
return $str;
}
根据session_key,encryptedData,iv 解析字符串,获取信息获取手机号(decryptData)
//解密
function decryptData( $appid, $sessionKey, $encryptedData, $iv, &$data )
{
$OK = 0;
$IllegalAesKey = -41001;
$IllegalIv = -41002;
$IllegalBuffer = -41003;
$DecodeBase64Error = -41004;
if (strlen($sessionKey) != 24) {
return $IllegalAesKey;
}
$aesKey=base64_decode($sessionKey);
if (strlen($iv) != 24) {
return $IllegalIv;
}
$aesIV=base64_decode($iv);
$aesCipher=base64_decode($encryptedData);
//var_dump($aesKey,$aesIV,$aesCipher);
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj=json_decode( $result );
if( $dataObj == NULL )
{
return $IllegalBuffer;
}
if( $dataObj->watermark->appid != $appid )
{
return $IllegalBuffer;
}
$data = $result;
return $OK;
}
下一篇文章是小程序生成二维码。