<?php // +---------------------------------------------------------------------- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ] // +---------------------------------------------------------------------- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved. // +---------------------------------------------------------------------- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权 // +---------------------------------------------------------------------- // | Author: CRMEB Team <admin@crmeb.com> // +---------------------------------------------------------------------- namespace app\controller\api\wechat; use app\common\ApiBaseController; use app\Request; use app\services\wechat\RoutineServices; use app\services\wechat\WechatServices; use GuzzleHttp\Exception\GuzzleException; use qiniu\services\huifu\CacheService; use qiniu\services\huifu\wechat\OfficialAccount; use qiniu\services\huifu\wechat\Work; use EasyWeChat\Kernel\Exceptions\BadRequestException; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; use EasyWeChat\Kernel\Exceptions\InvalidConfigException; use ReflectionException; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\Response; /** * 微信公众号 * Class WechatController * @package app\controller\api\wechat */ class Wechat extends ApiBaseController { /** * WechatController constructor. * @param Request $request * @param WechatServices $service */ public function __construct(Request $request, WechatServices $service) { parent::__construct($request); $this->service = $service; } /** * 微信公众号服务 * @return Response * @throws BadRequestException * @throws InvalidArgumentException * @throws InvalidConfigException * @throws ReflectionException */ public function serve() { return $this->service->serve(); } /** * 公众号权限配置信息获取 * @param Request $request * @return mixed * @throws GuzzleException * @throws \Psr\SimpleCache\InvalidArgumentException */ public function config(Request $request) { $url = $request->get('url', '') ?: sys_config('site_url'); return $this->success($this->service->config($url)); } /** * 公众号授权登陆 * @param Request $request * @return mixed * @throws DbException * @throws InvalidConfigException * @throws DataNotFoundException * @throws ModelNotFoundException */ public function auth(Request $request) { [$spread_spid, $login_type] = $request->getMore([ [['spread_spid', 'd'], 0], ['login_type', 'wechat'], ], true); $token = $this->service->auth($spread_spid, $login_type); if ($token && isset($token['key'])) { return $this->success('授权成功,请绑定手机号', $token); } else if ($token) { return $this->success('登录成功', ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]); } else return $this->error('登录失败'); } /** * 微信公众号静默授权 * @param string $spread_spid * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function silenceAuth($spread_spid = '') { $token = $this->service->silenceAuth($spread_spid); if ($token && isset($token['key'])) { return $this->success('授权成功,请绑定手机号', $token); } else if ($token) { return $this->success('登录成功', ['token' => $token['token'], 'expires_time' => $token['params']['exp']]); } else return $this->error('登录失败'); } /** * 微信公众号静默授权 * @param $spread_spid * @param $snsapi * @return mixed * @throws DataNotFoundException * @throws ModelNotFoundException */ public function silenceAuthNoLogin($spread_spid = '', $snsapi = '') { $token = $this->service->silenceAuth($spread_spid, true, $snsapi); if ($token && isset($token['auth_login'])) { return $this->success('授权成功', $token); } else if ($token) { return $this->success('登录成功', ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]); } else return $this->error('登录失败'); } /** * 静默授权 手机号直接注册登录 * @param string $key * @param string $phone * @param string $captcha * @return mixed * @throws \Psr\SimpleCache\InvalidArgumentException * @throws DataNotFoundException * @throws ModelNotFoundException|DbException */ public function authBindingPhone($key = '', $phone = '', $captcha = '') { //验证验证码 check_sms_captcha($phone, 'wechat_auth', $captcha); $token = $this->service->authBindingPhone($key, $phone); if ($token) { return $this->success('登录成功', $token); } else return $this->error('登录失败'); } /** * 关注二维码 * @return mixed */ public function follow() { $data = $this->service->follow(); if ($data) { return $this->success('ok', $data); } else { return $this->error('获取失败'); } } }