微信小程序获取用户信息接口调整目的以及使用方法

微信小程序获取用户信息接口调整目的以及使用方法介绍

微信小程序已经调整了获取用户信息的接口,还不知道的开发者看一下官网给出的理由和方法:

为优化用户体验,使用 wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持。从2018年4月30日开始,小程序与小游戏的体验版、开发版调用 wx.getUserInfo 接口,将无法弹出授权询问框,默认调用失败。正式版暂不受影响。开发者可使用以下方式获取或展示用户信息:

1、使用 button 组件,并将 open-type 指定为 getUserInfo 类型,获取用户基本信息。 详情参考文档: https://developers.weixin.qq.com ... mponent/button.html 

2、使用 open-data 展示用户基本信息。 详情参考文档: https://developers.weixin.qq.com ... nent/open-data.html

微信为什么要调整接口? 

开发者可以根据需要来调取用户信息,而不是用户首次进入小程序就弹出授权的弹框,这样对于用户来说是不友好的。比如可以在用户点击登录的时候再获取用户信息,或者提交表单的时候等等,总之可以根据业务逻辑进行开发。

然而对于我们项目的业务逻辑却是不好的事儿,因为我们需要在一开始就获取用户的信息入库,相信很多人都会有这样的需求,那就记录一下我们项目的登录。

首先自己写一个弹框,触发获取信息的内容,微信小程序原生组件弹框没有可以编辑的按钮,所以需要我们自己来写,如图: 

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

<!-- 自定义弹框开始 -->

  <view wx:if="{{showModel}}" class="model">

    <view class="modelTitle">

      获取微信授权信息

    </view>

    <view class="modelBody">微信登录需要获取您的用户信息,请前往设置</view>

    <view class="btns">

      <button open-type="getUserInfo" class="agree" bindgetuserinfo="agreeGetUser" lang="zh_CN">去设置</button>

    </view>

  </view>

  <view wx:if="{{showModel}}" class="mask"></view>

  <!-- 自定义弹框结束 -->

判断是否授权,如果没有授权,那么需要自定义弹框显示,点击“去设置”,然后弹出授权框;如果已经授权,逻辑上就应该不再弹出任何框,这里就需要把用户首次进入小程序授权的用户信息存在本地然后那来使用

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

// 登录

   wx.login({

     success: res => {

       app.globalData.code = res.code

       //取出本地存储用户信息,解决需要每次进入小程序弹框获取用户信息

       app.globalData.userInfo = wx.getStorageSync('userInfo')

       //wx.getuserinfo接口不再支持

       wx.getSetting({

         success: (res) => {

           //判断用户已经授权。不需要弹框

           if(!res.authSetting['scope.userInfo']){

             that.setData({

               showModel: true

             })

           }else{//没有授权需要弹框

             that.setData({

               showModel: false

             })

             wx.showLoading({

               title: '加载中...'

             })

             that.getOP(app.globalData.userInfo)

           }

         },

         fail: function () {

           wx.showToast({

             title: '系统提示:网络错误',

             icon: 'warn',

             duration: 1500,

           })

         }

       })

     },

     fail:function () {

       wx.showToast({

         title:'系统提示:网络错误',

         icon: 'warn',

         duration: 1500,

       })

     }

   })

 },

 //获取用户信息新接口

 agreeGetUser:function (e) {

   //设置用户信息本地存储

   try {

     wx.setStorageSync('userInfo', e.detail.userInfo)

   catch (e) {

     wx.showToast({

       title: '系统提示:网络错误',

       icon: 'warn',

       duration: 1500,

     })

   }

   wx.showLoading({

     title:'加载中...'

   })

   let that = this

   that.setData({

     showModel:false

   })

   that.getOP(e.detail.userInfo)

 },

 getOP: function (res) {//提交用户信息 获取用户id

   let that = this

   let userInfo = res

   app.globalData.userInfo = userInfo

   wx.request({

     url: 'https://xcx.xiancaijun.cn/tigerlogin/tgLogin',

     method: 'post',

     data: {

       "code": app.globalData.code,

       'userInfo': userInfo

     },

     success: function (res) {

       if(res.data.respcode == '0'){

         app.globalData.userId = res.data.uid

         app.globalData.keys = res.data.keys

         app.globalData.access = res.data.access

         that.getBook()

         that.shareInfo()

         if (that.data.cid) {

           wx.navigateTo({

             url: '/pages/course/course?cid=' + that.data.cid

           })

         }

       }else if(res.data.respcode == '15'){

         wx.hideLoading()

         wx.showToast({

           title:'没有授权,不能进入小程序',

           icon:'none',

           duration:2000

         })

       }

     }

   })

 }

猜你喜欢

转载自blog.csdn.net/sunlizhen/article/details/83862217
今日推荐