weixina.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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);
  110. $res = $data;
  111. if(empty($data)){
  112. return false;
  113. }
  114. return $data;
  115. $data = json_decode($data,true);
  116. if(empty($data) || empty($data["contentType"]) || empty($data["buffer"]) || $data["errcode"]!=0){
  117. $this->error="返回参数错误".$res;
  118. return false;
  119. }
  120. return $data;
  121. // return $data["phone_info"];
  122. // $key = md5($this->appid.$parms['path'].$parms['params']);
  123. // $imgsrc = Factory::cache('default')->hget($this->pathAndWxacodeunlimitKey, $key);
  124. // if(!empty($imgsrc)){
  125. // return ResultWrapper::success($imgsrc);
  126. // }
  127. //
  128. // $apiUrl = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
  129. //
  130. // $postData = [
  131. // 'scene' => $parms['params'],
  132. // 'page' => $parms['path'],
  133. // 'width' => 430,
  134. // 'auto_color' => true,
  135. // ];
  136. //
  137. // $reponse = request($apiUrl, json_encode($postData));
  138. // if($reponse['httpcode'] != 200){
  139. // return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  140. // }else{
  141. // if(!is_object($reponse['content'])){
  142. // $base64 = 'data:'.$reponse['content_type'].';base64,'.base64_encode($reponse['content']);
  143. // $upload = Factory::baseImg();
  144. // $upload->move($base64);
  145. // $imgsrc = $upload->getSaveName();
  146. // Factory::cache('default')->hset($this->pathAndWxacodeunlimitKey, $key, $imgsrc);
  147. // return ResultWrapper::success($imgsrc);
  148. // }
  149. // $reponseData = json_decode($reponse['content'], true);
  150. // if( isset($reponseData['errcode']) ){
  151. // return ResultWrapper::fail($reponseData['errmsg'], $reponseData['errcode']);
  152. // }
  153. // }
  154. }
  155. /**
  156. * 获取小程序二维码
  157. * @param type $path
  158. * @param type $width
  159. */
  160. public function createQRCode($path,$width){
  161. }
  162. /**
  163. * 新版获取接口调用凭证
  164. * @return type
  165. */
  166. public function getAccessToken(){
  167. $url = "https://api.weixin.qq.com/cgi-bin/token?"
  168. ."appid=".$this->appid
  169. ."&secret=".$this->secret
  170. ."&grant_type=client_credential";
  171. $data = $this->Get($url);
  172. if(empty($data)){
  173. return false;
  174. }
  175. return json_decode($data,true);
  176. }
  177. /**
  178. * 手机授权登录
  179. * @param string $state 状态值
  180. * @param string $scope 作用域
  181. */
  182. public function oauth($state = '', $scope = ''){
  183. if($state == '') $state = $this->state;
  184. if($scope == '') $scope = $this->scope;
  185. $oauth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid
  186. ."&redirect_uri=".$this->redirect_uri
  187. ."&response_type=".$this->response_type
  188. ."&scope=".$scope.
  189. "&state=".$state."#wechat_redirect";
  190. return redirect($oauth_url);
  191. }
  192. /**
  193. * 处理事情
  194. */
  195. public function oauth_reuslt($code){
  196. $oauth_reuslt_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
  197. ."&secret=".$this->secret
  198. ."&code=".$code
  199. ."&grant_type=authorization_code";
  200. $data = $this->Post("",$oauth_reuslt_url);
  201. return json_decode($data,true);
  202. }
  203. /**
  204. * 拉取用户信息(需scope为 snsapi_userinfo)
  205. *
  206. * openid 用户的唯一标识
  207. nickname 用户昵称
  208. sex 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
  209. province 用户个人资料填写的省份
  210. city 普通用户个人资料填写的城市
  211. country 国家,如中国为CN
  212. headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
  213. privilege 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
  214. unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)
  215. */
  216. public function userinfo($access_token){
  217. $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$this->appid."&lang=zh_CN";
  218. $data = $this->Post("",$userinfo_url);
  219. return json_decode($data,true);
  220. }
  221. /**
  222. * 检验授权凭证(access_token)是否有效
  223. * @param $access_token
  224. */
  225. public function auth($access_token,$openid){
  226. $r = false;
  227. $auth_url = "https://api.weixin.qq.com/sns/auth?access_token=&openid=".$openid;
  228. $data = $this->Post("",$auth_url);
  229. $d = json_decode($data,true);
  230. if($d['errcode'] == 0){
  231. $r = true;
  232. }
  233. return $r;
  234. }
  235. /**
  236. * 写入值
  237. * @param $curlPost
  238. * @param $url
  239. * @return mixed
  240. */
  241. function Post($curlPost, $url) {
  242. $curl = curl_init();
  243. curl_setopt($curl, CURLOPT_URL, $url);
  244. curl_setopt($curl, CURLOPT_HEADER, false);
  245. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  246. curl_setopt($curl, CURLOPT_NOBODY, true);
  247. curl_setopt($curl, CURLOPT_POST, true);
  248. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  249. $return_str = curl_exec($curl);
  250. $err = "";
  251. if(empty($return_str)){
  252. $err = "系统错误:".curl_error($curl);
  253. }
  254. curl_close($curl);
  255. if(!empty($err)){
  256. $this->error = $err;
  257. return false;
  258. }
  259. return $return_str;
  260. }
  261. /**
  262. * 写入值
  263. * @param $url
  264. * @return mixed
  265. */
  266. function Get($url) {
  267. $curl = curl_init();
  268. curl_setopt($curl, CURLOPT_URL, $url);
  269. curl_setopt($curl, CURLOPT_HEADER, false);
  270. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  271. $return_str = curl_exec($curl);
  272. $err = "";
  273. if(empty($return_str)){
  274. $err = "系统错误:".curl_error($curl);
  275. }
  276. curl_close($curl);
  277. if(!empty($err)){
  278. $this->error = $err;
  279. return false;
  280. }
  281. return $return_str;
  282. }
  283. /**
  284. * 写入日志
  285. * @param type $word
  286. * @param type $name
  287. */
  288. function log_result($word, $name) {
  289. $fp = fopen($name . "_log.txt", "a");
  290. flock($fp, LOCK_EX);
  291. fwrite($fp, $word . "\n");
  292. flock($fp, LOCK_UN);
  293. fclose($fp);
  294. }
  295. }