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

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

分享到: