1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/6/11
- * Time: 下午5:03
- */
- namespace app\api\controller\v1;
- use app\common\library\WeChatObject;
- use app\main\constants\ErrorCodeConstants;
- use app\main\service\LogService;
- use app\main\service\OpenPlatformService;
- use EasyWeChat\Kernel\Exceptions\HttpException;
- use Psr\SimpleCache\InvalidArgumentException;
- use think\Request;
- class Wechat extends Init
- {
- /**
- * 获取渠道token
- */
- public function token()
- {
- $admin_id = Request::instance()->param('admin_id');
- LogService::info('getToken:admin_id:' . $admin_id);
- if (!$admin_id) {
- $this->setMsg('缺少参数')->setCode(ErrorCodeConstants::PARAMS_ERROR_EMPTY)->getReturn();
- }
- $adminConfig = model('AdminConfig')->getAdminInfoAll($admin_id);
- if (!$adminConfig) {
- $this->setMsg('参数无效')->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->getReturn();
- }
- $wechat = new WeChatObject($adminConfig);
- $officialAccount = $wechat->getOfficialAccount();
- if (!$officialAccount) {
- $this->setMsg('账号未授权')->setCode(ErrorCodeConstants::PERMISSION_DENY)->getReturn();
- }
- try {
- $token = $officialAccount->access_token->getToken();
- } catch (HttpException $e) {
- $this->setMsg('账号被封')->setCode(ErrorCodeConstants::PERMISSION_DENY)->getReturn();
- } catch (InvalidArgumentException $e) {
- $this->setMsg($e->getMessage())->setCode(ErrorCodeConstants::API_ERROR)->getReturn();
- } catch (\Exception $e) {
- $this->setMsg($e->getMessage())->setCode(ErrorCodeConstants::API_ERROR)->getReturn();
- }
- if (isset($token) && is_array($token) && array_key_exists('authorizer_access_token', $token)) {
- $proxy = OpenPlatformService::instance()->getProxyconfigByChannel($admin_id, null, 'job_proxy_config', true);
- $data = [
- 'appid' => $adminConfig['appid'],
- 'token' => $token['authorizer_access_token'],
- 'proxy_config' => $proxy ?: [],
- ];
- $this->setData($data)->getReturn();
- } else {
- $this->setMsg('获取token失败')->setCode(ErrorCodeConstants::API_ERROR)->getReturn();
- }
- }
- }
|