MpService.php 16 KB

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