WechatServices.php 16 KB

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