微信小程序登录(php)

将(PHP文件)放到vendor目录下

  public function wxlogin()
    {
        //开发者使用登陆凭证 code 获取 session_key 和 openid
        $APPID = 'XXXXXXXX';//自己配置
        $AppSecret = 'XXXXXXXXXXX';//自己配置
        if(empty($this->request->post('code')) || empty($this->request->post('signature')) || empty($this->request->post('rawData')) || empty($this->request->post('encryptedData')) || empty($this->request->post('iv'))){
            $this->error(0,'参数缺失');
        }
        $code = $this->request->post('code');
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $APPID . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
        $arr = $this->vget($url);  // 一个使用curl实现的get方法请求
        $arr = json_decode($arr, true);
        if(empty($arr)||empty($arr['openid'])||empty($arr['session_key'])){
            $this->error(0,'请求微信接口失败,appid或私钥不匹配!');
        }
        $session_key = $arr['session_key'];
        //将php(PHP文件夹)放到vendor目录下
        Vendor('PHP.wxBizDataCrypt');
        $encryptedData = $this->request->post('encryptedData');
        $iv = $this->request->post('iv');
        $pc = new \WXBizDataCrypt($APPID, $session_key);
        $errCode = $pc->decryptData($encryptedData, $iv, $data); //其中$data包含用户的所有数据
        $data = json_decode($data,true);
        if ($errCode == 0) {
            $id = model('member')->where('openid',$data['openId'])->field('id')->select();
            if(empty($id)){
                model('member')->save([
                    'nickname' => $data['nickName'],
                    'avatar' => $data['avatarUrl'],
                    'openid' => $data['openId'],
                    'createtime'=>time(),
                    'updatetime'=>time()
                ]);
                $id = model('member')->where('openid',$data['openId'])->field('id')->find();
                $this->success('登录成功',$id);
            }else{
                $this->success('登录成功',$id);
            }
        } else {
            $this->error($errCode,'');
        }
    }

php文件夹中的文件
wxBizDataCrypt.php



/**
 * 对微信小程序用户加密数据的解密示例代码.
 *
 * @copyright Copyright (c) 1998-2014 Tencent Inc.
 */


include_once "errorCode.php";


class WXBizDataCrypt
{
    private $appid;
	private $sessionKey;

	/**
	 * 构造函数
	 * @param $sessionKey string 用户在小程序登录后获取的会话密钥
	 * @param $appid string 小程序的appid
	 */
	public function __construct( $appid, $sessionKey)
	{
		$this->sessionKey = $sessionKey;
		$this->appid = $appid;
	}


	/**
	 * 检验数据的真实性,并且获取解密后的明文.
	 * @param $encryptedData string 加密的用户数据
	 * @param $iv string 与用户数据一同返回的初始向量
	 * @param $data string 解密后的原文
     *
	 * @return int 成功0,失败返回对应的错误码
	 */
	public function decryptData( $encryptedData, $iv, &$data )
	{
		if (strlen($this->sessionKey) != 24) {
			return ErrorCode::$IllegalAesKey;
		}
		$aesKey=base64_decode($this->sessionKey);

        
		if (strlen($iv) != 24) {
			return ErrorCode::$IllegalIv;
		}
		$aesIV=base64_decode($iv);

		$aesCipher=base64_decode($encryptedData);

		$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);

		$dataObj=json_decode( $result );
		if( $dataObj  == NULL )
		{
			return ErrorCode::$IllegalBuffer;
		}
		if( $dataObj->watermark->appid != $this->appid )
		{
			return ErrorCode::$IllegalBuffer;
		}
		$data = $result;
		return ErrorCode::$OK;
	}

}


errorCode.php



/**
 * error code 说明.
 * 
    *
  • -41001: encodingAesKey 非法
  • *
  • -41003: aes 解密失败
  • *
  • -41004: 解密后得到的buffer非法
  • *
  • -41005: base64加密失败
  • *
  • -41016: base64解密失败
  • *
*/
class ErrorCode { public static $OK = 0; public static $IllegalAesKey = -41001; public static $IllegalIv = -41002; public static $IllegalBuffer = -41003; public static $DecodeBase64Error = -41004; } ?>

你可能感兴趣的:(微信小程序登录(php))