MpService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace App\Services;
  3. /**
  4. * 微信服务管理-服务类
  5. * @author laravel开发员
  6. * @since 2020/11/11
  7. * Class WechatService
  8. * @package App\Services
  9. */
  10. class MpService extends BaseService
  11. {
  12. // 静态对象
  13. protected static $instance = null;
  14. protected $debug = true;
  15. protected $expireTime = 7200; // 缓存日志时长
  16. protected $mpAppid = '';
  17. protected $mpAppSecret = '';
  18. // 接口地址
  19. protected $apiUrls = [
  20. // 授权登录
  21. 'auth'=> 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code',
  22. // 授权跳转地址
  23. 'authorize'=>'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect',
  24. // 获取token
  25. 'getToken'=>'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s',
  26. // 获取二维码
  27. 'getQrcode'=>'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s&scene=%s&page=%s&env_version=%s',
  28. // 获取用户信息
  29. 'getUserInfo'=>'https://api.weixin.qq.com/sns/jscode2session',
  30. // 获取手机号
  31. 'getPhoneNumber'=>'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s',
  32. ];
  33. public function __construct()
  34. {
  35. $this->mpAppid = ConfigService::make()->getConfigByCode('wechat_mp_appid');
  36. $this->mpAppSecret = ConfigService::make()->getConfigByCode('wechat_mp_appsecret');
  37. }
  38. /**
  39. * 静态入口
  40. * @return static|null
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = new static();
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * web 授权
  51. * @param $code
  52. * @return array|false|mixed|string|string[]
  53. */
  54. public function auth($code)
  55. {
  56. try {
  57. if(empty($this->mpAppid) || empty($this->mpAppSecret)){
  58. $this->error = '小程序参数未配置';
  59. return false;
  60. }
  61. // 没有code参数
  62. if(empty($code)){
  63. $uri=urlencode(env("WEB_URL")."/v1/mpAuth");
  64. $state=get_random_code(16);
  65. $url= sprintf($this->apiUrls['authorize'], $this->mpAppid, $this->mpAppSecret, $uri, $state);
  66. return redirect($url);
  67. }
  68. $cacheKey = "caches:mpApp:mp_{$this->mpAppid}:";
  69. $url = sprintf($this->apiUrls['auth'],$this->mpAppid, $this->mpAppSecret, $code);
  70. $result = httpRequest($url, '', 'get','',5);
  71. $this->saveLog($cacheKey.'auth:request', ['url'=>$url,'code'=>$code,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  72. if(empty($result)){
  73. $this->error = '授权登录失败';
  74. return "<script>alert('微信授权失败');window.close()</script>";
  75. }
  76. return $result;
  77. }catch (\Exception $e){
  78. $this->error = $e->getMessage();
  79. $this->saveLog($cacheKey.'auth:error', ['code'=>$code,'error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  80. return false;
  81. }
  82. }
  83. /**
  84. * 获取 access_token
  85. * @param false $refresh
  86. * @return false|mixed|string
  87. */
  88. public function getAccessToken($refresh = false)
  89. {
  90. try {
  91. if(empty($this->mpAppid) || empty($this->mpAppSecret)){
  92. $this->error = '小程序参数未配置';
  93. return false;
  94. }
  95. $cacheKey = "caches:mpApp:mp_{$this->mpAppid}:";
  96. $tokenData = RedisService::get($cacheKey.'access_token');
  97. $token = isset($tokenData['access_token'])? $tokenData['access_token'] : '';
  98. if($token && !$refresh){
  99. return $token;
  100. }
  101. $url = sprintf($this->apiUrls['getToken'], $this->mpAppid, $this->mpAppSecret);
  102. $result = httpRequest($url,'', 'get','',5);
  103. $this->saveLog($cacheKey.'tokens:request', ['url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  104. $token = isset($result['access_token'])? $result['access_token'] : '';
  105. if(empty($result) || empty($token)){
  106. $this->error = '获取小程序TOKEN失败';
  107. return false;
  108. }
  109. $result['date'] = date('Y-m-d H:i:s');
  110. RedisService::set($cacheKey.'access_token', $result, 7000);
  111. return $token;
  112. }catch (\Exception $e){
  113. $this->error = $e->getMessage();
  114. $this->saveLog($cacheKey.'tokens:error', ['error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  115. return false;
  116. }
  117. }
  118. /**
  119. * 小程序二维码
  120. * @param $page 页面
  121. * @param $scene 场景参数
  122. * @param string $version 类型:release-永久
  123. * @param false $refresh
  124. * @return false|string
  125. */
  126. public function getMiniQrcode($page, $scene, $version='release', $refresh=false)
  127. {
  128. if (!in_array($version,['release','trial','develop'])) {
  129. $version='release';
  130. }
  131. try {
  132. if(empty($page) || empty($scene)){
  133. $this->error = '缺少二维码参数';
  134. return false;
  135. }
  136. if(empty($this->mpAppid) || empty($this->mpAppSecret)){
  137. $this->error = '小程序参数未配置';
  138. return false;
  139. }
  140. if(!$token = $this->getAccessToken())
  141. {
  142. $this->error = '获取token失败';
  143. return false;
  144. }
  145. $cacheKey = "caches:members:mp_{$this->mpAppid}:";
  146. $filePath = base_path('public/uploads');
  147. $qrFile = '/qrcodes/mp_'.date("YmdHis")."_".md5($page.$scene).".png";
  148. $qrKey = md5(date("Ym").$page.$scene);
  149. if(RedisService::get($cacheKey.$qrKey) && file_exists($filePath.'/'.$qrFile) && !$refresh){
  150. return $qrFile;
  151. }
  152. if(!is_dir($filePath.'/qrcodes/')){
  153. @mkdirs($filePath.'/qrcodes/');
  154. }
  155. $data=['page' => $page,'scene'=>$scene,'check_path'=>false,'env_version'=>$version];
  156. $url = sprintf($this->apiUrls['getQrcode'], $token, $scene, $page, $version);
  157. $result = curl_post($url, json_encode($data));
  158. $datas = $result? json_decode($result, true) : [];
  159. $this->saveLog($cacheKey.'qrcode:request', ['page'=>$page,'scene'=>$scene,'url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  160. $errcode = isset($datas['errcode'])? $datas['errcode'] : '';
  161. $errmsg = isset($datas['errmsg'])? $datas['errmsg'] : '';
  162. if($errcode){
  163. $this->error = $errmsg? $errmsg : '获取二维码失败';
  164. return false;
  165. }
  166. file_put_contents($filePath.'/'.$qrFile, $result);
  167. if(!file_exists($filePath.'/'.$qrFile)){
  168. $this->error = '生成二维码失败';
  169. return false;
  170. }
  171. RedisService::set($cacheKey.$qrKey, ['page'=>$page,'scene'=>$scene,'qrcode'=>$qrFile,'date'=>date('Y-m-d H:i:s')], 30 * 86400);
  172. return $qrFile;
  173. }catch (\Exception $e){
  174. $this->error = $e->getMessage();
  175. $this->saveLog($cacheKey.'qrcode:error', ['page'=>$page,'scene'=>$scene,'error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  176. return false;
  177. }
  178. }
  179. /**
  180. * 获取用户信息
  181. * @param $code
  182. * @return array|false|mixed|string[]
  183. */
  184. public function getUserinfo($code)
  185. {
  186. try {
  187. if(empty($code)){
  188. $this->error = '缺少授权参数';
  189. return false;
  190. }
  191. if(empty($this->mpAppid) || empty($this->mpAppSecret)){
  192. $this->error = '小程序参数未配置';
  193. return false;
  194. }
  195. $data=[
  196. 'appid' => $this->mpAppid,
  197. 'secret'=> $this->mpAppSecret,
  198. 'js_code'=>$code,
  199. 'grant_type'=>'authorization_code'
  200. ];
  201. $cacheKey = "caches:mpApp:mp_{$this->mpAppid}:";
  202. $url = $this->apiUrls['getUserInfo'];
  203. $result = httpRequest($url, $data,'get','',5);
  204. $this->saveLog($cacheKey.'userInfo:request', ['code'=>$code,'url'=>$url,'query'=>$data,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  205. if(empty($result)){
  206. $this->error = '获取用户信息失败';
  207. return false;
  208. }
  209. return $result;
  210. }catch (\Exception $e){
  211. $this->error = $e->getMessage();
  212. $this->saveLog($cacheKey.'userInfo:error', ['code'=>$code,'error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  213. return false;
  214. }
  215. }
  216. /**
  217. * 获取用户手机号码
  218. * @param $code
  219. * @return array|false|mixed|string[]
  220. */
  221. public function getPhoneNumber($code)
  222. {
  223. try {
  224. if(empty($code)){
  225. $this->error = '缺少授权参数';
  226. return false;
  227. }
  228. if(empty($this->mpAppid) || empty($this->mpAppSecret)){
  229. $this->error = '小程序参数未配置';
  230. return false;
  231. }
  232. if(!$token = $this->getAccessToken())
  233. {
  234. $this->error = '获取token失败';
  235. return false;
  236. }
  237. $cacheKey = "caches:mpApp:mp_{$this->mpAppid}:";
  238. $url = sprintf($this->apiUrls['getPhoneNumber'], $token);
  239. $result = httpRequest($url, json_encode(['code'=>$code],256),'post','',5);
  240. $this->saveLog($cacheKey.'phone:request', ['code'=>$code,'url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  241. if(empty($result)){
  242. $this->error = '获取用户手机号失败';
  243. return false;
  244. }
  245. return $result;
  246. }catch (\Exception $e){
  247. $this->error = $e->getMessage();
  248. $this->saveLog($cacheKey.'phone:error', ['code'=>$code,'error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  249. return false;
  250. }
  251. }
  252. /**
  253. * 获取 客服接口 access_token
  254. * @param false $refresh
  255. * @return false|mixed|string
  256. */
  257. public function getServiceToken($refresh = false)
  258. {
  259. try {
  260. if(empty($this->mpAppid) || empty($this->mpAppSecret)){
  261. $this->error = '小程序参数未配置';
  262. return false;
  263. }
  264. $cacheKey = "caches:mpApp:mp_{$this->mpAppid}:";
  265. $tokenData = RedisService::get($cacheKey.'kf_access_token');
  266. $token = isset($tokenData['access_token'])? $tokenData['access_token'] : '';
  267. if($token && !$refresh){
  268. return $token;
  269. }
  270. $url = sprintf($this->apiUrls['getServiceToken'], $this->mpAppid, $this->mpAppSecret);
  271. $result = httpRequest($url,'', 'get','',5);
  272. $this->saveLog($cacheKey.'kfTokens:request', ['url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  273. $token = isset($result['access_token'])? $result['access_token'] : '';
  274. if(empty($result) || empty($token)){
  275. $this->error = '获取小程序客服TOKEN失败';
  276. return false;
  277. }
  278. $result['date'] = date('Y-m-d H:i:s');
  279. RedisService::set($cacheKey.'kf_access_token', $result, 7000);
  280. return $token;
  281. }catch (\Exception $e){
  282. $this->error = $e->getMessage();
  283. $this->saveLog($cacheKey.'kfTokens:error', ['error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  284. return false;
  285. }
  286. }
  287. /**
  288. * 获取客服地址
  289. * @return array|false|mixed|string[]
  290. */
  291. public function getServiceUrl()
  292. {
  293. try {
  294. if(empty($this->mpAppid) || empty($this->mpAppSecret)){
  295. $this->error = '小程序参数未配置';
  296. return false;
  297. }
  298. if(!$token = $this->getServiceToken())
  299. {
  300. $this->error = '获取token失败';
  301. return false;
  302. }
  303. $cacheKey = "caches:mpApp:mp_{$this->mpAppid}:";
  304. $url = sprintf($this->apiUrls['getServiceUrl'], $token);
  305. $result = httpRequest($url, '','post','',5);
  306. $this->saveLog($cacheKey.'service:request', ['url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  307. if(empty($result)){
  308. $this->error = '获取小程序客服地址失败';
  309. return false;
  310. }
  311. return $result;
  312. }catch (\Exception $e){
  313. $this->error = $e->getMessage();
  314. $this->saveLog($cacheKey.'service:error', ['error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  315. return false;
  316. }
  317. }
  318. /**
  319. * 检验数据的真实性,并且获取解密后的明文.
  320. * @param $encryptedData string 加密的用户数据
  321. * @param $iv string 与用户数据一同返回的初始向量
  322. * @param $sessionKey string 解密会话KEY
  323. *
  324. * @return int 成功0,失败返回对应的错误码
  325. */
  326. public function decryptData($encryptedData, $iv, $sessionKey)
  327. {
  328. if (strlen($sessionKey) != 24) {
  329. $this->error = -41001;
  330. return false;
  331. }
  332. $aesKey=base64_decode($sessionKey);
  333. if (strlen($iv) != 24) {
  334. $this->error = -41002;
  335. return false;
  336. }
  337. $aesIV=base64_decode($iv);
  338. $aesCipher=base64_decode($encryptedData);
  339. $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  340. $dataObj=json_decode( $result);
  341. if( $dataObj == NULL )
  342. {
  343. $this->error = -41003;
  344. return false;
  345. }
  346. if( $dataObj->watermark->appid != $this->mpAppid)
  347. {
  348. $this->error = -41003;
  349. return false;
  350. }
  351. return $dataObj;
  352. }
  353. /**
  354. * 保存日志
  355. * @param $cackekey
  356. * @param $data
  357. * @param $time
  358. */
  359. public function saveLog($cackekey, $data, $time=0)
  360. {
  361. if($this->debug){
  362. RedisService::set($cackekey, $data, $time?$time : $this->expireTime);
  363. }
  364. }
  365. }