123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace App\Http\Controllers;
- use App\Http\Controllers\Controller;
- use App\Http\Requests;
- use Illuminate\Http\Request;
- use App\Libs\Classes\BLogger;
- use App\Libs\Classes\WxSign;
- use EasyWeChat\Foundation\Application;
- use WechatOP;
- use Illuminate\Support\Facades\Redis;
- use App\Http\Models\WechatGroupGzh;
- use App\Http\Controllers\Wechat\Api\WechatInnerApisController;
- use Doctrine\Common\Cache\PredisCache;
- class WechatController extends Controller
- {
-
- public $wx_log_path;
- public $options;
- public $host;
- public $auth_host;
- public $third_host;
- public $WxSign;
- public $token;
- public $gzh_name;
- public $gzh_app_id;
- public $Redis;
- public $WechatApi;
- public $param;
- public $official_account;
- public function __construct($gzh_app_id='') {
-
- $this->param = array();
- $this->gzh_app_id = $gzh_app_id;
- $this->param['gzh_app_id'] = $this->gzh_app_id;
- $this->param['openid'] = '';
-
- if(!empty($this->gzh_app_id) && $this->gzh_app_id != 'wx570bc396a51b8ff8'){
- $this->wechat_group_gzh = WechatGroupGzh::get_wechat_group_gzh($this->gzh_app_id);
- if(!empty($this->wechat_group_gzh)) {
- $this->param['group_api'] = $this->wechat_group_gzh['group_api'];
- $this->param['group'] = $this->wechat_group_gzh['group'];
- }else{
- v('invalid gzh_app_id:'.$gzh_app_id);
-
- }
- }
-
- $this->wx_log_path = '/var/www/ydy_wechat/storage/logs/easywechat.log';
- $this->domain = env('DOMAIN');
- $this->auth_host = $this->host = env('ONLINE_AUTH_HOST');
- if(env('DEVELOP_MODE') == 'online'){
- $this->Redis = Redis::connection();
- }elseif(env('DEVELOP_MODE') == 'online_test'){
- $this->Redis = Redis::connection();
- }elseif(env('DEVELOP_MODE') == 'test'){
- $this->Redis = Redis::connection('test_redis');
- }elseif(env('DEVELOP_MODE') == 'local'){
- $this->wx_log_path = '/Applications/MAMP/htdocs/ydy_wechat/storage/logs/easywechat.log';
- $this->Redis = Redis::connection('test_redis');
- }
- $this->WxSign = new WxSign();
- $this->oauth_platform_callback_base_url = $this->auth_host.'oauth/';
-
- $this->token = env('WECHAT_OP_TOKEN');
-
- if(!empty($this->gzh_app_id)){
-
-
- $redis_key = '[wechat_op.common.component_refresh_token.'.$this->gzh_app_id.']';
- $component_refresh_token = Redis::Get($redis_key);
-
- $this->options = [
- 'app_id' => $this->gzh_app_id,
- 'secret' => env('WECHAT_OP_SECRET'),
- 'token' => env('WECHAT_OP_TOKEN'),
- 'aes_key' => env('WECHAT_OP_AES_KEY'),
- 'auth_type' => 'COMPONENT',
- 'component_refresh_token' => $component_refresh_token,
- 'oauth' => [
- 'scopes' => ['snsapi_base'],
- 'callback' => '/oauth_callback',
- ],
- 'cache' => [
- 'driver' => 'redis',
- 'dir' => storage_path('tmp')
- ],
- ];
-
- $this->app = WechatOP::app($this->options);
-
- $this->param['app'] = $this->app;
- $this->WechatApi = new WechatInnerApisController($this->param);
- $this->official_account = $this->WechatApi->get_official_account($this->gzh_app_id);
- $this->param['official_account'] = $this->official_account;
- $this->param['WechatApi'] = $this->WechatApi;
- }
- $this->set_init_param();
- v('post:');v($_POST);
- }
-
-
- public function set_init_param(){
- if(empty($_POST)) {
- $_POST = $_REQUEST;
- }
- if(empty($_REQUEST)) {
- $_REQUEST = $_POST;
- }
- if(isset($_POST['_url'])) {
- unset($_POST['_url']);
- }
- if(isset($_REQUEST['_url'])) {
- unset($_REQUEST['_url']);
- }
- }
-
- public function redirect_url($response_url){
- header("Location:".$response_url);
- exit;
- }
-
-
- public function check_sign_params($request){
- $result = array('code'=>1,'msg'=>'','data'=>'');
- $timestamp = !empty($request->get('timestamp'))?$request->get('timestamp'):'';
- $sign = !empty($request->get('sign'))?$request->get('sign'):'';
- v('check_sign_params:$timestamp:'.$timestamp.' $sign:'.$sign);
- if(empty($timestamp) || empty($sign)){
- $result['code'] = 0;
- $result['msg'] = 'invalid param';
- return $result;
- }
-
- $timestamp = $request->get('timestamp');
- $sign = $request->get('sign');
- $now = microtime(true);
- $diff = $now - $timestamp;
- v('timestamp:'.$timestamp.' now:'.$now.' diff:'.$diff);
-
- if($diff > 28*3600){
- $result['code'] = 0;
- $result['msg'] = 'invalid time';
- return $result;
- }
- $my_sign = $this->WxSign->get_sign($request->all());
- v('check_sign_params sign:'.$sign.' my_sign:'.$my_sign.' info:'.json_encode($request->all()));
- if($sign != $my_sign){
- $result['code'] = 0;
- $result['msg'] = 'invalid sign';
- return $result;
- }
- return $result;
- }
-
- }
|