weixina.php 11 KB

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