MpService.php 15 KB

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