wechatlogic.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /*
  3. * @Author: wang jun
  4. * @Date: 2022-01-18 11:12:23
  5. * @Last Modified by: wang jun
  6. * @Last Modified time: 2022-01-18 14:17:55
  7. * 微信类
  8. */
  9. namespace app\index\logic;
  10. use app\index\logic\wxBizDataCrypt;
  11. use app\index\model\usermodel;
  12. class wechatlogic
  13. {
  14. public function getselfwxconfig()
  15. {
  16. $config = [
  17. //'appid' => 'wx8788dcdf3d075963',
  18. 'appid' => 'wx8240b08d4e0b8954',
  19. //'appSecret' => '0d1928572155d59f0c66c30378e42bfb',
  20. 'appSecret' => 'df07e160bbb1235e7b737cbb7c87a0ed',
  21. ];
  22. return $config;
  23. }
  24. private function curl_get($url, &$httpCode = 0)
  25. {
  26. $ch = curl_init();
  27. curl_setopt($ch, CURLOPT_URL, $url);
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  29. //不做证书校验,部署在linux环境下请改为true
  30. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  31. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  32. $file_contents = curl_exec($ch);
  33. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  34. curl_close($ch);
  35. return $file_contents;
  36. }
  37. private function getAccessToken()
  38. {
  39. $config = $this->getselfwxconfig();
  40. $appid = $config['appid'];
  41. $appSecret = $config['appSecret'];
  42. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret;
  43. $result = $this->curl_get($url);
  44. $wxResult = json_decode($result, true);
  45. return $wxResult;
  46. }
  47. //开启curl post请求
  48. private function sendCmd($url, $data)
  49. {
  50. $curl = curl_init(); // 启动一个CURL会话
  51. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  52. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
  53. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  54. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
  55. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  56. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  57. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  58. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  59. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
  60. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  61. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  62. $tmpInfo = curl_exec($curl); // 执行操作
  63. if (curl_errno($curl)) {
  64. echo 'Errno' . curl_error($curl);
  65. }
  66. curl_close($curl); // 关键CURL会话
  67. return $tmpInfo; // 返回数据
  68. }
  69. /**
  70. * 获取openid
  71. * 20220118
  72. * wj
  73. * code 必填
  74. */
  75. public function getOpenid($arr)
  76. {
  77. $code = $arr['code'];
  78. $config = $this->getselfwxconfig();
  79. $appid = $config['appid'];
  80. $appSecret = $config['appSecret'];
  81. $wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
  82. $getUrl = sprintf($wxUrl, $appid, $appSecret, $code);
  83. $result = $this->curl_get($getUrl);
  84. $wxResult = json_decode($result, true);
  85. if (empty($wxResult)) {
  86. return backarr(0, "错误");
  87. } else {
  88. $logfail = array_key_exists('errcode', $wxResult);
  89. if ($logfail) {
  90. return backarr(1, 'success', $logfail['errmsg']);
  91. trace(json_encode($wxResult));
  92. } else {
  93. $openid = $wxResult['openid'];
  94. if (!isset($wxResult['unionid'])) {
  95. trace(json_encode($wxResult));
  96. }
  97. $data = ['openid' => $openid];
  98. if (isset($wxResult['unionid'])) {
  99. $unionid = $wxResult['unionid'];
  100. $data['unionid'] = $unionid;
  101. $t_m = new usermodel();
  102. $urec = $t_m->getinfobyopenid($openid);
  103. if ($urec) {
  104. if (!$urec['union_id']) {
  105. $t_m->updateunionidbyopenid($openid, $unionid);
  106. }
  107. }
  108. }
  109. return backarr(1, 'success', $data);
  110. }
  111. }
  112. }
  113. //获得二维码
  114. public function create_qrcode($arr)
  115. {
  116. $sharepage = request()->param();
  117. $qr_path = "./Uploads/";
  118. if (!file_exists($qr_path . 'user/')) {
  119. mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
  120. }
  121. $filename = 'user/' . time() . '.png';
  122. $file = $qr_path . $filename;
  123. $access = $this->getAccessToken();
  124. $access_token = $access['access_token'];
  125. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
  126. $qrcode = array(
  127. 'scene' => $arr['scene'], //二维码所带参数
  128. 'width' => 200,
  129. 'page' => $arr['page'], //二维码跳转路径(要已发布小程序)
  130. 'auto_color' => true,
  131. );
  132. $result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口
  133. $errcode = json_decode($result, true)['errcode'];
  134. $errmsg = json_decode($result, true)['errmsg'];
  135. if ($errcode) {
  136. trace(json_encode($result));
  137. return backarr(0, $errmsg);
  138. }
  139. $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
  140. if ($res === false) {
  141. echo '生成二维码失败';
  142. } else {
  143. $path = getselfurl('default') . "/Uploads/" . $filename;
  144. return backarr(1, 'success', ['path' => $path]);
  145. }
  146. }
  147. //网上代骊,待修改
  148. public function WxDecode()
  149. {
  150. // 接收参数
  151. $data = request()->param();
  152. $config = $this->getselfwxconfig();
  153. $appid = $config['appid'];
  154. $appSecret = $config['appSecret'];
  155. $grant_type = "authorization_code"; //授权(必填)
  156. $code = $data['code']; //有效期5分钟 登录会话
  157. $encryptedData = $data['encryptedData'];
  158. $iv = $data['iv'];
  159. // 拼接url
  160. $url = "https://api.weixin.qq.com/sns/jscode2session?" . "appid=" . $appid . "&secret=" . $appsecret . "&js_code=" . $code . "&grant_type=" . $grant_type;
  161. $res = json_decode($this->curl_get($url), true);
  162. $sessionKey = $res['session_key']; //取出json里对应的值
  163. $pc = new wxBizDataCrypt($appid, $sessionKey);
  164. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  165. //这里是按二手设备做的,以后应该单独变成逻辑,由逻辑层完成,以应对不同的需求
  166. if ($errCode == 0) {
  167. $result['code'] = 200;
  168. $resultData['msg'] = '解密成功';
  169. $resultData['userInfo'] = json_decode($data);
  170. $result['resultData'] = $resultData;
  171. echo json_encode($result);
  172. } else {
  173. return json($errCode);
  174. }
  175. }
  176. //获得二维码
  177. /**
  178. * 保存并下载二维码
  179. * 20220118
  180. * wj
  181. * scene page 必填
  182. */
  183. public function download_qrcode($arr)
  184. {
  185. $qr_path = "./Uploads/";
  186. if (!file_exists($qr_path . 'user/')) {
  187. mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
  188. }
  189. $filename = 'user/' . time() . '.png';
  190. $file = $qr_path . $filename;
  191. $access = $this->getAccessToken(); //json_decode($this->getAccessToken(),true);
  192. $access_token = $access['access_token'];
  193. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
  194. $qrcode = array(
  195. 'scene' => $arr['scene'], //二维码所带参数
  196. 'width' => 200,
  197. 'page' => $arr['page'], //二维码跳转路径(要已发布小程序)
  198. 'auto_color' => true,
  199. );
  200. $result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口
  201. $errcode = json_decode($result, true)['errcode'];
  202. $errmsg = json_decode($result, true)['errmsg'];
  203. if ($errcode) {
  204. trace(json_encode($result));
  205. return backarr(0, $errmsg);
  206. }
  207. $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
  208. $strfile = 'Uploads/' . $filename;
  209. $file1 = fopen($strfile, 'r');
  210. header("Content-Type: application/force-download");
  211. header("Content-Type: application/octet-stream");
  212. header("Content-Type: application/download");
  213. header('Content-Disposition:inline;filename="' . $strfile . '"');
  214. ob_clean();
  215. flush();
  216. echo fread($file1, filesize($file));
  217. fclose($file1);
  218. if ($res === false) {
  219. echo '生成二维码失败';
  220. } else {
  221. $path = getselfurl('default') . "/Uploads/" . $filename;
  222. return backarr(1, 'success', ['path' => $path]);
  223. }
  224. }
  225. }