当前位置:首页 > CMS教程 > Thinkphp > 列表

thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-11 12:50:30 浏览: 评论:0 

本文实例讲述了thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能,分享给大家供大家参考,具体如下:

php代码

  1. function curlHtml($url){ 
  2.   $ch = curl_init(); 
  3.   curl_setopt($ch, CURLOPT_URL, $url); 
  4.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  5.   curl_setopt($ch, CURLOPT_HEADER, 0); 
  6.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  7.   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
  8.   $output = curl_exec($ch); 
  9.   //释放curl句柄 
  10.   curl_close($ch); 
  11.   return $output; 
  12. } 
  13. class Wechat 
  14. { 
  15.   public $errmsg; 
  16.   //微信登录获取用户信息 
  17.   public function getUserInfo() { 
  18.     //1.准备scope为snsapi_base网页授权页面 
  19.     $redirect_url = config('system.site_url') . $_SERVER["REQUEST_URI"]; 
  20.     $baseurl = urlencode($redirect_url); 
  21.     $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'; 
  22.     //2.静默授权,获取code 
  23.     //页面跳转至redirect_uri/?code=CODE&state=STATE 
  24.     $code = input('code'); 
  25.     if (!isset($code) || emptyempty($code)) { 
  26.       header('Location:' . $snsapi_base_url);exit(0); 
  27.     } 
  28.     //3.通过code换取网页授权access_token和openID 
  29.     $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . config('system.appid') . '&secret=' . config('system.appsecret') . '&code=' . $code . '&grant_type=authorization_code'; 
  30.     $content = curlHtml($curl); 
  31.     $result = json_decode($content, true); 
  32.     if(!isset($result['openid'])) { 
  33.       $this->errmsg = $result['errmsg'];return false; 
  34.     } 
  35.     $openid = $result['openid']; 
  36.     $userinfo = $this->getUserByOpenid($openid); 
  37.     return $userinfo; 
  38.   } 
  39.   private function getUserByOpenid($openid) { 
  40.     //获取access_token 
  41.     $token_info  = $this->curlGetWxAccessToken(); 
  42.     $access_token = $token_info['value']; 
  43.     //通过OpenID来获取用户基本信息 
  44.     $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; 
  45.     $content = curlHtml($url); 
  46.     $result = json_decode($content, true); 
  47.     return $result; 
  48.   } 
  49.   /** 
  50.    * [wxShare 微信分享] 
  51.    * @param [type] $url [description] 
  52.    * @return [type]   [description] 
  53.    */ 
  54.   public function wxShare() 
  55.   { 
  56.     $noncestr = uniqid(); 
  57.     $timestamp = time(); 
  58.     $url = config('system.site_url') . $_SERVER["REQUEST_URI"]; 
  59.     // $redis   = new \Redis; 
  60.     // $ticket_key = 'wx_ticket'; 
  61.     // $ticket   = $redis->get($ticket_key); 
  62.     // if (!$ticket) { 
  63.     //   $ticket = $this->getJsapiTicket(); 
  64.     //   $redis->set($ticket_key, $ticket); 
  65.     //   $redis->expire($ticket_key, 7200); 
  66.     // } 
  67.     $ticket = $this->getJsapiTicket(); 
  68.     if ($ticket) { 
  69.       $str     = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '&timestamp=' . $timestamp . '&url=' . $url; 
  70.       $signature  = sha1($str); 
  71.       $return_data = [ 
  72.         'noncestr' => $noncestr, 
  73.         'timestamp' => $timestamp, 
  74.         'signature' => $signature, 
  75.         'appid'   => config('system.appid'), 
  76.         'link'   => $url, 
  77.       ]; 
  78.       return $return_data; 
  79.     } 
  80.   } 
  81.   private function getJsapiTicket() 
  82.   { 
  83.     $map['keyname'] = 'Ticket'; 
  84.     $map['modifytime'] = array('GT', time() - 7200); 
  85.     $return       = WxTokenModel::getOne('*', $map); 
  86.     if ($return) { 
  87.       return $return['value']; 
  88.     } else { 
  89.       $token_info  = $this->curlGetWxAccessToken(); 
  90.       $access_token = $token_info['value']; 
  91.       $url     = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $access_token . '&type=jsapi'; 
  92.       $output    = curlHtml($url); 
  93.       $data     = json_decode($output, true); 
  94.       if (isset($data['errcode']) && $data['errcode'] == 0) { 
  95.         $condition['keyname'] = 'Ticket'; 
  96.         $update_data['modifytime'] = time(); 
  97.         $update_data['value']   = $data['ticket']; 
  98.         $up_result      = WxTokenModel::updateData($condition, $update_data); 
  99.         if ($up_result !== false) { 
  100.           return $data['ticket']; 
  101.         } 
  102.       } 
  103.     } 
  104.     return false; 
  105.   } 
  106.   private function curlGetWxAccessToken() 
  107.   { 
  108.     $map['keyname'] = 'AccessToken'; 
  109.     $map['modifytime'] = array('GT', time() - 7200); 
  110.     $return = WxTokenModel::getOne('*', $map); 
  111.     if ($return) { 
  112.       return $return; 
  113.     } else { 
  114.       $url  = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . config('system.appid') . '&secret=' . config('system.appsecret'); 
  115.       $output = curlHtml($url); 
  116.       $data = json_decode($output, true); 
  117.       if ($data && isset($data['access_token'])) { 
  118.         $condition['keyname'] = 'AccessToken'; 
  119.         $update_data['modifytime'] = time(); 
  120.         $update_data['value'] = $data['access_token']; 
  121.         $up_result      = WxTokenModel::updateData($condition, $update_data); 
  122.         if ($up_result !== false) { 
  123.           return $update_data; 
  124.         } 
  125.       } 
  126.     } 
  127.     return false; 
  128.   } 
  129. } 

html代码

  1. <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> 
  2. <script> 
  3. wx.config({ 
  4.    debug: false, 
  5.    appId: '{$appid}', // 必填,公众号的唯一标识 
  6.    timestamp: '{$timestamp}', // 必填,生成签名的时间戳 
  7.    nonceStr: '{$noncestr}', // 必填,生成签名的随机串 
  8.    signature: '{$signature}',// 必填,签名,见附录1 
  9.    jsApiList: ['onMenuShareTimeline'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 
  10. }); 
  11. wx.ready(function () { 
  12.   // 分享到朋友圈 
  13.   wx.onMenuShareTimeline({ 
  14.    title: '', // 名 
  15.    link: '{$link}', // 地址 
  16.    imgUrl: '', // 分享的图标 
  17.    success: function () { 
  18.    // 用户确认分享后执行的回调函数 
  19.    }, 
  20.    cancel: function () { 
  21.     // 用户取消分享后执行的回调函数 
  22.   } 
  23.   }); 
  24. }); 
  25. </script>

Tags: thinkphp5微信登录 mysql

分享到: