ZLW-PC\Administrator
6 years ago
21 changed files with 440 additions and 156 deletions
-
7README.md
-
184app/Common/Wechat/WechatAuth.php
-
2app/Common/Wechat/WechatCallbackApi.php
-
2app/Common/Wechat/WechatMenu.php
-
12app/Common/Wechat/WxComponent.php
-
0app/Common/Wechat/aes/demo.php
-
2app/Common/Wechat/aes/errorCode.php
-
20app/Common/Wechat/aes/pkcs7Encoder.php
-
4app/Common/Wechat/aes/sha1.php
-
81app/Common/Wechat/aes/wxBizDataCrypt.php
-
5app/Common/Wechat/aes/wxBizMsgCrypt.php
-
4app/Common/Wechat/aes/xmlparse.php
-
103app/Common/WechatAuth.php
-
4app/Common/aes/ReadMe.txt
-
2app/Http/Controllers/Admin/WeixinMenuController.php
-
50app/Http/Controllers/Api/ImageController.php
-
4app/Http/Controllers/Api/NotifyController.php
-
2app/Http/Controllers/Weixin/UserController.php
-
86app/Http/Service/Smsbao.php
-
BINpublic/images/pcscreenshots1.jpg
-
BINpublic/images/pcscreenshots2.jpg
@ -0,0 +1,184 @@ |
|||
<?php |
|||
namespace App\Common\Wechat; |
|||
|
|||
/** |
|||
* OAuth2.0微信授权登录实现/微信PC扫码授权登录 |
|||
* 微信/PC扫码登录,两种的方式是一样的,先跳转到微信网页获取code,通过code获取token,通过token获取用户信息 |
|||
*/ |
|||
class WechatAuth |
|||
{ |
|||
//高级功能->开发者模式->获取
|
|||
private $app_id; |
|||
private $app_secret; |
|||
|
|||
public function __construct($app_id, $app_secret) |
|||
{ |
|||
$this->app_id = $app_id; |
|||
$this->app_secret = $app_secret; |
|||
} |
|||
|
|||
/** |
|||
* 获取微信授权链接 |
|||
* |
|||
* @param string $redirect_uri 回调地址,授权后重定向的回调链接地址,请使用urlEncode对链接进行处理 |
|||
* @param mixed $state 可以为空,重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 |
|||
*/ |
|||
public function get_authorize_url($redirect_uri = '', $state = '') |
|||
{ |
|||
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->app_id."&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect"; |
|||
} |
|||
|
|||
/** |
|||
* 微信PC扫码授权登录链接 |
|||
* |
|||
* @param string $redirect_uri 回调地址,授权后重定向的回调链接地址,请使用urlEncode对链接进行处理 |
|||
* @param mixed $state 可以为空,重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 |
|||
*/ |
|||
public function get_qrconnect_url($redirect_uri = '', $state = '') |
|||
{ |
|||
return "https://open.weixin.qq.com/connect/qrconnect?appid".$this->app_id."&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope=snsapi_login&state=".$state."#wechat_redirect"; |
|||
} |
|||
|
|||
/** |
|||
* 获取授权token |
|||
* |
|||
* @param string $code 通过get_authorize_url获取到的code |
|||
*/ |
|||
public function get_access_token($code = '') |
|||
{ |
|||
$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code"; |
|||
$token_data = $this->http($token_url); |
|||
|
|||
return json_decode($token_data, true); |
|||
} |
|||
|
|||
/** |
|||
* 获取access_token,access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。 |
|||
*/ |
|||
public function get_token() |
|||
{ |
|||
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}"; |
|||
$token_data = $this->http($token_url); |
|||
return json_decode($token_data, true); |
|||
} |
|||
|
|||
/** |
|||
* 获取小程序码,适用于需要的码数量较少的业务场景,通过该接口生成的小程序码,永久有效,数量限制见文末说明,请谨慎使用。 |
|||
* @param string $path 不能为空,最大长度 128 字节 |
|||
* @param int $width 二维码的宽度,默认430 |
|||
*/ |
|||
public function getwxacode($path, $width = 430) |
|||
{ |
|||
$access_token = $this->get_token(); |
|||
$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token['access_token']; |
|||
$path ="pages/mine/mine/mine?query=1"; |
|||
$data ='{"path":"'.$path.'","width":'.$width.'}'; |
|||
$res = $this->http($url, $data); |
|||
|
|||
return $res; |
|||
//将生成的小程序码存入相应文件夹下
|
|||
//file_put_contents('./public/wxyido/img/'.time().'.jpg',$return);
|
|||
} |
|||
|
|||
/** |
|||
* 获取小程序码,通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene 字段的值,再做处理逻辑。 |
|||
* @param string $data['scene'] 二维码场景值 |
|||
* @param string $data['page'] 必须是已经发布的小程序存在的页面(否则报错),例如 "pages/index/index" ,根路径前不要填加'/',不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面 |
|||
* @param int $data['width'] 二维码的宽度,默认430 |
|||
* @param int $data['type'] 0路径存储,1base64 |
|||
*/ |
|||
public function getwxacodeunlimit($data) |
|||
{ |
|||
$access_token = $this->get_token(); |
|||
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token['access_token']; |
|||
|
|||
$post_data = array(); |
|||
$post_data['scene'] = $data['scene']; |
|||
$post_data['page'] = $data['page']; |
|||
$post_data['width'] = $data['width']; |
|||
|
|||
$res = $this->http($url, json_encode($post_data)); |
|||
if($data['type']==0) |
|||
{ |
|||
file_put_contents($data['image_path'], $res); |
|||
} |
|||
else |
|||
{ |
|||
$res = $this->data_uri($res); |
|||
} |
|||
|
|||
return $res; |
|||
//将生成的小程序码存入相应文件夹下
|
|||
//file_put_contents('./public/wxyido/img/'.time().'.jpg',$res);
|
|||
} |
|||
|
|||
public function data_uri($contents, $mime = 'image/png') |
|||
{ |
|||
$base64 = base64_encode($contents); |
|||
return ('data:' . $mime . ';base64,' . $base64); |
|||
} |
|||
|
|||
/** |
|||
* 获取授权后的微信用户信息 |
|||
* |
|||
* @param string $access_token |
|||
* @param string $open_id |
|||
*/ |
|||
public function get_user_info($access_token = '', $open_id = '') |
|||
{ |
|||
$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN"; |
|||
$info_data = $this->http($info_url); |
|||
|
|||
return json_decode($info_data, true); |
|||
} |
|||
|
|||
/** |
|||
* 获取用户基本信息(包括UnionID机制) |
|||
* |
|||
* @param string $access_token |
|||
* @param string $open_id |
|||
*/ |
|||
public function get_user_unionid($access_token = '', $open_id = '') |
|||
{ |
|||
$info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$open_id}&lang=zh_CN"; |
|||
$info_data = $this->http($info_url); |
|||
|
|||
return json_decode($info_data, true); |
|||
} |
|||
|
|||
/** |
|||
* 小程序登录凭证校验 |
|||
* 小程序调用wx.login() 获取 临时登录凭证code ,并回传到开发者服务器。 |
|||
* 开发者服务器以code换取 用户唯一标识openid 和 会话密钥session_key。 |
|||
* 临时登录凭证校验接口是一个 HTTPS 接口,开发者服务器使用 临时登录凭证code 获取 session_key 和 openid 等。 |
|||
* @param string $js_code 小程序登录时获取的code |
|||
*/ |
|||
public function miniprogram_wxlogin($js_code) |
|||
{ |
|||
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->app_id}&secret={$this->app_secret}&js_code=$js_code&grant_type=authorization_code"; |
|||
$res = $this->http($url); |
|||
|
|||
return json_decode($res, true); |
|||
} |
|||
|
|||
// cURL函数简单封装
|
|||
public function http($url, $data = null) |
|||
{ |
|||
$curl = curl_init(); |
|||
curl_setopt($curl, CURLOPT_URL, $url); |
|||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); |
|||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); |
|||
|
|||
if (!empty($data)) |
|||
{ |
|||
curl_setopt($curl, CURLOPT_POST, 1); |
|||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
|||
} |
|||
|
|||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|||
$output = curl_exec($curl); |
|||
curl_close($curl); |
|||
|
|||
return $output; |
|||
} |
|||
} |
@ -1,5 +1,5 @@ |
|||
<?php |
|||
namespace App\Common; |
|||
namespace App\Common\Wechat; |
|||
|
|||
/** |
|||
* 微信自定义菜单-响应菜单点击事件 |
@ -1,5 +1,5 @@ |
|||
<?php |
|||
namespace App\Common; |
|||
namespace App\Common\Wechat; |
|||
|
|||
/** |
|||
* 微信自定义菜单 |
@ -0,0 +1,81 @@ |
|||
<?php |
|||
/** |
|||
* 对微信小程序用户加密数据的解密示例代码. |
|||
* |
|||
* @copyright Copyright (c) 1998-2014 Tencent Inc. |
|||
*/ |
|||
|
|||
/** |
|||
* error code 说明. |
|||
* <ul> |
|||
|
|||
* <li>-41001: encodingAesKey 非法</li> |
|||
* <li>-41003: aes 解密失败</li> |
|||
* <li>-41004: 解密后得到的buffer非法</li> |
|||
* <li>-41005: base64加密失败</li> |
|||
* <li>-41016: base64解密失败</li> |
|||
* </ul> |
|||
*/ |
|||
class ErrorCode |
|||
{ |
|||
public static $OK = 0; |
|||
public static $IllegalAesKey = -41001; |
|||
public static $IllegalIv = -41002; |
|||
public static $IllegalBuffer = -41003; |
|||
public static $DecodeBase64Error = -41004; |
|||
} |
|||
|
|||
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; |
|||
} |
|||
} |
@ -1,103 +0,0 @@ |
|||
<?php |
|||
namespace App\Common; |
|||
|
|||
/** |
|||
* OAuth2.0微信授权登录实现/微信PC扫码授权登录 |
|||
* 微信/PC扫码登录,两种的方式是一样的,先跳转到微信网页获取code,通过code获取token,通过token获取用户信息 |
|||
*/ |
|||
class WechatAuth |
|||
{ |
|||
//高级功能->开发者模式->获取
|
|||
private $app_id; |
|||
private $app_secret; |
|||
|
|||
public function __construct($app_id, $app_secret) |
|||
{ |
|||
$this->app_id = $app_id; |
|||
$this->app_secret = $app_secret; |
|||
} |
|||
|
|||
/** |
|||
* 获取微信授权链接 |
|||
* |
|||
* @param string $redirect_uri 回调地址,授权后重定向的回调链接地址,请使用urlEncode对链接进行处理 |
|||
* @param mixed $state 可以为空,重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 |
|||
*/ |
|||
public function get_authorize_url($redirect_uri = '', $state = '') |
|||
{ |
|||
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->app_id."&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect"; |
|||
} |
|||
|
|||
/** |
|||
* 微信PC扫码授权登录链接 |
|||
* |
|||
* @param string $redirect_uri 回调地址,授权后重定向的回调链接地址,请使用urlEncode对链接进行处理 |
|||
* @param mixed $state 可以为空,重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 |
|||
*/ |
|||
public function get_qrconnect_url($redirect_uri = '', $state = '') |
|||
{ |
|||
return "https://open.weixin.qq.com/connect/qrconnect?appid".$this->app_id."&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope=snsapi_login&state=".$state."#wechat_redirect"; |
|||
} |
|||
|
|||
/** |
|||
* 获取授权token |
|||
* |
|||
* @param string $code 通过get_authorize_url获取到的code |
|||
*/ |
|||
public function get_access_token($code = '') |
|||
{ |
|||
$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code"; |
|||
$token_data = $this->http($token_url); |
|||
|
|||
return json_decode($token_data, true); |
|||
} |
|||
|
|||
/** |
|||
* 获取授权后的微信用户信息 |
|||
* |
|||
* @param string $access_token |
|||
* @param string $open_id |
|||
*/ |
|||
public function get_user_info($access_token = '', $open_id = '') |
|||
{ |
|||
$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN"; |
|||
$info_data = $this->http($info_url); |
|||
|
|||
return json_decode($info_data, true); |
|||
} |
|||
|
|||
/** |
|||
* 获取用户基本信息(包括UnionID机制) |
|||
* |
|||
* @param string $access_token |
|||
* @param string $open_id |
|||
*/ |
|||
public function get_user_unionid($access_token = '', $open_id = '') |
|||
{ |
|||
$info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$open_id}&lang=zh_CN"; |
|||
$info_data = $this->http($info_url); |
|||
|
|||
return json_decode($info_data, true); |
|||
} |
|||
|
|||
// cURL函数简单封装
|
|||
public function http($url, $data = null) |
|||
{ |
|||
$curl = curl_init(); |
|||
curl_setopt($curl, CURLOPT_URL, $url); |
|||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); |
|||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); |
|||
|
|||
if (!empty($data)) |
|||
{ |
|||
curl_setopt($curl, CURLOPT_POST, 1); |
|||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
|||
} |
|||
|
|||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|||
$output = curl_exec($curl); |
|||
curl_close($curl); |
|||
|
|||
return $output; |
|||
} |
|||
} |
@ -1,4 +0,0 @@ |
|||
注意事项: |
|||
1.WXBizMsgCrypt.php文件提供了WXBizMsgCrypt类的实现,是用户接入企业微信的接口类。Sample.php提供了示例以供开发者参考。errorCode.php, pkcs7Encoder.php, sha1.php, xmlparse.php文件是实现这个类的辅助类,开发者无须关心其具体实现。 |
|||
2.WXBizMsgCrypt类封装了 DecryptMsg, EncryptMsg两个接口,分别用于开发者解密以及开发者回复消息的加密。使用方法可以参考Sample.php文件。 |
|||
3.加解密协议请参考微信公众平台官方文档。 |
@ -0,0 +1,86 @@ |
|||
<?php |
|||
namespace App\Http\Service; |
|||
|
|||
/** |
|||
* 短信宝 |
|||
*/ |
|||
class Smsbao |
|||
{ |
|||
private $user_name; |
|||
private $password; |
|||
private $smsapi = "http://api.smsbao.com/"; |
|||
private $status_str = array( |
|||
"0" => "短信发送成功", |
|||
"-1" => "参数不全", |
|||
"-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!", |
|||
"30" => "密码错误", |
|||
"40" => "账号不存在", |
|||
"41" => "余额不足", |
|||
"42" => "帐户已过期", |
|||
"43" => "IP地址限制", |
|||
"50" => "内容含有敏感词" |
|||
); |
|||
|
|||
public function __construct($user_name, $password) |
|||
{ |
|||
$this->user_name = $user_name; |
|||
$this->password = $password; |
|||
} |
|||
|
|||
/** |
|||
* 国内短信 |
|||
* |
|||
* @param string $sms_content 要发送的短信内容 |
|||
* @param string $sms_phone 接收的手机号,单发:15205201314,群发:15205201314,15205201315,群发时多个手机号以逗号分隔,一次不要超过99个号码 |
|||
* return string |
|||
*/ |
|||
public function sms($sms_content, $sms_phone) |
|||
{ |
|||
$user = $this->user_name; //短信平台帐号
|
|||
$pass = md5($this->password); //短信平台密码
|
|||
$content = $sms_content; //要发送的短信内容
|
|||
$phone = $sms_phone; //要发送短信的手机号码
|
|||
$sendurl = $this->smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content); |
|||
$result = file_get_contents($sendurl) ; |
|||
|
|||
return $this->status_str[$result]; |
|||
} |
|||
|
|||
/** |
|||
* 国际短信 |
|||
* |
|||
* @param string $sms_content 要发送的短信内容 |
|||
* @param string $sms_phone 接收的手机号,单发:+60901234567,群发:+60901234567,+60901234567,群发时多个手机号以逗号分隔,一次不要超过99个号码,注:国际号码需包含国际地区前缀号码,格式必须是"+"号开头("+"号需要urlencode处理,如:urlencode("+60901234567")否则会出现格式错误) |
|||
* return string |
|||
*/ |
|||
public function wsms($sms_content, $sms_phone) |
|||
{ |
|||
$user = $this->user_name; //短信平台帐号
|
|||
$pass = md5($this->password); //短信平台密码
|
|||
$content = $sms_content; //要发送的短信内容
|
|||
$phone = $sms_phone; //要发送短信的手机号码
|
|||
$sendurl = $this->smsapi."wsms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content); |
|||
$result = file_get_contents($sendurl) ; |
|||
|
|||
return $this->status_str[$result]; |
|||
} |
|||
|
|||
/** |
|||
* 语音验证码发送 |
|||
* |
|||
* @param string $sms_code 发送的验证码 |
|||
* @param string $sms_phone 目标手机号码 |
|||
* return string |
|||
*/ |
|||
public function voice($sms_code, $sms_phone) |
|||
{ |
|||
$user = $this->user_name; //短信平台帐号
|
|||
$pass = md5($this->password); //短信平台密码
|
|||
$content = $sms_code; //要发送的短信内容
|
|||
$phone = $sms_phone; //要发送短信的手机号码
|
|||
$sendurl = $this->smsapi."voice?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content); |
|||
$result = file_get_contents($sendurl) ; |
|||
|
|||
return $this->status_str[$result]; |
|||
} |
|||
} |
Binary file not shown.
After Width: 1076 | Height: 937 | Size: 136 KiB |
Binary file not shown.
After Width: 1082 | Height: 943 | Size: 78 KiB |
Write
Preview
Loading…
Cancel
Save
Reference in new issue