MpService.php 16 KB

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