thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能示例
程序员文章站
2022-05-14 16:42:04
本文实例讲述了thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能。分享给大家供大家参考,具体如下:
php代码
function cur...
本文实例讲述了thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能。分享给大家供大家参考,具体如下:
php代码
function curlhtml($url){ $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); $output = curl_exec($ch); //释放curl句柄 curl_close($ch); return $output; } class wechat { public $errmsg; //微信登录获取用户信息 public function getuserinfo() { //1.准备scope为snsapi_base网页授权页面 $redirect_url = config('system.site_url') . $_server["request_uri"]; $baseurl = urlencode($redirect_url); $snsapi_base_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . config('system.appid') . '&redirect_uri=' . $baseurl . '&response_type=code&scope=snsapi_base&state=state#wechat_redirect'; //2.静默授权,获取code //页面跳转至redirect_uri/?code=code&state=state $code = input('code'); if (!isset($code) || empty($code)) { header('location:' . $snsapi_base_url);exit(0); } //3.通过code换取网页授权access_token和openid $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . config('system.appid') . '&secret=' . config('system.appsecret') . '&code=' . $code . '&grant_type=authorization_code'; $content = curlhtml($curl); $result = json_decode($content, true); if(!isset($result['openid'])) { $this->errmsg = $result['errmsg'];return false; } $openid = $result['openid']; $userinfo = $this->getuserbyopenid($openid); return $userinfo; } private function getuserbyopenid($openid) { //获取access_token $token_info = $this->curlgetwxaccesstoken(); $access_token = $token_info['value']; //通过openid来获取用户基本信息 $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_cn"; $content = curlhtml($url); $result = json_decode($content, true); return $result; } /** * [wxshare 微信分享] * @param [type] $url [description] * @return [type] [description] */ public function wxshare() { $noncestr = uniqid(); $timestamp = time(); $url = config('system.site_url') . $_server["request_uri"]; // $redis = new \redis; // $ticket_key = 'wx_ticket'; // $ticket = $redis->get($ticket_key); // if (!$ticket) { // $ticket = $this->getjsapiticket(); // $redis->set($ticket_key, $ticket); // $redis->expire($ticket_key, 7200); // } $ticket = $this->getjsapiticket(); if ($ticket) { $str = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '×tamp=' . $timestamp . '&url=' . $url; $signature = sha1($str); $return_data = [ 'noncestr' => $noncestr, 'timestamp' => $timestamp, 'signature' => $signature, 'appid' => config('system.appid'), 'link' => $url, ]; return $return_data; } } private function getjsapiticket() { $map['keyname'] = 'ticket'; $map['modifytime'] = array('gt', time() - 7200); $return = wxtokenmodel::getone('*', $map); if ($return) { return $return['value']; } else { $token_info = $this->curlgetwxaccesstoken(); $access_token = $token_info['value']; $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $access_token . '&type=jsapi'; $output = curlhtml($url); $data = json_decode($output, true); if (isset($data['errcode']) && $data['errcode'] == 0) { $condition['keyname'] = 'ticket'; $update_data['modifytime'] = time(); $update_data['value'] = $data['ticket']; $up_result = wxtokenmodel::updatedata($condition, $update_data); if ($up_result !== false) { return $data['ticket']; } } } return false; } private function curlgetwxaccesstoken() { $map['keyname'] = 'accesstoken'; $map['modifytime'] = array('gt', time() - 7200); $return = wxtokenmodel::getone('*', $map); if ($return) { return $return; } else { $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . config('system.appid') . '&secret=' . config('system.appsecret'); $output = curlhtml($url); $data = json_decode($output, true); if ($data && isset($data['access_token'])) { $condition['keyname'] = 'accesstoken'; $update_data['modifytime'] = time(); $update_data['value'] = $data['access_token']; $up_result = wxtokenmodel::updatedata($condition, $update_data); if ($up_result !== false) { return $update_data; } } } return false; } }
html代码
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <script> wx.config({ debug: false, appid: '{$appid}', // 必填,公众号的唯一标识 timestamp: '{$timestamp}', // 必填,生成签名的时间戳 noncestr: '{$noncestr}', // 必填,生成签名的随机串 signature: '{$signature}',// 必填,签名,见附录1 jsapilist: ['onmenusharetimeline'] // 必填,需要使用的js接口列表,所有js接口列表见附录2 }); wx.ready(function () { // 分享到朋友圈 wx.onmenusharetimeline({ title: '', // 名 link: '{$link}', // 地址 imgurl: '', // 分享的图标 success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享后执行的回调函数 } }); }); </script>
上一篇: JS获取填报扩展单元格控件的值
下一篇: vue-cli随机生成port源码的方法