/** * undocumented function * * @return void * @author **/ public function actionCreateWechat() { if (Yii::$app->request->get('auth_code', false)) { // 获取Token信息 $token = Token::getInstance(); $auth_code = Yii::$app->request->get('auth_code', false); $authorizer_access_token = $token->getAuthorizerAccessToken($auth_code, false); $authorizer_info = $token->getAuthorizerInfo(); $authorizer_all = $token->getAuthorizerInfo(false); if ($authorizer_info) { $mApp = new App(); // 判断账号是否已经被授权 $app_id = $authorizer_access_token->authorizer_appid; if (!$mApp->find()->where(['authorizer_appid' => $app_id, 'app_active' => 0])->one()) { // 保存二维码 $qrlog_name = md5($app_id) . ".jpg"; if (!file_exists(Yii::getAlias('@service/web/uploads/weixin'))) { mkdir(Yii::getAlias('@service/web/uploads/weixin')); } file_put_contents(Yii::getAlias('@service/web/uploads/weixin/') . $qrlog_name, file_get_contents($authorizer_info->qrcode_url)); // 存入数据 $data['App'] = ['service_id' => $this->user->id, 'authorizer_appid' => $app_id, 'authorizer_access_token' => json_encode($authorizer_access_token->authorizer_all), 'app_service_type' => $authorizer_info->service_type_info->id, 'app_name' => $authorizer_info->nick_name, 'app_origin_id' => $authorizer_info->user_name, 'app_logo' => $authorizer_info->head_img, 'app_qrlogo' => $qrlog_name, 'app_alias' => $authorizer_info->alias, 'authorizer_info' => json_encode($authorizer_all)]; if ($mApp->load($data) && $mApp->save(false)) { Yii::$app->session->setFlash('success', '授权成功:' . $authorizer_info->nick_name); return $this->redirect('wechat'); } } else { Yii::$app->session->setFlash('error', '授权异常:此公众号已被授权到另一个账号了'); return $this->redirect('wechat'); } } } $base_url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s"; $data['rd_url'] = sprintf($base_url, $this->config->platform_info->appId, $this->token->getPreAuthCode(), urlencode(Url::current([], true))); return $this->render('create-wechat', ['data' => $data]); }