MpService.php 13 KB

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