MpService.php 18 KB

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