当前位置:   article > 正文

微信小程序无法获取头像,昵称的解决办法 (原生)_小程序无法获得用户头像和昵称

小程序无法获得用户头像和昵称

最新发布的微信小程序已经无法获取头像和昵称,那么如何解决这个问题呢?

其实很简单,新用户注册后,提示跳转到新页面,要求修改昵称和头像即可。

  1. <import src="../temp.wxml"></import>
  2. <template is="nav_top" data="{{nav}}"></template>
  3. <view class="main_top" style="padding-top: {{nav.top+nav.safe_top+15}}px;"></view>
  4. <view class="warn">{{info.warn}}</view>
  5. <view class="box">
  6. <view class="logo">
  7. <view><view class="image" style="background: url({{user.logo}}) no-repeat center;background-size: cover;" ></view><text class="icon icon-bianji"></text>
  8. <button open-type="chooseAvatar" bindchooseavatar="save_logo"></button>
  9. </view>
  10. </view>
  11. <view class="l">
  12. <input type="nickname" placeholder="我的昵称" value="{{user.nickname}}" data-val="{{user.nickname}}" bindblur="val_change" data-c="nickname"></input>
  13. </view>
  14. <view class="l">
  15. <text>联系电话</text>
  16. <input placeholder="联系电话" value="{{user.phone}}" data-val="{{user.phone}}" bindblur="val_change" data-c="phone" style="text-align: left;font-size: 17px;" disabled="true"></input>
  17. <text style="position: absolute;right: 0;left:auto;color: #00a47c;">获取电话
  18. </text>
  19. <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" style="position: absolute;height: 40px;width:60px;z-index: 100;opacity:0;right: 0;top: 0;" ></button>
  20. </view>
  21. <view class="l" style="padding-right: 0;"><text>所在城市</text>
  22. <picker mode="region" value="{{region?region:''}}" bindchange="region_change" class="region">
  23. <view>{{user.dist?(user.dist):'未选择默认城市'}}
  24. <text class="icon icon-chevron-right"></text>
  25. </view>
  26. </picker>
  27. </view>
  28. </view>
  1. var app = getApp()
  2. var fun = require('../fun.js');
  3. Page({
  4. data: {
  5. nav:{top:app.globalData.nav_top,title:'我的信息',home:'show',back:'show',safe_top:app.globalData.safe_top,page:'my',no_data:'您还没有购买记录'},page:1,can_load:1,
  6. },
  7. onLoad(op) {
  8. },
  9. onReady() {
  10. this.load();
  11. var pages=getCurrentPages();
  12. this.setData({page_num:Object.keys(pages).length})
  13. },
  14. onShow() {
  15. if(app.globalData.flash==1){this.load();app.globalData.flash=0;}
  16. },
  17. load() {
  18. fun.get({ac:'get_my_info'}).then(res=>{
  19. var region;
  20. if(res.user.pro){
  21. region=[res.user.pro,res.user.city,res.user.dist]
  22. }
  23. this.setData({user:res.user,info:res.info,set:res.set,region:region})
  24. })
  25. },
  26. save_logo(e){
  27. console.log(e)
  28. var user=this.data.user;
  29. var save_data=this.data.save_data;if(!save_data){save_data={}}
  30. var that=this;
  31. var url=e.detail.avatarUrl;
  32. user.logo=url;
  33. //that.setData({user:user});
  34. wx.showLoading({
  35. title: '头像上传中,请稍后...',
  36. })
  37. setTimeout(function(){wx.hideLoading()},1500)
  38. var data={tempFilePath:url,size:0,c:'logo',}
  39. fun.upload(data).then(res=>{
  40. user.logo=res.img_240;
  41. that.setData({user:user});
  42. save_data.logo=res.img_240;
  43. that.data.save_data=save_data;
  44. that.save()
  45. })
  46. },
  47. save() {
  48. var save_data=this.data.save_data;
  49. if(!save_data){return;}
  50. if(Object.keys(save_data).length==0){return}
  51. fun.get({ac:'user_change',data:JSON.stringify(save_data)}).then(res=>{
  52. wx.hideLoading();
  53. this.data.save_data={}
  54. app.msg('保存成功');
  55. this.load();
  56. app.globalData.flash=1;
  57. })
  58. },
  59. val_change(e){
  60. var val=e.detail.value;
  61. var c=e.currentTarget.dataset.c;
  62. var save_data=this.data.save_data;if(!save_data){save_data={}}
  63. save_data[[c]]=val;
  64. this.data.save_data=save_data;
  65. this.save()
  66. },
  67. region_change(e){
  68. var region=e.detail.value;
  69. if(!app.globalData.region){
  70. app.globalData.region=region;
  71. try {
  72. wx.setStorageSync('region',region);
  73. } catch (e) { }
  74. }
  75. if(!app.globalData.area){
  76. app.globalData.area=region[2];
  77. try {
  78. wx.setStorageSync('area',region[2]);
  79. } catch (e) { }
  80. }
  81. var save_data=this.data.save_data;
  82. if(!save_data){save_data={}}
  83. save_data['pro']=region[0];save_data['city']=region[1];save_data['dist']=region[2];
  84. this.data.save_data=save_data;
  85. this.save();
  86. },
  87. getPhoneNumber(e){
  88. console.log('获取手机号',e.detail.errMsg,e.detail);
  89. var user=this.data.user;
  90. if(e.detail.errMsg=='getPhoneNumber:fail user deny'){
  91. this.back();return;
  92. }else{
  93. fun.get({ac:'get_phone',code:e.detail.code}).then(res=>{
  94. //获取成功后 则缓存状态
  95. if(res.phone){
  96. user.phone=res.phone;
  97. this.setData({user:user});
  98. var save_data=this.data.save_data;if(!save_data){save_data={}}
  99. save_data.phone=res.phone;
  100. this.data.save_data=save_data;
  101. this.save();
  102. }
  103. })
  104. }
  105. //this.back();
  106. },
  107. onReachBottom() {
  108. },
  109. onShareAppMessage() {
  110. },go(e){ wx.navigateTo({url: e.currentTarget.dataset.url})},
  111. back(e){var page_num=this.data.page_num;
  112. if(page_num==1){this.home();}else{wx.navigateBack({delta:1})}},
  113. home(e){ wx.reLaunch({url: '/pages/index/index'})},
  114. rgo(e){wx.redirectTo({url: e.currentTarget.dataset.url})},
  115. login: function (e) {wx.navigateTo({url: '/pages/login/index',})},
  116. win_open(e){var c=e.currentTarget.dataset.c;this.setData({[c]:'show'})},//打开窗口
  117. win_close(e){var c=e.currentTarget.dataset.c;this.setData({[c]:''})},//关闭窗口
  118. })
  1. .main_top{float: left;width: 100%;background: #fff;}
  2. .warn{float: left;width: 100%;padding: 10px;color: crimson;background: rgb(250, 218, 225);}
  3. page{background: #fff;}
  4. .box{float: left;width: 100%;background: #fff;padding:15px;margin-top: 15px;}
  5. .logo{float: left;width: 100%;display: flex;justify-content: center;}
  6. .logo view{float: left;width:80px;height: 80px;}
  7. .logo .image{float: left;width: 100%;height: 100%;border-radius: 50%;border: 5px solid #ccc;}
  8. .logo text{position: absolute;top:0px;right: 0px;background: #fff;width:24px;height: 24px;border-radius: 50%;font-size: 12px;text-align: center;line-height:18px;border:3px solid #eee;}
  9. .logo button{position: absolute;width:100%;height: 100%;z-index: 100;opacity: 0;}
  10. .l{float: left;width: 100%;margin-top:20px;padding-left:80px;padding-right:80px;font-size: 18px;}
  11. .l input{float: left;width: 100%;height: 40px;line-height: 40px;font-size: 20px;text-align: center;}
  12. .l text{position: absolute;left:0;height: 40px;line-height: 40px;}
  13. .l .region{float: left;width: 100%;height: 40px;line-height: 40px;padding-right: 30px;text-align: right;}
  14. .l .icon{right: 0;left: auto;font-size: 30px;}
  15. .l .region view{float: left;width: 100%;padding-right: 30px;}

本代码为原生代码,提供思路与解决办法,后端请自行编写.

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

闽ICP备14008679号