WechatServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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\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 \think\Response
  53. * @throws \EasyWeChat\Server\BadRequestException
  54. */
  55. public function miniServe()
  56. {
  57. ob_clean();
  58. return MiniProgramService::serve();
  59. }
  60. /**
  61. * 支付异步回调
  62. * @return string
  63. * @throws \EasyWeChat\Core\Exceptions\FaultException
  64. */
  65. public function notify()
  66. {
  67. ob_clean();
  68. return WechatAuthService::handleNotify()->getContent();
  69. }
  70. /**
  71. * v3支付回调
  72. * @return string
  73. * @throws \EasyWeChat\Core\Exceptions\FaultException
  74. * @author 等风来
  75. * @email 136327134@qq.com
  76. * @date 2022/9/22
  77. */
  78. public function v3notify()
  79. {
  80. /** @var Pay $pay */
  81. $pay = app()->make(Pay::class, ['v3_wechat_pay']);
  82. return $pay->handleNotify()->getContent();
  83. }
  84. /**
  85. * 公众号权限配置信息获取
  86. * @param $url
  87. * @return mixed
  88. */
  89. public function config($url)
  90. {
  91. return json_decode(WechatAuthService::jsSdk($url), true);
  92. }
  93. /**
  94. * 公众号授权登录,返回token
  95. * @param $spread
  96. * @return array
  97. * @throws \Psr\SimpleCache\InvalidArgumentException
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\DbException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. * @author: 吴汐
  102. * @email: 442384644@qq.com
  103. * @date: 2023/8/12
  104. */
  105. public function authLogin($spread = '')
  106. {
  107. /** @var OAuth $oauth */
  108. $oauth = app()->make(OAuth::class);
  109. $wechatInfo = $oauth->oauth();
  110. if (!isset($wechatInfo['nickname'])) {
  111. $wechatInfo = $oauth->getUserInfo($wechatInfo['openid']);
  112. if (!isset($wechatInfo['nickname']))
  113. throw new ApiException(410131);
  114. if (isset($wechatInfo['tagid_list']))
  115. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  116. } else {
  117. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  118. }
  119. $wechatInfo['user_type'] = 'wechat';
  120. $openid = $wechatInfo['openid'];
  121. /** @var WechatUserServices $wechatUserServices */
  122. $wechatUserServices = app()->make(WechatUserServices::class);
  123. $user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
  124. $createData = [$openid, $wechatInfo, $spread, 'wechat', 'wechat'];
  125. $storeUserMobile = sys_config('store_user_mobile');
  126. if ($storeUserMobile && (($user && $user['phone'] == '') || !$user)) {
  127. $userInfoKey = md5($openid . '_' . time() . '_wechat');
  128. CacheService::set($userInfoKey, $createData, 7200);
  129. return ['bindPhone' => true, 'key' => $userInfoKey];
  130. }
  131. if (!$user) {
  132. $user = $wechatUserServices->wechatOauthAfter($createData);
  133. } else {
  134. $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
  135. }
  136. $token = $this->createToken((int)$user['uid'], 'api');
  137. if ($token) {
  138. app()->make(UserVisitServices::class)->loginSaveVisit($user);
  139. $token['bindPhone'] = false;
  140. return [
  141. 'token' => $token['token'],
  142. 'expires_time' => $token['params']['exp'],
  143. 'bindPhone' => false
  144. ];
  145. } else {
  146. throw new ApiException(410019);
  147. }
  148. }
  149. /**
  150. * 公众号强制绑定手机号
  151. * @param $key
  152. * @param $phone
  153. * @return array
  154. * @throws \Psr\SimpleCache\InvalidArgumentException
  155. * @throws \think\db\exception\DataNotFoundException
  156. * @throws \think\db\exception\DbException
  157. * @throws \think\db\exception\ModelNotFoundException
  158. * @author: 吴汐
  159. * @email: 442384644@qq.com
  160. * @date: 2023/8/12
  161. */
  162. public function authBindingPhone($key, $phone)
  163. {
  164. [$openid, $wechatInfo, $spreadId, $login_type, $userType] = CacheService::get($key);
  165. $wechatInfo['phone'] = $phone;
  166. //写入用户信息
  167. $user = app()->make(WechatUserServices::class)->wechatOauthAfter([$openid, $wechatInfo, $spreadId, $login_type, $userType]);
  168. $token = $this->createToken((int)$user['uid'], 'api');
  169. if ($token) {
  170. app()->make(UserVisitServices::class)->loginSaveVisit($user);
  171. return [
  172. 'token' => $token['token'],
  173. 'expires_time' => $token['params']['exp'],
  174. 'bindName' => false
  175. ];
  176. } else {
  177. throw new ApiException(410019);
  178. }
  179. }
  180. /**
  181. * 获取关注二维码
  182. * @return string[]
  183. * @throws \Exception
  184. */
  185. public function follow()
  186. {
  187. $canvas = Canvas::instance();
  188. $path = 'uploads/follow/';
  189. $imageType = 'jpg';
  190. $name = 'follow';
  191. $siteUrl = sys_config('site_url');
  192. $imageUrl = $path . $name . '.' . $imageType;
  193. $canvas->setImageUrl('statics/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
  194. $wechatQrcode = sys_config('wechat_qrcode');
  195. if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
  196. $wechatQrcode = substr($wechatQrcode, $strlen);
  197. }
  198. if (!$wechatQrcode)
  199. throw new ApiException(410081);
  200. $canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
  201. $image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
  202. return ['path' => $image ? $siteUrl . '/' . $image : ''];
  203. }
  204. /**
  205. * 是否关注
  206. * @param int $uid
  207. * @return bool
  208. */
  209. public function isSubscribe(int $uid)
  210. {
  211. if ($uid) {
  212. $subscribe = (bool)$this->dao->value(['uid' => $uid], 'subscribe');
  213. } else {
  214. $subscribe = true;
  215. }
  216. return $subscribe;
  217. }
  218. /**
  219. * app登录
  220. * @param array $userData
  221. * @param string $phone
  222. * @param string $userType
  223. * @return array|false
  224. * @throws \think\db\exception\DataNotFoundException
  225. * @throws \think\db\exception\ModelNotFoundException
  226. */
  227. public function appAuth(array $userData, string $phone, string $userType = 'app')
  228. {
  229. $openid = $userData['openId'] ?? "";
  230. $userInfo = [
  231. 'phone' => $phone,
  232. 'unionid' => $userData['unionId'] ?? '',
  233. 'headimgurl' => $userData['avatarUrl'] ?? '',
  234. 'nickname' => $userData['nickName'] ?? '',
  235. 'province' => $userData['province'] ?? '',
  236. 'country' => $userData['country'] ?? '',
  237. 'city' => $userData['city'] ?? '',
  238. 'openid' => $openid,
  239. ];
  240. $login_type = $userType;
  241. $spreadId = $userInfo['spreadId'] ?? "";
  242. if (!$phone) {
  243. //获取是否强制绑定手机号
  244. $storeUserMobile = sys_config('store_user_mobile');
  245. if ($userInfo['unionid'] && $storeUserMobile) {
  246. /** @var UserServices $userServices */
  247. $userServices = app()->make(UserServices::class);
  248. $uid = $this->dao->value(['unionid' => $userInfo['unionid'], 'is_del' => 0], 'uid');
  249. $res = $userServices->value(['uid' => $uid, 'is_del' => 0], 'phone');
  250. if (!$uid && !$res) {
  251. return false;
  252. }
  253. } elseif ($openid && $storeUserMobile) {
  254. /** @var UserServices $userServices */
  255. $userServices = app()->make(UserServices::class);
  256. $uid = $this->dao->value(['openid' => $openid], 'uid');
  257. $res = $userServices->value(['uid' => $uid], 'phone');
  258. if (!$uid && !$res) {
  259. return false;
  260. }
  261. }
  262. }
  263. /** @var WechatUserServices $wechatUser */
  264. $wechatUser = app()->make(WechatUserServices::class);
  265. //更新用户信息
  266. $user = $wechatUser->wechatOauthAfter([$openid, $userInfo, $spreadId, $login_type, $userType]);
  267. $token = $this->createToken((int)$user['uid'], 'api');
  268. if ($token) {
  269. /** @var UserVisitServices $visitServices */
  270. $visitServices = app()->make(UserVisitServices::class);
  271. $visitServices->loginSaveVisit($user);
  272. return [
  273. 'token' => $token['token'],
  274. 'userInfo' => $user,
  275. 'expires_time' => $token['params']['exp'],
  276. 'isbind' => false
  277. ];
  278. } else
  279. throw new ApiException(410019);
  280. }
  281. }