当前位置:   article > 正文

uniapp微信小程序授权获取用户信息封装_uniapp开发@getuserinfo 个人号关于获取信息接口的权限

uniapp开发@getuserinfo 个人号关于获取信息接口的权限

根据平台公告信息,2022年底wx.getUserProfile 与 wx.getUserInfo 接口被回收。新的小程序不能再授权获取头像和昵称;可以通过wx.login 接口直接获取用户的 openId 与 unionId 信息;

所以需要获取头像和昵称的同志们需要自己写一个完善资料了

1、在  common  中创建一个    setUserInfo.js

  1.  export default() => {
  2.     return new Promise((resolve, reject) => {
  3.         uni.getUserProfile({
  4.             desc: '用于完善个人资料' // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  5.         })
  6.         .then((response) => {
  7.             if (response[1].errMsg === 'getUserProfile:ok') {
  8.                 uni.setStorageSync('userInfo', response[1].userInfo); //存储用户信息
  9.                 resolve(response[1]);//返回的信息 
  10.             }else {
  11.                 uni.showToast({
  12.                     title: '授权失敗',
  13.                     icon: 'error'
  14.                 });
  15.             }
  16.         }).catch((reject) => {
  17.              console.log('拒绝授权',reject)
  18.         })
  19.     })
  20. }

2、在   main.js  中全局引入

  1. import setUserInfo from "common/setUserInfo.js" //微信用户信息
  2. Vue.prototype.$setUserInfo = setUserInfo

3、在  components  中创建一个组件    authorize.vue

 html

  1. <template>
  2.     <view v-if="ifAuthorize" style="background: rgba(0,0,0,0.3);position: fixed;top: 0;width: 100%;height: 100%;">
  3.         <view style="width: 320px;margin: auto;margin-top: 30px;background: #fff;border-radius: 10px;">
  4.             <view class="header">
  5.                 需要您的授权
  6.             </view>
  7.             <view style="text-align: center;">
  8.                 <view style="text-align: center;padding-top: 17px;">
  9.                     <view>为了提供更好的服务</view>
  10.                     <view>请允许获取您的用户信息</view>
  11.                     <view>如暂不需要,可点击取消或拒绝</view>
  12.                 </view>
  13.                 <view style="text-align: center;">
  14.                     <image src="../static/image/shouquan.png" mode="widthFix" style="width: 230px;"></image>
  15.                 </view>
  16.                 <view style="padding-bottom: 5px;">
  17.                     <button type="default" class="btn" @click="authorize()">授权</button>
  18.                     <button type="default" class="btn" @click="cancel()">取消</button>
  19.                 </view>
  20.             </view>
  21.         </view>
  22.     </view>
  23. </template>

script

  1. <script>
  2.     export default {
  3.         name:"authorize",
  4.         data() {
  5.             return {
  6.                 ifAuthorize:true
  7.             };
  8.         },
  9.         methods:{
  10.             cancel() {
  11.                 this.ifAuthorize = false
  12.             },
  13.             authorize() {
  14.                 this.$setUserInfo().then(res => {
  15.                     this.ifAuthorize = false
  16.                 });
  17.             }
  18.         }
  19.     }
  20. </script>

   css

  1. <style lang="scss">
  2.     .header {
  3.         padding: 8px 0;
  4.         line-height: 33px;
  5.         border-bottom: 0.5px solid #eee;
  6.         text-align: center;
  7.     }
  8.     .btn {
  9.         background-color: #04be01 !important;
  10.         color: #fff !important;
  11.         border-radius: 44px;
  12.         width: 275px;
  13.         margin-bottom: 20px;
  14.     }
  15. </style>

4、在需要授权的页面中引入组件  authorize.vue

5、效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/740232
推荐阅读
相关标签
  

闽ICP备14008679号