WechatServices.php 16 KB

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