WechatServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 = '', $agent_id = '')
  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, $agent_id, '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. $user = $wechatUserServices->wechatOauthAfter($createData);
  132. $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
  133. $token = $this->createToken((int)$user['uid'], 'api');
  134. if ($token) {
  135. app()->make(UserVisitServices::class)->loginSaveVisit($user);
  136. $token['bindPhone'] = false;
  137. return [
  138. 'token' => $token['token'],
  139. 'expires_time' => $token['params']['exp'],
  140. 'bindPhone' => false
  141. ];
  142. } else {
  143. throw new ApiException(410019);
  144. }
  145. }
  146. /**
  147. * 公众号强制绑定手机号
  148. * @param $key
  149. * @param $phone
  150. * @return array
  151. * @throws \Psr\SimpleCache\InvalidArgumentException
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\DbException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. * @author: 吴汐
  156. * @email: 442384644@qq.com
  157. * @date: 2023/8/12
  158. */
  159. public function authBindingPhone($key, $phone)
  160. {
  161. [$openid, $wechatInfo, $spreadId, $agent_id, $login_type, $userType] = CacheService::get($key);
  162. $wechatInfo['phone'] = $phone;
  163. //写入用户信息
  164. $user = app()->make(WechatUserServices::class)->wechatOauthAfter([$openid, $wechatInfo, $spreadId, $agent_id, $login_type, $userType]);
  165. $token = $this->createToken((int)$user['uid'], 'api');
  166. if ($token) {
  167. app()->make(UserVisitServices::class)->loginSaveVisit($user);
  168. return [
  169. 'token' => $token['token'],
  170. 'expires_time' => $token['params']['exp'],
  171. 'bindName' => false
  172. ];
  173. } else {
  174. throw new ApiException(410019);
  175. }
  176. }
  177. /**
  178. * 获取关注二维码
  179. * @return string[]
  180. * @throws \Exception
  181. */
  182. public function follow()
  183. {
  184. $canvas = Canvas::instance();
  185. $path = 'uploads/follow/';
  186. $imageType = 'jpg';
  187. $name = 'follow';
  188. $siteUrl = sys_config('site_url');
  189. $imageUrl = $path . $name . '.' . $imageType;
  190. $canvas->setImageUrl('statics/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
  191. $wechatQrcode = sys_config('wechat_qrcode');
  192. if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
  193. $wechatQrcode = substr($wechatQrcode, $strlen);
  194. }
  195. if (!$wechatQrcode)
  196. throw new ApiException(410081);
  197. $canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
  198. $image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
  199. return ['path' => $image ? $siteUrl . '/' . $image : ''];
  200. }
  201. /**
  202. * 是否关注
  203. * @param int $uid
  204. * @return bool
  205. */
  206. public function isSubscribe(int $uid)
  207. {
  208. if ($uid) {
  209. $subscribe = (bool)$this->dao->value(['uid' => $uid], 'subscribe');
  210. } else {
  211. $subscribe = true;
  212. }
  213. return $subscribe;
  214. }
  215. /**
  216. * app登录
  217. * @param array $userData
  218. * @param string $phone
  219. * @param string $userType
  220. * @return array|false
  221. * @throws \think\db\exception\DataNotFoundException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. */
  224. public function appAuth(array $userData, string $phone, string $userType = 'app')
  225. {
  226. $openid = $userData['openId'] ?? "";
  227. $userInfo = [
  228. 'phone' => $phone,
  229. 'unionid' => $userData['unionId'] ?? '',
  230. 'headimgurl' => $userData['avatarUrl'] ?? '',
  231. 'nickname' => $userData['nickName'] ?? '',
  232. 'province' => $userData['province'] ?? '',
  233. 'country' => $userData['country'] ?? '',
  234. 'city' => $userData['city'] ?? '',
  235. 'openid' => $openid,
  236. ];
  237. $login_type = $userType;
  238. $spreadId = $userInfo['spreadId'] ?? "";
  239. if (!$phone) {
  240. //获取是否强制绑定手机号
  241. $storeUserMobile = sys_config('store_user_mobile');
  242. if ($userInfo['unionid'] && $storeUserMobile) {
  243. /** @var UserServices $userServices */
  244. $userServices = app()->make(UserServices::class);
  245. $uid = $this->dao->value(['unionid' => $userInfo['unionid'], 'is_del' => 0], 'uid');
  246. $res = $userServices->value(['uid' => $uid, 'is_del' => 0], 'phone');
  247. if (!$uid && !$res) {
  248. return false;
  249. }
  250. } elseif ($openid && $storeUserMobile) {
  251. /** @var UserServices $userServices */
  252. $userServices = app()->make(UserServices::class);
  253. $uid = $this->dao->value(['openid' => $openid], 'uid');
  254. $res = $userServices->value(['uid' => $uid], 'phone');
  255. if (!$uid && !$res) {
  256. return false;
  257. }
  258. }
  259. }
  260. /** @var WechatUserServices $wechatUser */
  261. $wechatUser = app()->make(WechatUserServices::class);
  262. //更新用户信息
  263. $user = $wechatUser->wechatOauthAfter([$openid, $userInfo, $spreadId, 0, $login_type, $userType]);
  264. $token = $this->createToken((int)$user['uid'], 'api');
  265. if ($token) {
  266. /** @var UserVisitServices $visitServices */
  267. $visitServices = app()->make(UserVisitServices::class);
  268. $visitServices->loginSaveVisit($user);
  269. return [
  270. 'token' => $token['token'],
  271. 'userInfo' => $user,
  272. 'expires_time' => $token['params']['exp'],
  273. 'isbind' => false
  274. ];
  275. } else
  276. throw new ApiException(410019);
  277. }
  278. }