WechatServices.php 16 KB

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