微信ウィジェット登録(php)

28946 ワード

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; } ?>