• uniapp的微信小程序授权头像昵称(最新版)


    前面我出过两期博客关于小程序授权登录,利用php实现一个简单的小程序授权登录并存储授权用户信息到数据库的完整流程。无奈,小程序官方又整幺蛾子了。wx.getUserInfo接口收回,wx.getUserProfile接口也不让用。导致我的个人小程序:梦缘  的授权登录得改。所以,这篇博客专门针对授权登录中头像昵称的使用进行说明。内附源码

     1.问题定位

           lz的小程序:梦缘   技术栈是基于:php+uniapp+vant 写的,之前因为偷懒,没写后台管理,然后php用的也是tp5,没有走管理框架,导致后端代码不规范,后面就因为一些特殊原因,就重构了一下。然后也就理所当然的遇到了,授权登录头像昵称的问题

    之前走的wx.getUserProfile,也能用,但是获取的头像都变成了灰色头像,昵称统一为微信用户。作为强迫症患者的我肯定是不允许的,所以,必须解决。但是之前的代码我又不想删,毕竟都是自己一行一行敲出来的,我更加偏向于加代码。

    这是之前授权登录得代码,直接调用wx.getUserProfile即可。

    2.解决思路

            前面提到过,之前的授权登录代码依旧是可以用的,只是头像和昵称统一是灰白色和微信用户。再结合前面官方说的,基础版本库的影响,基础版本库低于2.21.2,wx.getUserProfile返回的就是正常的头像昵称,高于2.21.2的话,就要使用昵称头像填写功能,把这两个值作为参数传递给后端。

     既然如此,我之前的代码就可以保留了,在传参时额外增加nicknameavatar参数即可。后端针对是否有这两个参数做针对性处理。而这两个参数就需要前端利用小程序昵称头像的填写来获取了。

    3.源码解析

    下面直接公布授权登录得代码:login.vue

    这里通过动态的获取当前小程序基础库来决定是否调用微信的头像昵称填写功能。然后再统一调用授权登录接口。后端根据动态动态保存。

    1. <template>
    2. <view>
    3. <view>
    4. <view class='header'>
    5. <view class="userinfo-avatar">
    6. <open-data type="userAvatarUrl" lang="zh_CN" />
    7. view>
    8. view>
    9. <view class='content'>
    10. <view>申请获取以下权限view>
    11. <text>获得你的公开信息(昵称,头像等)text>
    12. view>
    13. <button class='bottom' type='primary' @click="login" v-if="ischeck" >
    14. 授权登录
    15. button>
    16. <button class='bottom' type='primary' @click="login_zheshow" v-else>
    17. 授权登录
    18. button>
    19. <btnlogin :zheshow='zheshow' v-if="zheshow" />
    20. view>
    21. view>
    22. template>
    23. <script>
    24. import btnlogin from '@/components/butlogin/butlogin';
    25. const context = require("../../context/ggyzContext.js");
    26. export default {
    27. data() {
    28. return {
    29. code:'',
    30. ischeck:true,
    31. zheshow:false,
    32. nickname:"",
    33. avatar:"",
    34. }
    35. },
    36. components:{
    37. btnlogin
    38. },
    39. onShow() {
    40. var that=this;
    41. wx.login({
    42. success(res) {
    43. console.log("code:",res.code);
    44. if (res.code) {
    45. that.code=res.code;
    46. } else {
    47. console.log('登录失败!' + res.errMsg)
    48. }
    49. }
    50. })
    51. var {SDKVersion} = wx.getSystemInfoSync()
    52. // 判断是否支持getUserProfile()获取头像昵称
    53. var compareRes = this.compareVersion(SDKVersion, "2.21.2");
    54. // 不支持
    55. if (compareRes !== -1) {
    56. console.log("不支持getUserProfile()获取头像")
    57. this.ischeck = false;
    58. return
    59. }
    60. // 支持
    61. console.log("支持getUserProfile()获取头像")
    62. },
    63. methods: {
    64. login_zheshow(){
    65. this.zheshow = true;
    66. },
    67. loset(Logon_Credentials){
    68. console.log(Logon_Credentials,'登录信息');
    69. this.avatar=Logon_Credentials.active;
    70. this.nickname=Logon_Credentials.nickname;
    71. this.login();
    72. },
    73. close(){
    74. this.zheshow=false;
    75. },
    76. /**
    77. * 版本比较
    78. * v1 >= v2 返回 0或1 否则 -1
    79. * @param {String} v1
    80. * @param {String} v2
    81. */
    82. compareVersion (v1, v2) {
    83. v1 = v1.split('.')
    84. v2 = v2.split('.')
    85. const len = Math.max(v1.length, v2.length)
    86. while (v1.length < len) {
    87. v1.push('0')
    88. }
    89. while (v2.length < len) {
    90. v2.push('0')
    91. }
    92. for (let i = 0; i < len; i++) {
    93. const num1 = parseInt(v1[i])
    94. const num2 = parseInt(v2[i])
    95. if (num1 > num2) {
    96. return 1
    97. } else if (num1 < num2) {
    98. return -1
    99. }
    100. }
    101. return 0
    102. },
    103. login(){
    104. var that=this;
    105. wx.getUserProfile({
    106. desc: '用于完善会员资料',
    107. success: e => {
    108. console.log("授权信息:",e);
    109. //发起网络请求
    110. context.request({
    111. url: context.constant.url.login,
    112. method:'POST',
    113. data: {
    114. encryptedData: e.encryptedData,
    115. iv: e.iv,
    116. code: that.code,
    117. nickname:that.nickname,
    118. avatar:that.avatar
    119. },
    120. success(res) {
    121. console.log(res.data);
    122. if(res.data.code==1){
    123. uni.setStorageSync('userInfo',res.data.data);
    124. uni.setStorageSync('token',res.data.data.token);
    125. uni.setStorageSync('loginFlag',{expireTime:res.data.data.expiretime})
    126. uni.switchTab({
    127. url:'/pages/my/my'
    128. })
    129. }else{
    130. setTimeout( () => {
    131. uni.showToast({
    132. title: res.data.msg,
    133. icon: "none",
    134. });
    135. setTimeout( () =>{
    136. wx.hideToast();
    137. },2000)
    138. },0);
    139. }
    140. }
    141. })
    142. }
    143. })
    144. }
    145. }
    146. }
    147. script>
    148. <style>
    149. page{
    150. background: #FFFFFF;
    151. }
    152. .header {
    153. margin: 90rpx 90rpx 90rpx 50rpx;
    154. border-bottom: 1px solid #ccc;
    155. text-align: center;
    156. width: 650rpx;
    157. height: 300rpx;
    158. line-height: 450rpx;
    159. display: flex;
    160. justify-content: center;
    161. align-items: center;
    162. }
    163. .header .userinfo-avatar {
    164. width: 200rpx;
    165. height: 200rpx;
    166. margin-bottom: 80rpx;
    167. }
    168. .content {
    169. margin-left: 50rpx;
    170. margin-bottom: 90rpx;
    171. }
    172. .content text {
    173. display: block;
    174. color: #9d9d9d;
    175. margin-top: 40rpx;
    176. }
    177. .bottom {
    178. border-radius: 80rpx;
    179. margin: 70rpx 50rpx;
    180. font-size: 35rpx;
    181. }
    182. style>

    btnlogin.vue昵称填写组件:

    tips:这里用到了vant的弹窗,记得uni-app项目是否引入了该组件库。别忘了在pages.json声明引用

    1. <template>
    2. <view>
    3. <van-popup position="bottom" :show="zheshow1" round>
    4. <view class="zheshow" >
    5. <view class="cen_ter">
    6. <view class="box_At">
    7. <view class="box_At_text">获取您的昵称、头像、手机号view>
    8. <view class="box_At_co">获取用户头像、昵称、手机号信息,主要用于完善个人资料,向用户提供更好使用体验view>
    9. <view class="box_B" style="border-top:1px solid #f3f3f3 ;">
    10. <view class="acvter">头像view>
    11. <button v-if="!active" class="acvter_all" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
    12. <view class="mast">请选择头像view>
    13. button>
    14. <view v-if="active" class="img"><image :src="active" >image>view>
    15. view>
    16. <view class="box_B">
    17. <view class="acvter">昵称view>
    18. <input class="acvter_all" type="nickname" :value="nickname" @blur="bindblur" placeholder="请输入昵称" />
    19. view>
    20. <view class="Brn_S">
    21. <view class="btn_btns" @click="colse">取消view>
    22. <button class="btn" @click="btns" style="background-color: #22ac38 !important;color: #ffffff !important;">
    23. <view class="btns">保存view>
    24. button>
    25. view>
    26. view>
    27. view>
    28. view>
    29. van-popup>
    30. view>
    31. template>
    32. <script>
    33. export default {
    34. props:['zheshow'],
    35. data() {
    36. return {
    37. on_zheshows:false,
    38. zheshow1:false,
    39. active:'',
    40. nickname:''
    41. }
    42. },
    43. mounted() {
    44. this.zheshow1=this.zheshow;
    45. },
    46. watch:{
    47. zheshow(zheshow,oldValue) {
    48. this.zheshow1=this.zheshow
    49. },
    50. },
    51. methods: {
    52. colse(){
    53. console.log("取消")
    54. this.$parent.close();
    55. },
    56. onChooseAvatar(e) {
    57. let that = this
    58. uni.getFileSystemManager().readFile({
    59. filePath: e.detail.avatarUrl, //选择图片返回的相对路径
    60. encoding: "base64",
    61. success: (res) => {
    62. let base64s = "data:image/jpeg;base64," + res.data
    63. that.active = base64s
    64. },
    65. fail: (res) => reject(res.errMsg),
    66. });
    67. },
    68. bindblur(e){this.nickname = e.detail.value},
    69. btns(e){
    70. if(this.active==''){
    71. uni.showToast({title:'请选择上传头像',icon:'none'})
    72. return
    73. }
    74. if(this.nickname==''){
    75. uni.showToast({title:'请填写昵称',icon:'none'})
    76. return
    77. }
    78. this.$parent.loset({nickname:this.nickname,active:this.active}) // 信息传递父组件中
    79. }
    80. }
    81. }
    82. script>
    83. <style lang="scss" scoped>
    84. .zheshow{
    85. width: 100%;height: 100%;background-color: rgba(0,0,0,0.3);position: fixed;top: 0;left: 0;
    86. display: flex;align-items: center;align-items: flex-end;
    87. .mast{margin-top: 6rpx;}
    88. .Brn_S{width: 70%;height: 100rpx;display: flex;align-items: center;justify-content: space-between;margin: 10rpx auto;}
    89. .btn_btns{width: 300rpx;height: 80rpx;background: antiquewhite; display: flex;align-items: center;
    90. justify-content: center; border-radius: 10rpx; margin-right: 70rpx;background-color: #fafafa;color: #39B54A;}
    91. .imgs{position: absolute;right: 6%;width: 32rpx;height: 32rpx;}
    92. .img{width: 90rpx;height: 90rpx;border-radius: 50%;margin-left: 80rpx;image{width: 100%;height: 100%;border-radius: 50%;}}
    93. .cen_ter{
    94. width: 100%;height: 600rpx;border-top-left-radius: 30rpx;border-top-right-radius: 30rpx;background-color: #FFFFFF;
    95. display: flex;align-items: center;justify-content: center;
    96. .box_At{width: 90%;height: 92%;margin-top: 20rpx;display: flex;flex-direction: column;.box_At_text{font-weight: bold;font-size: 30rpx}
    97. .box_At_co{font-size: 28rpx;color: #ababab;margin-top: 24rpx;}
    98. .box_B{width: 100%;height: 120rpx;border-bottom: 1px solid #f3f3f3;display: flex;align-items: center;
    99. margin-top: 12rpx;.acvter_all{font-size: 28rpx;color: #ababab;margin-left: 80rpx;}}
    100. .btn{width:300rpx;margin: 35rpx auto;height: 80rpx;display: flex;align-items: center;justify-content: center;
    101. background-color: #39B54A;color: #FFFFFF;border-radius: 10rpx;font-size: 30rpx;}
    102. }}
    103. }
    104. button {
    105. border-radius: 30rpx;height: 80rpx !important;padding-left: 0!important ;
    106. padding-right: 0!important ; background-color: rgba(0,0,0,0) !important;color: #ababab !important;font-family: PingFang SC !important;
    107. }
    108. button:after {
    109. top: 0;left: 0; border: 1px solid rgba(0,0,0,0) !important; -webkit-transform: scale(.5);
    110. transform: scale(.5); -webkit-transform-origin: 0 0; transform-origin: 0 0; box-sizing: border-box; border-radius: 10px;
    111. }
    112. style>

    组件效果:

  • 相关阅读:
    概率论的学习和整理10:0-1分支 和 二项分布
    前端——Layui的导航栏与tab页联动
    上传文件到到大数据平台
    负载均衡的算法(静态算法与动态算法)
    【Android】深入Binder拦截
    使用python校验密码强度
    D. Grid-00100(构造一个行列均匀的方阵)
    Redis内存耗尽后会发生什么?
    Numpy学习笔记
    第三篇------Virtual I/O Device (VIRTIO) Version 1.1
  • 原文地址:https://blog.csdn.net/qq_34761385/article/details/136291382