您的位置:首页 > 编程学习 > > 正文

thinkphp5.1手动连接mysql数据库(thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能示例)

更多 时间:2021-10-28 10:12:40 类别:编程学习 浏览量:2454

thinkphp5.1手动连接mysql数据库

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

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

php代码

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 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 . '&timestamp=' . $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代码

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • <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>
  • 希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

    原文链接:https://blog.csdn.net/fangdong88/article/details/78521111

    您可能感兴趣