72 lines
2.5 KiB
PHP
72 lines
2.5 KiB
PHP
<?php
|
|
namespace app\controllers\mini;
|
|
use app\controllers\channel\CommentController;
|
|
use app\models\common\MiniAuth;
|
|
use yii;
|
|
use yii\data\ActiveDataProvider;
|
|
use EasyWeChat\Factory;
|
|
use EasyWeChat\OpenPlatform\Server\Guard;
|
|
class ComponentController extends CommonController {
|
|
|
|
public function actionIndex(){
|
|
$server = $this->wecaht->server;
|
|
// 处理授权成功事件
|
|
$server->push(function ($message) {
|
|
dd($message);
|
|
}, Guard::EVENT_AUTHORIZED);
|
|
|
|
// 处理授权更新事件
|
|
$server->push(function ($message) {
|
|
dd($message);
|
|
}, Guard::EVENT_UPDATE_AUTHORIZED);
|
|
|
|
// 处理授权取消事件
|
|
$server->push(function ($message) {
|
|
dd($message);
|
|
}, Guard::EVENT_UNAUTHORIZED);
|
|
|
|
return $server->serve();
|
|
}
|
|
|
|
public function actionUrl(){
|
|
$url = Yii::$app->request->getHostInfo().'/index.php'.yii\helpers\Url::toRoute(['call-back']);
|
|
echo "<div style='margin:0 auto;'><h2><a href='{$this->wecaht->getPreAuthorizationUrl($url)}'>授权微信小程序</a></h2></div>";
|
|
}
|
|
|
|
public function actionCallBack(){
|
|
$server = $this->wecaht;
|
|
//$auth = $server->handleAuthorize(Yii::$app->request->get('auth_code'));
|
|
$auth = $server->handleAuthorize(Yii::$app->request->get('auth_code'));
|
|
if(!isset($auth)){
|
|
return $this->result(2,$auth['msg']);
|
|
}
|
|
$miniAuth = MiniAuth::find()->where(['authorizer_appid'=>$auth['authorizer_appid']])->one();
|
|
if(empty($miniAuth)){
|
|
$miniAuth = new MiniAuth();
|
|
}
|
|
$params[$miniAuth->formName()]=$auth['authorization_info'];
|
|
if($miniAuth->load($params) && $miniAuth->save()){
|
|
echo "success";
|
|
}else{
|
|
dd($miniAuth->getFirstErrors());
|
|
}
|
|
}
|
|
|
|
public function actionGetInfo(){
|
|
$server = $this->wecaht;
|
|
$info = $server->getAuthorizer('wx2ab4c60f7f12db97');
|
|
$miniAuth = MiniAuth::find()->where(['authorizer_appid'=>'wx2ab4c60f7f12db97'])->one();
|
|
$params[$miniAuth->formName()] = $info['authorization_info'];
|
|
$miniAuth->load($params);
|
|
$miniAuth->authorizer_info = $info['authorizer_info'];
|
|
$miniAuth->save();
|
|
$miniAuth = MiniAuth::find()->where(['authorizer_appid'=>'wx2ab4c60f7f12db97'])->one();
|
|
return $this->result(1,'ok',yii\helpers\ArrayHelper::toArray($miniAuth));
|
|
}
|
|
|
|
public function actionOptions(){
|
|
$server = $this->wecaht;
|
|
$res = $server->getAuthorizers(0,500);
|
|
dd($res);
|
|
}
|
|
} |