WechatServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\wechat;
  13. use app\services\BaseServices;
  14. use app\dao\wechat\WechatUserDao;
  15. use app\services\user\UserServices;
  16. use app\services\user\UserVisitServices;
  17. use crmeb\exceptions\ApiException;
  18. use crmeb\services\CacheService;
  19. use crmeb\services\CacheService as Cache;
  20. use crmeb\services\app\WechatService as WechatAuthService;
  21. use crmeb\services\oauth\OAuth;
  22. use crmeb\services\pay\Pay;
  23. use crmeb\utils\Canvas;
  24. /**
  25. *
  26. * Class WechatServices
  27. * @package app\services\wechat
  28. * @method value(array $where, ?string $field)
  29. */
  30. class WechatServices extends BaseServices
  31. {
  32. /**
  33. * WechatServices constructor.
  34. * @param WechatUserDao $dao
  35. */
  36. public function __construct(WechatUserDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 微信公众号服务
  42. * @return \think\Response
  43. * @throws \EasyWeChat\Server\BadRequestException
  44. */
  45. public function serve()
  46. {
  47. ob_clean();
  48. return WechatAuthService::serve();
  49. }
  50. /**
  51. * 支付异步回调
  52. * @return string
  53. * @throws \EasyWeChat\Core\Exceptions\FaultException
  54. */
  55. public function notify()
  56. {
  57. ob_clean();
  58. return WechatAuthService::handleNotify()->getContent();
  59. }
  60. /**
  61. * v3支付回调
  62. * @return string
  63. * @throws \EasyWeChat\Core\Exceptions\FaultException
  64. * @author 等风来
  65. * @email 136327134@qq.com
  66. * @date 2022/9/22
  67. */
  68. public function v3notify()
  69. {
  70. /** @var Pay $pay */
  71. $pay = app()->make(Pay::class, ['v3_wechat_pay']);
  72. return $pay->handleNotify()->getContent();
  73. }
  74. /**
  75. * 公众号权限配置信息获取
  76. * @param $url
  77. * @return mixed
  78. */
  79. public function config($url)
  80. {
  81. return json_decode(WechatAuthService::jsSdk($url), true);
  82. }
  83. /**
  84. * 公众号授权登陆
  85. * @return mixed
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public function auth($spreadId, $login_type)
  91. {
  92. /** @var OAuth $oauth */
  93. $oauth = app()->make(OAuth::class);
  94. $wechatInfo = $oauth->oauth();
  95. if (!isset($wechatInfo['nickname'])) {
  96. $wechatInfo = $oauth->getUserInfo($wechatInfo['openid']);
  97. if (!isset($wechatInfo['nickname']))
  98. throw new ApiException(410131);
  99. if (isset($wechatInfo['tagid_list']))
  100. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  101. } else {
  102. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  103. /** @var WechatUserServices $wechatUser */
  104. $wechatUser = app()->make(WechatUserServices::class);
  105. if (!$wechatUser->getOne(['openid' => $wechatInfo['openid']])) {
  106. $wechatInfo['subscribe'] = 0;
  107. }
  108. }
  109. $wechatInfo['user_type'] = 'wechat';
  110. $openid = $wechatInfo['openid'];
  111. /** @var WechatUserServices $wechatUserServices */
  112. $wechatUserServices = app()->make(WechatUserServices::class);
  113. $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
  114. $createData = [$openid, $wechatInfo, $spreadId, $login_type, 'wechat'];
  115. if (!$user) {
  116. $user = $wechatUserServices->wechatOauthAfter($createData);
  117. } else {
  118. //更新用户信息
  119. $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
  120. }
  121. $token = $this->createToken((int)$user['uid'], 'wechat');
  122. if ($token) {
  123. /** @var UserVisitServices $visitServices */
  124. $visitServices = app()->make(UserVisitServices::class);
  125. $visitServices->loginSaveVisit($user);
  126. return ['userInfo' => $user];
  127. } else
  128. throw new ApiException(410019);
  129. }
  130. /**
  131. * 新公众号授权登录
  132. * @return mixed
  133. * @throws \think\db\exception\DataNotFoundException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. * @throws \think\exception\DbException
  136. */
  137. public function newAuth($spreadId, $login_type)
  138. {
  139. /** @var OAuth $oauth */
  140. $oauth = app()->make(OAuth::class);
  141. $wechatInfo = $oauth->oauth();
  142. if (!isset($wechatInfo['nickname'])) {
  143. $wechatInfo = $oauth->getUserInfo($wechatInfo['openid']);
  144. if (!isset($wechatInfo['nickname']))
  145. throw new ApiException(410131);
  146. if (isset($wechatInfo['tagid_list']))
  147. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  148. } else {
  149. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  150. }
  151. $wechatInfo['user_type'] = 'wechat';
  152. $openid = $wechatInfo['openid'];
  153. /** @var WechatUserServices $wechatUserServices */
  154. $wechatUserServices = app()->make(WechatUserServices::class);
  155. $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
  156. $createData = [$openid, $wechatInfo, $spreadId, $login_type, 'wechat'];
  157. //获取是否强制绑定手机号
  158. $storeUserMobile = sys_config('store_user_mobile');
  159. if ($storeUserMobile && !$user) {
  160. $userInfoKey = md5($openid . '_' . time() . '_wechat');
  161. Cache::setTokenBucket($userInfoKey, $createData, 7200);
  162. return ['key' => $userInfoKey];
  163. } else if (!$user) {
  164. $user = $wechatUserServices->wechatOauthAfter($createData);
  165. } else {
  166. //更新用户信息
  167. $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
  168. }
  169. $token = $this->createToken((int)$user['uid'], 'wechat');
  170. if ($token) {
  171. /** @var UserVisitServices $visitServices */
  172. $visitServices = app()->make(UserVisitServices::class);
  173. $visitServices->loginSaveVisit($user);
  174. $token['userInfo'] = $user;
  175. return $token;
  176. } else
  177. throw new ApiException(410019);
  178. }
  179. /**
  180. * 获取关注二维码
  181. * @return string[]
  182. * @throws \Exception
  183. */
  184. public function follow()
  185. {
  186. $canvas = Canvas::instance();
  187. $path = 'uploads/follow/';
  188. $imageType = 'jpg';
  189. $name = 'follow';
  190. $siteUrl = sys_config('site_url');
  191. $imageUrl = $path . $name . '.' . $imageType;
  192. $canvas->setImageUrl('statics/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
  193. $wechatQrcode = sys_config('wechat_qrcode');
  194. if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
  195. $wechatQrcode = substr($wechatQrcode, $strlen);
  196. }
  197. if (!$wechatQrcode)
  198. throw new ApiException(410081);
  199. $canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
  200. $image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
  201. return ['path' => $image ? $siteUrl . '/' . $image : ''];
  202. }
  203. /**
  204. * 微信公众号静默授权
  205. * @param $spread
  206. * @return array
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. */
  210. public function silenceAuth($spread)
  211. {
  212. /** @var OAuth $oauth */
  213. $oauth = app()->make(OAuth::class);
  214. $wechatInfoConfig = $oauth->oauth();
  215. $wechatInfo = $oauth->getUserInfo($wechatInfoConfig['openid']);
  216. $openid = $wechatInfoConfig['openid'];
  217. /** @var WechatUserServices $wechatUserServices */
  218. $wechatUserServices = app()->make(WechatUserServices::class);
  219. $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
  220. $wechatInfo['headimgurl'] = isset($wechatInfo['headimgurl']) && $wechatInfo['headimgurl'] != '' ? $wechatInfo['headimgurl'] : sys_config('h5_avatar');
  221. $createData = [$openid, $wechatInfo, $spread, '', 'wechat'];
  222. //获取是否强制绑定手机号
  223. $storeUserMobile = sys_config('store_user_mobile');
  224. if ($storeUserMobile && !$user) {
  225. $userInfoKey = md5($openid . '_' . time() . '_wechat');
  226. Cache::setTokenBucket($userInfoKey, $createData, 7200);
  227. return ['key' => $userInfoKey];
  228. } else if (!$user) {
  229. //写入用户信息
  230. $user = $wechatUserServices->wechatOauthAfter($createData);
  231. $token = $this->createToken((int)$user['uid'], 'wechat');
  232. if ($token) {
  233. /** @var UserVisitServices $visitServices */
  234. $visitServices = app()->make(UserVisitServices::class);
  235. $visitServices->loginSaveVisit($user);
  236. return $token;
  237. } else
  238. throw new ApiException(410019);
  239. } else {
  240. //更新用户信息
  241. $wechatUserServices->wechatUpdata([$user['uid'], ['code' => $spread]]);
  242. $token = $this->createToken((int)$user['uid'], 'wechat');
  243. if ($token) {
  244. /** @var UserVisitServices $visitServices */
  245. $visitServices = app()->make(UserVisitServices::class);
  246. $visitServices->loginSaveVisit($user);
  247. return $token;
  248. } else
  249. throw new ApiException(410019);
  250. }
  251. }
  252. /**
  253. * 微信公众号静默授权(不登录注册用户)
  254. * @param $spread
  255. * @return array
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. public function silenceAuthNoLogin($spread)
  260. {
  261. /** @var OAuth $oauth */
  262. $oauth = app()->make(OAuth::class);
  263. $wechatInfoConfig = $oauth->oauth();
  264. $openid = $wechatInfoConfig['openid'];
  265. try {
  266. $wechatInfo = $oauth->getUserInfo($wechatInfoConfig['openid']);
  267. } catch (\Throwable $e) {
  268. $createData = [$openid, [], $spread, '', 'wechat'];
  269. $userInfoKey = md5($openid . '_' . time() . '_wechat');
  270. Cache::setTokenBucket($userInfoKey, $createData, 7200);
  271. return ['auth_login' => 1, 'key' => $userInfoKey];
  272. }
  273. /** @var WechatUserServices $wechatUserServices */
  274. $wechatUserServices = app()->make(WechatUserServices::class);
  275. $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
  276. $createData = [$openid, $wechatInfo, $spread, '', 'wechat'];
  277. $userInfoKey = md5($openid . '_' . time() . '_wechat');
  278. Cache::setTokenBucket($userInfoKey, $createData, 7200);
  279. return ['auth_login' => 1, 'key' => $userInfoKey];
  280. // if (!$user) {//没有注册用户也没有强制手机号绑定 返回让微信授权登录
  281. // $createData = [$openid, $wechatInfo, $spread, '', 'wechat'];
  282. // $userInfoKey = md5($openid . '_' . time() . '_wechat');
  283. // Cache::setTokenBucket($userInfoKey, $createData, 7200);
  284. // return ['auth_login' => 1, 'key' => $userInfoKey];
  285. // } else {
  286. // //更新用户信息
  287. // $wechatUserServices->wechatUpdata([$user['uid'], ['code' => $spread]]);
  288. // $token = $this->createToken((int)$user['uid'], 'wechat');
  289. // if ($token) {
  290. // /** @var UserVisitServices $visitServices */
  291. // $visitServices = app()->make(UserVisitServices::class);
  292. // $visitServices->loginSaveVisit($user);
  293. // $token['userInfo'] = $user;
  294. // return $token;
  295. // } else
  296. // throw new ApiException(410019);
  297. // }
  298. }
  299. /**
  300. * 微信公众号静默授权
  301. * @param $key
  302. * @param $phone
  303. * @return array
  304. * @throws \Psr\SimpleCache\InvalidArgumentException
  305. * @throws \think\db\exception\DataNotFoundException
  306. * @throws \think\db\exception\ModelNotFoundException
  307. */
  308. public function silenceAuthBindingPhone($key, $phone)
  309. {
  310. if (!$key) {
  311. throw new ApiException(410037);
  312. }
  313. [$openid, $wechatInfo, $spreadId, $login_type, $userType] = $createData = CacheService::getTokenBucket($key);
  314. if (!$createData) {
  315. throw new ApiException(410037);
  316. }
  317. $wechatInfo['phone'] = $phone;
  318. /** @var WechatUserServices $wechatUser */
  319. $wechatUser = app()->make(WechatUserServices::class);
  320. //更新用户信息
  321. $user = $wechatUser->wechatOauthAfter([$openid, $wechatInfo, $spreadId, $login_type, $userType]);
  322. $token = $this->createToken((int)$user['uid'], 'wechat');
  323. if ($token) {
  324. /** @var UserVisitServices $visitServices */
  325. $visitServices = app()->make(UserVisitServices::class);
  326. $visitServices->loginSaveVisit($user);
  327. return [
  328. 'token' => $token['token'],
  329. 'userInfo' => $user,
  330. 'expires_time' => $token['params']['exp'],
  331. ];
  332. } else
  333. throw new ApiException(410019);
  334. }
  335. /**
  336. * 是否关注
  337. * @param int $uid
  338. * @return bool
  339. */
  340. public function isSubscribe(int $uid)
  341. {
  342. if ($uid) {
  343. $subscribe = (bool)$this->dao->value(['uid' => $uid], 'subscribe');
  344. } else {
  345. $subscribe = true;
  346. }
  347. return $subscribe;
  348. }
  349. /**
  350. * app登录
  351. * @param array $userData
  352. * @param string $phone
  353. * @param string $userType
  354. * @return array|false
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\ModelNotFoundException
  357. */
  358. public function appAuth(array $userData, string $phone, string $userType = 'app')
  359. {
  360. $openid = $userData['openId'] ?? "";
  361. $userInfo = [
  362. 'phone' => $phone,
  363. 'unionid' => $userData['unionId'] ?? '',
  364. 'headimgurl' => $userData['avatarUrl'] ?? '',
  365. 'nickname' => $userData['nickName'] ?? '',
  366. 'province' => $userData['province'] ?? '',
  367. 'country' => $userData['country'] ?? '',
  368. 'city' => $userData['city'] ?? '',
  369. 'openid' => $openid,
  370. ];
  371. $login_type = $userType;
  372. $spreadId = $userInfo['spreadId'] ?? "";
  373. if (!$phone) {
  374. //获取是否强制绑定手机号
  375. $storeUserMobile = sys_config('store_user_mobile');
  376. if ($userInfo['unionid'] && $storeUserMobile) {
  377. /** @var UserServices $userServices */
  378. $userServices = app()->make(UserServices::class);
  379. $uid = $this->dao->value(['unionid' => $userInfo['unionid']], 'uid');
  380. $res = $userServices->value(['uid' => $uid], 'phone');
  381. if (!$uid && !$res) {
  382. return false;
  383. }
  384. }
  385. if ($openid && $storeUserMobile) {
  386. /** @var UserServices $userServices */
  387. $userServices = app()->make(UserServices::class);
  388. $uid = $this->dao->value(['openid' => $openid], 'uid');
  389. $res = $userServices->value(['uid' => $uid], 'phone');
  390. if (!$uid && !$res) {
  391. return false;
  392. }
  393. }
  394. }
  395. /** @var WechatUserServices $wechatUser */
  396. $wechatUser = app()->make(WechatUserServices::class);
  397. //更新用户信息
  398. $user = $wechatUser->wechatOauthAfter([$openid, $userInfo, $spreadId, $login_type, $userType]);
  399. $token = $this->createToken((int)$user['uid'], 'wechat');
  400. if ($token) {
  401. /** @var UserVisitServices $visitServices */
  402. $visitServices = app()->make(UserVisitServices::class);
  403. $visitServices->loginSaveVisit($user);
  404. return [
  405. 'token' => $token['token'],
  406. 'userInfo' => $user,
  407. 'expires_time' => $token['params']['exp'],
  408. 'isbind' => false
  409. ];
  410. } else
  411. throw new ApiException(410019);
  412. }
  413. }