weixina.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\lib;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-11-10 13:21
  12. // +----------------------------------------------------------------------
  13. class weixina {
  14. //AppID(应用ID)
  15. private $appid = '';
  16. //AppSecret(应用密钥)
  17. private $secret = "";
  18. //授权回调URL
  19. private $redirect_uri = '';
  20. //返回类型,请填写code
  21. private $response_type = 'code';
  22. /**
  23. * 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息
  24. */
  25. private $scope ='snsapi_userinfo';
  26. private $state = 'STATE';
  27. public $error="";
  28. /**
  29. * 构造函数
  30. */
  31. public function __construct($info = null) {
  32. if(empty($info)) $info = config('weixin');
  33. $this->appid = $info['APPID'];
  34. $this->secret = $info['APPSECRET'];
  35. $this->redirect_uri = $info['REDIRECT'] ;// config() .'api/weixin/result';
  36. }
  37. /**
  38. * 新版授权登录
  39. * @param type $jsCode
  40. */
  41. public function auth_code2Session($jsCode){
  42. $oauth_url = "https://api.weixin.qq.com/sns/jscode2session?"
  43. ."appid=".$this->appid
  44. ."&secret=".$this->secret
  45. ."&js_code=".$jsCode
  46. ."&grant_type=authorization_code";
  47. $data = $this->Get($oauth_url);
  48. if(empty($data)){
  49. return false;
  50. }
  51. return json_decode($data,true);
  52. }
  53. /**
  54. * 新版获取微信手机号
  55. * @param type $jsCode
  56. */
  57. public function getPhoneNumber($jsCode){
  58. $accessData = $this->getAccessToken();
  59. if(empty($accessData) || empty($accessData["access_token"])){
  60. return false;
  61. }
  62. $access_token = $accessData["access_token"];
  63. $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?"
  64. ."access_token=".$access_token;
  65. $postData = json_encode(array(
  66. "code"=>$jsCode
  67. ));
  68. $data = $this->Post($postData,$url);
  69. $res = $data;
  70. if(empty($data)){
  71. return false;
  72. }
  73. $data = json_decode($data,true);
  74. if(empty($data) || empty($data["phone_info"]) || empty($data["phone_info"]["purePhoneNumber"])){
  75. $this->error="返回参数名称错误".$res;
  76. return false;
  77. }
  78. return $data["phone_info"];
  79. }
  80. /**
  81. * 获取小程序码
  82. * @param type $path
  83. * @param type $width
  84. */
  85. public function getQRCode($path,$width){
  86. }
  87. /**
  88. * 获取不限制的小程序码
  89. * @param type $page 页面
  90. * @param type $scene 携带参数
  91. * @param type $width 宽度
  92. * @return bool
  93. */
  94. public function getUnlimitedQRCode($page,$scene,$width=430){
  95. $accessData = $this->getAccessToken();
  96. if(empty($accessData) || empty($accessData["access_token"])){
  97. return false;
  98. }
  99. $access_token = $accessData["access_token"];
  100. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?"
  101. ."access_token=".$access_token;
  102. $postData = json_encode(array(
  103. "page"=>$page,
  104. "scene"=>$scene,
  105. "width"=>$width,//默认430
  106. // "auto_color"=>true,//自动配置线条颜色
  107. "is_hyaline" => false,//是否透明底色
  108. ));
  109. $data = $this->Post($postData,$url,1);
  110. $res = $data;
  111. if(empty($data)){
  112. return false;
  113. }
  114. if(empty($data) || empty($data["contentType"]) || empty($data["buffer"])){
  115. $this->error="返回参数错误".$res;
  116. return false;
  117. }
  118. return $data;
  119. // return $data["phone_info"];
  120. // $key = md5($this->appid.$parms['path'].$parms['params']);
  121. // $imgsrc = Factory::cache('default')->hget($this->pathAndWxacodeunlimitKey, $key);
  122. // if(!empty($imgsrc)){
  123. // return ResultWrapper::success($imgsrc);
  124. // }
  125. //
  126. // $apiUrl = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
  127. //
  128. // $postData = [
  129. // 'scene' => $parms['params'],
  130. // 'page' => $parms['path'],
  131. // 'width' => 430,
  132. // 'auto_color' => true,
  133. // ];
  134. //
  135. // $reponse = request($apiUrl, json_encode($postData));
  136. // if($reponse['httpcode'] != 200){
  137. // return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  138. // }else{
  139. // if(!is_object($reponse['content'])){
  140. // $base64 = 'data:'.$reponse['content_type'].';base64,'.base64_encode($reponse['content']);
  141. // $upload = Factory::baseImg();
  142. // $upload->move($base64);
  143. // $imgsrc = $upload->getSaveName();
  144. // Factory::cache('default')->hset($this->pathAndWxacodeunlimitKey, $key, $imgsrc);
  145. // return ResultWrapper::success($imgsrc);
  146. // }
  147. // $reponseData = json_decode($reponse['content'], true);
  148. // if( isset($reponseData['errcode']) ){
  149. // return ResultWrapper::fail($reponseData['errmsg'], $reponseData['errcode']);
  150. // }
  151. // }
  152. }
  153. /**
  154. * 获取小程序二维码
  155. * @param type $path
  156. * @param type $width
  157. */
  158. public function createQRCode($path,$width){
  159. }
  160. /**
  161. * 新版获取接口调用凭证
  162. * @return type
  163. */
  164. public function getAccessToken(){
  165. $url = "https://api.weixin.qq.com/cgi-bin/token?"
  166. ."appid=".$this->appid
  167. ."&secret=".$this->secret
  168. ."&grant_type=client_credential";
  169. $data = $this->Get($url);
  170. if(empty($data)){
  171. return false;
  172. }
  173. return json_decode($data,true);
  174. }
  175. /**
  176. * 手机授权登录
  177. * @param string $state 状态值
  178. * @param string $scope 作用域
  179. */
  180. public function oauth($state = '', $scope = ''){
  181. if($state == '') $state = $this->state;
  182. if($scope == '') $scope = $this->scope;
  183. $oauth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid
  184. ."&redirect_uri=".$this->redirect_uri
  185. ."&response_type=".$this->response_type
  186. ."&scope=".$scope.
  187. "&state=".$state."#wechat_redirect";
  188. return redirect($oauth_url);
  189. }
  190. /**
  191. * 处理事情
  192. */
  193. public function oauth_reuslt($code){
  194. $oauth_reuslt_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
  195. ."&secret=".$this->secret
  196. ."&code=".$code
  197. ."&grant_type=authorization_code";
  198. $data = $this->Post("",$oauth_reuslt_url);
  199. return json_decode($data,true);
  200. }
  201. /**
  202. * 拉取用户信息(需scope为 snsapi_userinfo)
  203. *
  204. * openid 用户的唯一标识
  205. nickname 用户昵称
  206. sex 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
  207. province 用户个人资料填写的省份
  208. city 普通用户个人资料填写的城市
  209. country 国家,如中国为CN
  210. headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
  211. privilege 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
  212. unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)
  213. */
  214. public function userinfo($access_token){
  215. $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$this->appid."&lang=zh_CN";
  216. $data = $this->Post("",$userinfo_url);
  217. return json_decode($data,true);
  218. }
  219. /**
  220. * 检验授权凭证(access_token)是否有效
  221. * @param $access_token
  222. */
  223. public function auth($access_token,$openid){
  224. $r = false;
  225. $auth_url = "https://api.weixin.qq.com/sns/auth?access_token=&openid=".$openid;
  226. $data = $this->Post("",$auth_url);
  227. $d = json_decode($data,true);
  228. if($d['errcode'] == 0){
  229. $r = true;
  230. }
  231. return $r;
  232. }
  233. /**
  234. * 写入值
  235. * @param $curlPost
  236. * @param $url
  237. * @return mixed
  238. */
  239. function Post($curlPost, $url, $isres=0) {
  240. $curl = curl_init();
  241. curl_setopt($curl, CURLOPT_URL, $url);
  242. curl_setopt($curl, CURLOPT_HEADER, false);
  243. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  244. // curl_setopt($curl, CURLOPT_NOBODY, true);
  245. curl_setopt($curl, CURLOPT_POST, true);
  246. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  247. $return_str = curl_exec($curl);
  248. $err = "";
  249. if(empty($return_str)){
  250. $err = "系统错误:".curl_error($curl);
  251. }
  252. $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  253. $content_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
  254. curl_close($curl);
  255. if(!empty($err)){
  256. $this->error = $err;
  257. return false;
  258. }
  259. if($isres==1){
  260. if($httpcode!=200){
  261. return false;
  262. }
  263. return [
  264. "contentType"=>$content_type,
  265. "buffer"=>$return_str
  266. ];
  267. }
  268. return $return_str;
  269. }
  270. /**
  271. * 写入值
  272. * @param $url
  273. * @return mixed
  274. */
  275. function Get($url) {
  276. $curl = curl_init();
  277. curl_setopt($curl, CURLOPT_URL, $url);
  278. curl_setopt($curl, CURLOPT_HEADER, false);
  279. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  280. $return_str = curl_exec($curl);
  281. $err = "";
  282. if(empty($return_str)){
  283. $err = "系统错误:".curl_error($curl);
  284. }
  285. curl_close($curl);
  286. if(!empty($err)){
  287. $this->error = $err;
  288. return false;
  289. }
  290. return $return_str;
  291. }
  292. /**
  293. * 写入日志
  294. * @param type $word
  295. * @param type $name
  296. */
  297. function log_result($word, $name) {
  298. $fp = fopen($name . "_log.txt", "a");
  299. flock($fp, LOCK_EX);
  300. fwrite($fp, $word . "\n");
  301. flock($fp, LOCK_UN);
  302. fclose($fp);
  303. }
  304. }