初始界面:
![](https://i-blog.csdnimg.cn/blog_migrate/96d3a65717687472176c7d6db3e5d11b.png)
判断用户是否过期(如果未过期则重新登录):
![](https://i-blog.csdnimg.cn/blog_migrate/f751533509886aff941aa48839780e70.png)
获取用户信息:
![](https://i-blog.csdnimg.cn/blog_migrate/e98ad89a8af20c28e38a0018a82fa7ff.png)
获取用户的信息并在前台显示:
![](https://i-blog.csdnimg.cn/blog_migrate/21c7b7d5a791f42b1b00c6f2278ce2e9.png)
主要实现两个功能:
①判断登录是否过期,如果过期则就重新登录,如果没过期就提示未过期
②获取用户的信息,并在前台显示
index.wxml
?
1 2 3 4 5 6 7 | < button bindtap = "login" >登录</ button > < button bindtap = "checksession" >登录是否过期</ button > < button open-type = "getUserInfo" bindgetuserinfo = "info" >点击授权</ button > < text >{{city}}</ text > < text >{{country}}</ text > < text >{{nickName}}</ text > < text >{{province}}</ text > |
index.js
?
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 | //index.js //获取应用实例 const app = getApp() Page({ data: { city: '' , country: '' , nickName: '' , province: '' }, //发起http请求 login: function (){ wx.login({ success: function (res){ console.log(res.code) //发送请求 wx.request({ url: '自己的域名' , //仅为示例,并非真实的接口地址 data: { code:res.code }, header: { 'content-type' : 'application/json' // 默认值 }, success(res) { console.log(res) } }) } }) }, //验证登录是否过期 checksession: function (){ wx.checkSession({ success: function (res){ console.log(res, '登录未过期' ) wx.showToast({ title: '登录未过期啊' , }) }, fail: function (res){ console.log(res, '登录过期了' ) wx.showModal({ title: '提示' , content: '你的登录信息过期了,请重新登录' , }) //再次调用wx.login() wx.login({ success: function (res) { console.log(res.code) //发送请求 wx.request({ url: '自己的域名' , //仅为示例,并非真实的接口地址 data: { code: res.code }, header: { 'content-type' : 'application/json' // 默认值 }, success(res) { console.log(res) } }) } }) } }) }, //获取用户的信息 info: function (){ var that= this wx.getUserInfo({ success: function (res){ console.log(res.userInfo) var city = res.userInfo.city var country = res.userInfo.country var nickName = res.userInfo.nickName var province = res.userInfo.province that.setData({ city:city, country:country, nickName:nickName, province:province }) } }) } }) |
index.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 | <?php //声明code,用来接收前台传过来的code $code = $_GET [ 'code' ]; //获取到appid $appid = "xxxxxxxxxxx" ; //自己的appid $secret = "xxxxxxxxxxxx" ; //自己的secret $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code" ; //可去小程序开发文档中查看这个链接 //发送的代码 function httpGet( $url ){ $curl =curl_init(); curl_setopt( $curl , CURLOPT_RETURNTRANSFER, true); curl_setopt( $curl , CURLOPT_TIMEOUT, 500); curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, true); curl_setopt( $curl , CURLOPT_SSL_VERIFYHOST, true); curl_setopt( $curl , CURLOPT_URL, $url ); $res = curl_exec( $curl ); curl_close( $curl ); return $res ; } $str =httpGet( $api ); echo $str ; ?> |
关于这个php文件的说明:
①获取appid和secret:
![](https://i-blog.csdnimg.cn/blog_migrate/d2393f62cc9dc946e86a636cd2fe9b55.png)
②当你点击登录的时候,出现这些东西就说明php文件调用成功
![](https://i-blog.csdnimg.cn/blog_migrate/0ef3eee33547f497bc2966f4c46db665.png)
③登录凭证校检地址(该里面的参数即可):
![](https://i-blog.csdnimg.cn/blog_migrate/1141d67cee43560ae5dca3239a5f41f3.png)
④域名要合法
在小程序平台上:
![](https://i-blog.csdnimg.cn/blog_migrate/2cc37d66b8c49199564d78f132c2b723.png)
在web开发者工具里:
![](https://i-blog.csdnimg.cn/blog_migrate/c2042e23faeaa8725a9229aca488f9f8.png)