WechatServices.php 12 KB

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