MpService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. // 获取客服token
  33. 'getServiceToken'=>'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s',
  34. // 获取客服地址
  35. 'getServiceUrl'=>'https://qyapi.weixin.qq.com/cgi-bin/kf/add_contact_way?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:mpQrcode:mp_{$this->mpAppid}:";
  150. $filePath = base_path('public/uploads');
  151. $qrFile = '/qrcodes/mp_'.date("YmdHis")."_".md5($page.$scene).".png";
  152. $qrKey = md5($qrFile);
  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. $url = sprintf($this->apiUrls['getQrcode'], $token, $scene, $page, $version);
  160. $result = curl_post($url, '');
  161. $datas = $result? json_decode($result, true) : [];
  162. $this->saveLog($cacheKey.'qrcode:request', ['page'=>$page,'scene'=>$scene,'url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  163. $errcode = isset($datas['errcode'])? $datas['errcode'] : '';
  164. $errmsg = isset($datas['errmsg'])? $datas['errmsg'] : '';
  165. if($errcode){
  166. $this->error = $errmsg? $errmsg : '获取二维码失败';
  167. return false;
  168. }
  169. file_put_contents($filePath.'/'.$qrFile, $result);
  170. if(!file_exists($filePath.'/'.$qrFile)){
  171. $this->error = '保存二维码失败';
  172. return false;
  173. }
  174. $result['date'] = date('Y-m-d H:i:s');
  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. var_dump($result);
  277. $this->saveLog($cacheKey.'kfTokens:request', ['url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  278. $token = isset($result['access_token'])? $result['access_token'] : '';
  279. if(empty($result) || empty($token)){
  280. $this->error = '获取小程序客服TOKEN失败';
  281. return false;
  282. }
  283. $result['date'] = date('Y-m-d H:i:s');
  284. RedisService::set($cacheKey.'kf_access_token', $result, 7000);
  285. return $token;
  286. }catch (\Exception $e){
  287. $this->error = $e->getMessage();
  288. $this->saveLog($cacheKey.'kfTokens:error', ['error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  289. return false;
  290. }
  291. }
  292. /**
  293. * 获取客服地址
  294. * @return array|false|mixed|string[]
  295. */
  296. public function getServiceUrl()
  297. {
  298. try {
  299. if(empty($this->mpAppid) || empty($this->mpAppSecret)){
  300. $this->error = '小程序参数未配置';
  301. return false;
  302. }
  303. if(!$token = $this->getServiceToken())
  304. {
  305. $this->error = '获取token失败';
  306. return false;
  307. }
  308. $cacheKey = "caches:mpApp:mp_{$this->mpAppid}:";
  309. $url = sprintf($this->apiUrls['getServiceUrl'], $token);
  310. $result = httpRequest($url, '','post','',5);
  311. $this->saveLog($cacheKey.'service:request', ['url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
  312. if(empty($result)){
  313. $this->error = '获取小程序客服地址失败';
  314. return false;
  315. }
  316. return $result;
  317. }catch (\Exception $e){
  318. $this->error = $e->getMessage();
  319. $this->saveLog($cacheKey.'service:error', ['error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
  320. return false;
  321. }
  322. }
  323. /**
  324. * 保存日志
  325. * @param $cackekey
  326. * @param $data
  327. * @param $time
  328. */
  329. public function saveLog($cackekey, $data, $time=0)
  330. {
  331. if($this->debug){
  332. RedisService::set($cackekey, $data, $time?$time : $this->expireTime);
  333. }
  334. }
  335. }