123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\wechat;
- use app\services\BaseServices;
- use app\dao\wechat\WechatUserDao;
- use app\services\user\UserServices;
- use app\services\user\UserVisitServices;
- use crmeb\services\CacheService;
- use crmeb\services\CacheService as Cache;
- use crmeb\services\wechat\OfficialAccount;
- use crmeb\services\wechat\Work;
- use crmeb\utils\Canvas;
- use EasyWeChat\Kernel\Exceptions\BadRequestException;
- use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
- use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
- use GuzzleHttp\Exception\GuzzleException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- /**
- *
- * Class WechatServices
- * @package app\services\wechat
- * @mixin WechatUserDao
- */
- class WechatServices extends BaseServices
- {
- /**
- * WechatServices constructor.
- * @param WechatUserDao $dao
- */
- public function __construct(WechatUserDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 微信公众号服务
- * @return \think\Response
- * @throws BadRequestException
- * @throws InvalidArgumentException
- * @throws InvalidConfigException
- * @throws \ReflectionException
- */
- public function serve()
- {
- ob_clean();
- return OfficialAccount::serve();
- }
- /**
- * 企业微信服务
- * @return \think\Response
- * @throws BadRequestException
- * @throws InvalidArgumentException
- * @throws InvalidConfigException
- * @throws \ReflectionException
- */
- public function workServe()
- {
- ob_clean();
- return Work::serve();
- }
- /**
- * 公众号权限配置信息获取
- * @param $url
- * @return mixed
- * @throws GuzzleException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function config($url)
- {
- return json_decode(OfficialAccount::jsSdk($url), true);
- }
- /**
- * 获取授权信息
- * @param string $code
- * @return array
- */
- public function getAuthWechatInfo()
- {
- try {
- $userInfoConfig = OfficialAccount::tokenFromCode();
- } catch (\Throwable $e) {
- \think\facade\Log::error([
- 'error' => '授权失败:' . $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine()
- ]);
- throw new ValidateException('授权失败');
- }
- if (!isset($userInfoConfig['openid']) || !$userInfoConfig['openid']) {
- throw new ValidateException('openid获取失败');
- }
- return $userInfoConfig;
- }
- /**
- * 获取返回信息
- * @param $user
- * @param string $userType
- * @return array
- */
- public function getReturnInfo($user, string $userType = 'wechat')
- {
- if (!$user || !isset($user['uid']) || !$user['uid']) {
- throw new ValidateException('获取用户信息失败');
- }
- $token = $this->createToken((int)$user['uid'], $userType, $user['pwd'] ?? '');
- if (!$token) {
- throw new ValidateException('登录失败!');
- }
- /** @var UserVisitServices $visitServices */
- $visitServices = app()->make(UserVisitServices::class);
- $visitServices->loginSaveVisit($user);
- $token['store_user_avatar'] = 0;
- $token['userInfo'] = is_object($user) && method_exists($user, 'toArray') ? $user->toArray() : $user;
- $token['expires_time'] = $token['params']['exp'] ?? 0;
- return $token;
- }
- /**
- * 公众号授权登录,返回是否需要绑定手机号token
- * @param $spread_uid
- * @return array
- * @throws DataNotFoundException
- * @throws InvalidConfigException
- * @throws ModelNotFoundException
- * @throws \think\db\exception\DbException
- */
- public function authLogin($spread_uid = '')
- {
- $wechatInfo = OfficialAccount::userFromCode();
- if (!isset($wechatInfo['nickname'])) {
- $wechatInfo = OfficialAccount::userService()->get($wechatInfo['openid']);
- if (!isset($wechatInfo['nickname']))
- throw new ValidateException('授权失败');
- if (isset($wechatInfo['tagid_list']))
- $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
- } else {
- if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
- }
- $wechatInfo['user_type'] = 'wechat';
- $openid = $wechatInfo['openid'];
- /** @var WechatUserServices $wechatUserServices */
- $wechatUserServices = app()->make(WechatUserServices::class);
- $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
- $createData = [$openid, $wechatInfo, $spread_uid, 'wechat', 'wechat'];
- //获取是否强制绑定手机号
- $storeUserMobile = sys_config('store_user_mobile');
- if ($storeUserMobile && (($user && $user['phone'] == '') || !$user)) {
- $userInfoKey = md5($openid . '_' . time() . '_wechat');
- CacheService::set($userInfoKey, $createData, 7200);
- return ['bindPhone' => true, 'key' => $userInfoKey];
- }
- if (!$user) {
- $user = $wechatUserServices->wechatOauthAfter($createData);
- } else {
- $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
- }
- return $this->getReturnInfo($user);
- }
- /**
- * @param $spread_uid
- * @param $login_type
- * @return array
- * @throws DataNotFoundException
- * @throws InvalidConfigException
- * @throws ModelNotFoundException
- * @throws \think\db\exception\DbException
- */
- public function auth($spread_uid, $login_type)
- {
- $wechatInfo = $this->getAuthWechatInfo();
- if (!isset($wechatInfo['nickname'])) {
- $wechatInfo = OfficialAccount::userService()->get($wechatInfo['openid']);
- if (!isset($wechatInfo['nickname']))
- throw new ValidateException('授权失败');
- if (isset($wechatInfo['tagid_list']))
- $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
- } else {
- if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- if (!$wechatUser->getOne(['openid' => $wechatInfo['openid']])) {
- $wechatInfo['subscribe'] = 0;
- }
- }
- $wechatInfo['user_type'] = 'wechat';
- $openid = $wechatInfo['openid'];
- /** @var WechatUserServices $wechatUserServices */
- $wechatUserServices = app()->make(WechatUserServices::class);
- $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
- $createData = [$openid, $wechatInfo, $spread_uid, $login_type, 'wechat'];
- if (!$user) {
- $user = $wechatUserServices->wechatOauthAfter($createData);
- } else {
- //更新用户信息
- $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
- }
- return $this->getReturnInfo($user);
- }
- /**
- * 新公众号授权登录
- * @param $spread_uid
- * @param $login_type
- * @return mixed
- * @throws DataNotFoundException
- * @throws InvalidConfigException
- * @throws ModelNotFoundException
- */
- public function newAuth($spread_uid, $login_type)
- {
- $wechatInfo = OfficialAccount::userFromCode();
- if (!isset($wechatInfo['nickname'])) {
- $wechatInfo = OfficialAccount::userService()->get($wechatInfo['openid']);
- if (!isset($wechatInfo['nickname']))
- throw new ValidateException('授权失败');
- if (isset($wechatInfo['tagid_list']))
- $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
- } else {
- if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
- }
- $wechatInfo['user_type'] = 'wechat';
- $openid = $wechatInfo['openid'];
- /** @var WechatUserServices $wechatUserServices */
- $wechatUserServices = app()->make(WechatUserServices::class);
- $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
- $createData = [$openid, $wechatInfo, $spread_uid, $login_type, 'wechat'];
- //获取是否强制绑定手机号
- $storeUserMobile = sys_config('store_user_mobile');
- if ($storeUserMobile && !$user) {
- $userInfoKey = md5($openid . '_' . time() . '_wechat');
- Cache::setTokenBucket($userInfoKey, $createData, 7200);
- return ['key' => $userInfoKey];
- } else if (!$user) {
- $user = $wechatUserServices->wechatOauthAfter($createData);
- } else {
- //更新用户信息
- $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
- }
- return $this->getReturnInfo($user);
- }
- public function follow()
- {
- $canvas = Canvas::instance();
- $path = 'uploads/follow/';
- $imageType = 'jpg';
- $name = 'follow';
- $siteUrl = sys_config('site_url');
- $imageUrl = $path . $name . '.' . $imageType;
- $canvas->setImageUrl(public_path() . 'statics/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
- $wechatQrcode = sys_config('wechat_qrcode');
- if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
- $wechatQrcode = substr($wechatQrcode, $strlen);
- }
- if (!$wechatQrcode)
- throw new ValidateException('请上传二维码');
- $canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
- $image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
- return ['path' => $image ? $siteUrl . '/' . $image : ''];
- }
- /**
- * 微信公众号静默授权
- * @param $spread
- * @param bool $notLogin
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- */
- public function silenceAuth($spread, bool $notLogin = false, string $snsapi = '')
- {
- if ($snsapi) {
- $wechatInfoConfig = $this->getAuthWechatInfo();
- } else {
- $wechatInfoConfig = OfficialAccount::userFromCode();
- }
- $openid = $wechatInfoConfig['openid'];
- try {
- $wechatInfo = OfficialAccount::userService()->get($wechatInfoConfig['openid']);
- } catch (\Throwable $e) {
- $createData = [$openid, [], $spread, '', 'wechat'];
- $userInfoKey = md5($openid . '_' . time() . '_wechat');
- Cache::setTokenBucket($userInfoKey, $createData, 7200);
- return ['auth_login' => 1, 'key' => $userInfoKey];
- }
- /** @var WechatUserServices $wechatUserServices */
- $wechatUserServices = app()->make(WechatUserServices::class);
- $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
- if (!$user) {
- $wechatInfo['headimgurl'] = isset($wechatInfo['headimgurl']) && $wechatInfo['headimgurl'] != '' ? $wechatInfo['headimgurl'] : sys_config('h5_avatar');
- $createData = [$openid, $wechatInfo, $spread, '', 'wechat'];
- //获取是否强制绑定手机号
- $storeUserMobile = sys_config('store_user_mobile');
- if ($notLogin || $storeUserMobile) {
- $userInfoKey = md5($openid . '_' . time() . '_wechat');
- Cache::setTokenBucket($userInfoKey, $createData, 7200);
- return ['auth_login' => 1, 'key' => $userInfoKey];
- } else {
- //写入用户信息
- $user = $wechatUserServices->wechatOauthAfter($createData);
- }
- } else {
- //更新用户信息
- $wechatUserServices->wechatUpdata([$user['uid'], ['spread_uid' => $spread]]);
- }
- return $this->getReturnInfo($user);
- }
- /**
- * 微信公众号静默授权
- * @param $spread
- * @param $phone
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException|\Psr\SimpleCache\InvalidArgumentException
- */
- public function silenceAuthBindingPhone($key, $phone)
- {
- if (!$key) {
- throw new ValidateException('请刷新页面或者重新授权');
- }
- [$openid, $wechatInfo, $spread_uid, $login_type, $userType] = $createData = CacheService::getTokenBucket($key);
- if (!$createData) {
- throw new ValidateException('请刷新页面或者重新授权');
- }
- $wechatInfo['phone'] = $phone;
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- //更新用户信息
- $user = $wechatUser->wechatOauthAfter([$openid, $wechatInfo, $spread_uid, $login_type, $userType]);
- return $this->getReturnInfo($user);
- }
- /**
- * @param array $userData
- * @param string $phone
- * @param string $userType
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- */
- public function appAuth(array $userData, string $phone, string $userType = 'app')
- {
- $openid = $userData['openId'] ?? "";
- $userInfo = [
- 'phone' => $phone,
- 'unionid' => $userData['unionId'] ?? '',
- 'headimgurl' => $userData['avatarUrl'] ?? '',
- 'nickname' => $userData['nickName'] ?? '',
- 'province' => $userData['province'] ?? '',
- 'country' => $userData['country'] ?? '',
- 'city' => $userData['city'] ?? '',
- 'openid' => $openid,
- ];
- $login_type = $userType;
- $spread_uid = $userInfo['spreadId'] ?? "";
- if (!$phone) {
- //获取是否强制绑定手机号
- $storeUserMobile = sys_config('store_user_mobile');
- if ($userInfo['unionid'] && $storeUserMobile) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $uid = $this->dao->value(['unionid' => $userInfo['unionid'], 'is_del' => 0], 'uid');
- $res = $userServices->value(['uid' => $uid], 'phone');
- if (!$uid && !$res) {
- return false;
- }
- }
- if ($openid && $storeUserMobile) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $uid = $this->dao->value(['openid' => $openid, 'is_del' => 0], 'uid');
- $res = $userServices->value(['uid' => $uid], 'phone');
- if (!$uid && !$res) {
- return false;
- }
- }
- }
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- //更新用户信息
- $user = $wechatUser->wechatOauthAfter([$openid, $userInfo, $spread_uid, $login_type, $userType]);
- $token = $this->getReturnInfo($user);
- $token['isbind'] = false;
- return $token;
- }
- /**
- * 是否关注
- * @param int $uid
- * @return bool
- */
- public function isSubscribe(int $uid)
- {
- if ($uid) {
- $subscribe = (bool)$this->dao->value(['uid' => $uid], 'subscribe');
- } else {
- $subscribe = true;
- }
- return $subscribe;
- }
- /**
- * 更新公众号用户信息
- * @param int $uid
- * @return array
- */
- public function updateUserInfo(int $uid)
- {
- $wechatInfoConfig = OfficialAccount::userFromCode();
- $openid = $wechatInfoConfig['openid'] ?? null;
- try {
- $wechatInfo = OfficialAccount::userService()->get($wechatInfoConfig['openid']);
- } catch (\Throwable $e) {
- throw new ValidateException('更新公众号用户信息失败:' . $e->getMessage());
- }
- if (!$openid) {
- throw new ValidateException('更新公众号用户信息失败:没有获取到openid');
- }
- $wechatInfo['nickname'] = $wechatInfoConfig['nickname'] ?? $wechatInfo['nickname'];
- $wechatInfo['headimgurl'] = $wechatInfoConfig['headimgurl'] ?? $wechatInfo['headimgurl'];
- /** @var WechatUserServices $wechatUserServices */
- $wechatUserServices = app()->make(WechatUserServices::class);
- $id = $wechatUserServices->value(['openid' => $openid, 'uid' => $uid, 'user_type' => 'wechat'], 'id');
- if (!$id) {
- throw new ValidateException('没有查到用户信息');
- }
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $user = $userService->getUserInfo($uid);
- if (isset($user['status']) && !$user['status']) {
- throw new ValidateException('您已被禁止登录,请联系管理员');
- }
- if ($user) {
- //更新用户信息
- $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
- }
- return [
- 'nickname' => $wechatInfo['nickname'],
- 'avatar' => $wechatInfo['headimgurl'],
- 'is_complete' => 1
- ];
- }
- }
|