微信小程序用户授权,以及判断登录是否过期

判断用户是否过期(如果未过期则重新登录):

获取用户信息:

获取用户的信息并在前台显示:

主要实现两个功能:

①判断登录是否过期,如果过期则就重新登录,如果没过期就提示未过期

②获取用户的信息,并在前台显示

index.wxml


  
  
  1. <button bindtap="login">登录 </button>
  2. <button bindtap="checksession">登录是否过期 </button>
  3. <button open-type="getUserInfo" bindgetuserinfo="info">点击授权 </button>
  4. <text>{{city}} </text>
  5. <text>{{country}} </text>
  6. <text>{{nickName}} </text>
  7. <text>{{province}} </text>

index.js


  
  
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. city: '',
  7. country: '',
  8. nickName: '',
  9. province: ''
  10. },
  11. //发起http请求
  12. login: function(){
  13. wx.login({
  14. success: function(res){
  15. console.log(res.code)
  16. //发送请求
  17. wx.request({
  18. url: '自己的域名', //仅为示例,并非真实的接口地址
  19. data: {
  20. code:res.code
  21. },
  22. header: {
  23. 'content-type': 'application/json' // 默认值
  24. },
  25. success(res) {
  26. console.log(res)
  27. }
  28. })
  29. }
  30. })
  31. },
  32. //验证登录是否过期
  33. checksession: function(){
  34. wx.checkSession({
  35. success: function(res){
  36. console.log(res, '登录未过期')
  37. wx.showToast({
  38. title: '登录未过期啊',
  39. })
  40. },
  41. fail: function(res){
  42. console.log(res, '登录过期了')
  43. wx.showModal({
  44. title: '提示',
  45. content: '你的登录信息过期了,请重新登录',
  46. })
  47. //再次调用wx.login()
  48. wx.login({
  49. success: function (res) {
  50. console.log(res.code)
  51. //发送请求
  52. wx.request({
  53. url: '自己的域名', //仅为示例,并非真实的接口地址
  54. data: {
  55. code: res.code
  56. },
  57. header: {
  58. 'content-type': 'application/json' // 默认值
  59. },
  60. success(res) {
  61. console.log(res)
  62. }
  63. })
  64. }
  65. })
  66. }
  67. })
  68. },
  69. //获取用户的信息
  70. info: function(){
  71. var that= this
  72. wx.getUserInfo({
  73. success: function(res){
  74. console.log(res.userInfo)
  75. var city = res.userInfo.city
  76. var country = res.userInfo.country
  77. var nickName = res.userInfo.nickName
  78. var province = res.userInfo.province
  79. that.setData({
  80. city:city,
  81. country:country,
  82. nickName:nickName,
  83. province:province
  84. })
  85. }
  86. })
  87. }
  88. })

index.php


  
  
  1. <?php
  2. //声明code,用来接收前台传过来的code
  3. $code=$_GET[ 'code'];
  4. //获取到appid
  5. $appid= "xxxxxxxxxxx"; //自己的appid
  6. $secret= "xxxxxxxxxxxx"; //自己的secret
  7. $api= "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"; //可去小程序开发文档中查看这个链接
  8. //发送的代码
  9. function httpGet($url){
  10. $curl=curl_init();
  11. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  12. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  13. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  14. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
  15. curl_setopt($curl, CURLOPT_URL, $url);
  16. $res= curl_exec($curl);
  17. curl_close($curl);
  18. return $res;
  19. }
  20. $str=httpGet($api);
  21. echo $str;
  22. ?>

关于这个php文件的说明:

①获取appid和secret:

②当你点击登录的时候,出现这些东西就说明php文件调用成功

③登录凭证校检地址(该里面的参数即可):

④域名要合法

在小程序平台上:

在web开发者工具里:

猜你喜欢

转载自blog.csdn.net/u012746918/article/details/89497701