半年前做的个小程序,更新了二个文字,重新上传审核通过,悲剧了,新用户的昵称全部变为微信用户,头像全部变为默认头像,查了半天代码没找到原因,相当头大,搜了一下文档,尴尬了,11月9号新更新的规则,不再返回昵称和头像值....需要用头像昵称获取能力去触发获取!

解决方案

第一步,点击登录按钮,弹出层,通过授权获取OPENID值和手机号信息,先完成注册。

第二步,完成注册后验证昵称和头像是否为空,如为空弹出资料设置层,验证资料是否填写。点击头像按钮获取微信或者自定义上传头像,点昵称栏获取微信昵称或自定义昵称。保存!

wxml代码

  <view class="index"><view class="head"><view class="headbox"><view class="position"><view class="face"><view class="img"><block wx:if="{{avatarimg}}"><image class="self" mode="widthFix" src="{{avatarimg}}"></image></block><block wx:else><image mode="widthFix" src="headlogonew.png"></image></block></view><block wx:if="{{nickname}}"><view class="name">{{nickname}}</view></block><block wx:else><view class="name" bindtap="getUserProfile">点击登录/注册</view></block></view><block wx:if="{{isShowPhone}}"><view class="dialog-mask"></view><view class="dialog-info"><view class="dialog-title">请点击获取授权选择绑定的手机号</view><view class="dialog-footer"><button class="button" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取授权</button></view></view></block> <block wx:if="{{isShowUserInfo}}"><view class="dialog-mask"></view><view class="dialog-info"><view class="dialog-title">请设置头像和昵称</view><view class="dialog-footer dialog-line"><div class="set"><button open-type="chooseAvatar"  bindchooseavatar="onChooseAvatar"><block wx:if="{{avatarimg}}"><image mode="widthFix" src="{{avatarimg}}"></image></block><block wx:else><image mode="widthFix" src="/images/uploadface.png"></image></block></button></div><div class="set"><input  placeholder="请输入您的昵称" type="nickname" value="{{nickname}}" bindblur="bindblur"></input></div><div class="set"><button class="button" bindtap="setBtnTap">保存</button></div></view></view></block> <div class="clear"></div></view></view></view></view>

js代码

/*** 页面的初始数据*/data: {isShowPhone: false, //默认先不显示获取手机号isShowUserInfo: false,user_id: '',phone: '',avatarimg: '',nickname:'',},/** 获取昵称信息 */bindblur(e) {this.data.nickname = e.detail.value},/** 更换头像 */onChooseAvatar(e) {var that = thiswx.getFileSystemManager().readFile({filePath: e.detail.avatarUrl, //选择图片返回的相对路径 encoding: 'base64',success: res => {wx.request({url: app.SiteUrl + "imgupload",data: {file: 'data:image/png;base64,' + res.data,},header: {'content-type': 'multipart/form-data'},success: function (res) {if (res.data.status == 'success') {that.setData({avatarimg: res.data.data.image_name,//isShowUserInfo: false,})} else {console.log('fail');}}})console.log('data:image/png;base64,' + res.data)}})},setBtnTap() {var that = thisif (that.data.avatarimg.length == 0) {wx.showToast({title: '请上传头像!',icon: 'error',duration: 1500})} else if (that.data.nickname.length == 0) {wx.showToast({title: '请填写昵称!',icon: 'error',duration: 1500})} else {wx.request({url: app.SiteUrl + "setUserInfo",data: {user_id: that.data.user_id,nickname: that.data.nickname,avatarimg: that.data.avatarimg,},method: 'GET',header: {'content-type': 'application/json'},success: function (res) {if (res.data.data.phone != '') {that.setData({isShowPhone: false,isShowUserInfo: false,nickname: that.data.nickname,})wx.setStorageSync('user_id', res.data.data.user_id);wx.setStorageSync('phone', res.data.data.phone);wx.setStorageSync('nickname', that.data.nickname);wx.setStorageSync('avatarimg', that.data.avatarimg);wx.showToast({title: '更新成功!',icon: 'success',duration: 1500});} else {that.setData({isShowUserInfo: false,isShowPhone: true,})}}})}},getUserProfile() {var that = thiswx.showLoading({title: '获取授权中'})wx.getUserProfile({desc: '使用户得到更好的体验',success: (res) => {wx.hideLoading()wx.request({url: app.SiteUrl + "getopenid",data: {code: that.data.code},method: 'GET',header: {'content-type': 'application/json'},success: function (res) {that.setData({session_key: res.data.data.session_key,openid: res.data.data.openid})wx.request({url: app.SiteUrl + "checkuser",data: {openid: res.data.data.openid},method: 'GET',header: {'content-type': 'application/json'},success: function (res) {if (res.data.data.phone == '') {that.setData({isShowUserInfo: false,isShowPhone: true,})}else if (res.data.data.nickname == '') {that.setData({isShowUserInfo: true,isShowPhone: false,})}else {that.setData({isShowPhone: false,isShowUserInfo: false,user_id:res.data.data.user_id,phone:res.data.data.phone,nickname:res.data.data.nickname,avatarimg:res.data.data.avatarimg,})wx.setStorageSync('user_id', res.data.data.user_id);wx.setStorageSync('phone', res.data.data.phone);wx.setStorageSync('nickname', res.data.data.nickname);wx.setStorageSync('avatarimg', res.data.data.avatarimg);} }})}}),that.setData({})},fail: res => {wx.hideLoading()console.log("获取用户信息失败", res)}})},getLogin() {//code获取成功,保存为当前页面的全局变量codewx.login({success: res => {this.setData({code: res.code})},fail: res => {//失败}})},getPhoneNumber: function (e) {// 登录wx.showLoading({title: '加载中...'})var self = thiswx.login({success: res => {wx.hideLoading()var encryptedData = encodeURI(e.detail.encryptedData) // 完整的加密数据var iv = e.detail.iv //加密算法的初始向量console.log('encryptedData', encryptedData)console.log('iv', iv)wx.request({url: app.SiteUrl + 'getPhoneNumber',data: {'session_key': self.data.session_key,'encryptedData': encryptedData,'iv': iv,},method: 'GET',header: {'content-type': 'application/json'},success: function (res) {wx.hideLoading()if (res.data.status == 1) {self.setData({phone: res.data.data.phoneNumber,isShowPhone: false,isShowUserInfo: false,})wx.setStorageSync('phone', res.data.data.phoneNumber);if (self.data.nickname == '') {wx.request({url: app.SiteUrl + 'login',data: {phone: self.data.phone,password: 888888,openid: self.data.openid,},success: function (res) {if (res.data != null) {wx.showToast({title: "" + res.data.msg + "",icon: 'loading',duration: 2000})self.setData({user_id: res.data.data.user_id,isShowPhone: false,isShowUserInfo: true,})wx.setStorageSync('user_id', res.data.data.user_id);}}});}}},fail: function (err) {console.log(err);}})}})},

TP5接口

    //登陆时先判断用户是否存在,OPENID唯一值public function api_checkuser() {$attr = $this->getattr();$openid = trim($attr['openid']);$usercount = db('user')->where(['openid' => $openid])->count();if ($usercount!=0) {$usercheck = db('user')->where(['openid' => $openid])->find();$newid = $usercheck['id'];$phone = $usercheck['phone'];$nickname = $usercheck['nickname'];$avatarimg=$usercheck['avatarimg'];$tiptext = "0";} else {$newid='';$phone = '';$nickname = '';$avatarimg='';$tiptext = "1";}$json = $this->message(1,$tiptext,["user_id" => $newid,"phone" => $phone,"nickname" => $nickname,"avatarimg" => $avatarimg]);return $this->returnjson($json, $attr['callback']);}//登陆/注册public function api_login() {$attr = $this->getattr();$phone = trim($attr['phone']);$password = trim($attr['password']);$openid = trim($attr['openid']);$nickname = trim($attr['nickname']);$avatarimg = trim($attr['avatarimg']);$gender = trim($attr['gender']);$params = ["phone" => $phone,"password" => md5($password),"openid" => $openid,"nickname" => $nickname,"avatarimg" => $avatarimg,"gender" => $gender,"status" => 1,"create_time" => time(),];$newid = 0;try {$newid = db('user')->where(['openid' => $openid])->count() >= 1 ? true : false;if (!$newid) {$newid = db('user')->insert($params, false, true, "id");$tiptext = "注册成功";} else {$newid = db('user')->where(['openid' => $openid])->find();$newid = $newid['id'];$tiptext = "登陆成功";}} catch (Exception $ex1) {$json = $this->message(0, "网络错误,请刷新后再填写!");return $this->returnjson($json, $attr['callback']);} catch (PDOException $ex) {$json = $this->message(0, "网络错误,请刷新后再填写!");return $this->returnjson($json, $attr['callback']);}if ($newid <= 0) {$json = $this->message(0, "网络错误,请刷新后再填写!");return $this->returnjson($json, $attr['callback']);}$json = $this->message(1, $tiptext, ["user_id" => $newid]);return $this->returnjson($json, $attr['callback']);}//更新头像和昵称public function api_setUserInfo() {$attr = $this->getattr();$user_id = trim($attr['user_id']);$nickname = trim($attr['nickname']);$avatarimg = trim($attr['avatarimg']);$params = ["nickname" => $nickname,"avatarimg" => $avatarimg,];$newid = 0;try {$newid = db('user')->where(['id' => $user_id])->count() >= 1 ? true : false;//   echo($newid);if (!$newid) {//   $newid = db('user')->update(['nickname' => 'thinkphp','avatarimg'=>1])->where('id',$user_id);//    echo($newid);$tiptext = "更新失败";} else {//    $tiptext = "更新失败";$newid = db('user')->where(['id'=>$user_id])->update($params);$tiptext = "更新成功";}} catch (Exception $ex1) {$json = $this->message(0, "网络错误,请刷新后再填写!");return $this->returnjson($json, $attr['callback']);} catch (PDOException $ex) {$json = $this->message(0, "网络错误,请刷新后再填写!");return $this->returnjson($json, $attr['callback']);}if ($newid <= 0) {$json = $this->message(0, "网络错误,请刷新后再填写!");return $this->returnjson($json, $attr['callback']);}$json = $this->message(1, $tiptext, ["user_id" => $newid]);return $this->returnjson($json, $attr['callback']);}

微信小程序授权登录wx.getUserProfile获取不到昵称及头像解决方案相关推荐

  1. 新版微信小程序授权登录流程及问题汇总(getUserProfile)

    问题来源:前不久去面试的时候有面试官问我你有自己的博客啥的吗?只能很尴尬的说没有.其实一直想有一个属于自己的博客啥的去记录自己在开发过程中遇到的问题,正好现在微信小程序比较流行,就花了两天自己搞了一个 ...

  2. uniapp微信小程序授权登录和获取微信绑定的手机号码

    uniapp微信小程序授权登录和获取微信绑定的手机号码 弹出授权的弹框 <view class="weixinOk" @tap="getUserProfile&qu ...

  3. SpringCloud 微信小程序授权登录 获取openId SessionKey【SpringCloud系列13】

    SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见. 自我提升方法推荐:神奇的早起 早上 5:00 -5:20 起床刷牙 5:30-6:00 晨练(跑步.跳绳.骑自行车.打球等等) ...

  4. 微信小程序授权登录+获取手机号

    微信小程序授权登录+获取手机号 一.官方文档背景: 小程序登录的链接: 微信小程序获取手机号的链接: 二.微信小程序授权登录+获取手机号 1.简单说明官方文档的操作:先授权登录后拿手机号 2.前端代码 ...

  5. 微信小程序授权登录 组件的封装

    微信小程序授权登录 组件的封装 新建components文件 写wxml文件 wxss部分 js部分 json引用 页面使用 页面js 授权登录 流程如下: 因为多个页面功能需要登录状态 所以做了个组 ...

  6. 微信小程序授权登录界面

    微信小程序授权登录界面 原先用的 wx.getUserInfo(Object object) ,现已改革 现用的 wx.getUserProfile(Object object),获取用户信息.每次请 ...

  7. 微信小程序授权登录页面(有提示窗)

    微信小程序授权登录(有弹窗提示) 1 效果显示 1.1 授权登录页面 1.2 授权登录提示弹窗 1.3 拒绝授权登录 1.4 允许登录后,跳转到小程序首页 2 代码 2.1 wxml 文件 <v ...

  8. SpringBoot微信小程序授权登录

    SpringBoot微信小程序授权登录 一.appId 1.1.自己是管理者:微信公众平台,申请或登录自己的微信小程序,在开发者管理中即可看到 2.2.自己是开发者:让管理员将自己加入到小程序开发者管 ...

  9. 微信小程序授权登录取消授权重新授权处理方法 附可用代码

    微信小程序授权登录基本是小程序的标配了,但是官方的demo,取消授权后,就不能再重新点击登录,除非重新加载小程序才可以,这下怎么办? 我们可以先在首页引导用户点击,然后跳转到一个新的页面,在新的页面进 ...

最新文章

  1. AI:2020年6月22日北京智源大会演讲分享之认知神经基础专题论坛——13:40-14:20毕彦超教授《Knowledge representation in the Human brain》
  2. Caffe学习系列(13):对训练好的模型进行fine-tune
  3. python类与继承person类_关于python中类的继承及self
  4. 论文浅尝 | 使用位置敏感的序列标注联合抽取实体和重叠关系
  5. 七彩影视双端新版本源码
  6. 关于RecyclerView(一)基本使用
  7. Mac 屏幕可不可以用酒精清洁?正确清洁 Mac 的方法
  8. LPDIRECTDRAW7编译错误的解决方法
  9. Tapestry 5 原则
  10. REST服务和RESTful API是什么
  11. c语言程序设计 网上资源,超星尔雅C语言程序设计-资源包完整答案
  12. Ryu学习总结(持续更新)
  13. 有没有谁做过完整的ptf上传下载
  14. 百度富文本编辑器插入视频链接相关问题
  15. 魔方机器人大赛——魔方步骤转换算法
  16. 2020作为一个资深的Android开发者需要掌握哪些技能?
  17. 基于重心距离识别的蠓虫分类模型(唯一能看的只有代码)
  18. Linear Regression Logistic Regression
  19. MySql 中 一次update更新多条数据
  20. pyhton爬诛仙小说

热门文章

  1. 中国信息化系统集成行业协会:计算机系统集成行业还有发展空间么?
  2. 客流统计大揭秘——各种客流
  3. 加速遗传算法 matlab,求加速遗传算法,不是自适应遗传算法
  4. C#读取dbf数据或者Excel转为DataTable
  5. 四川仁寿铧中学高考2021成绩查询,仁寿中学2021年排名
  6. NOI / 1.5编程基础之循环控制 08:多边形内角和
  7. 合肥工业大学2021汇编语言程序设计上机实验二
  8. [Android开发] Json解析工具类,一个类搞定Json的解析
  9. 2019 第十届蓝桥杯大赛软件类省赛 C/C++ 大学 A 组 【部分题解】
  10. ssm毕设项目一起学习吧s77u8(java+VUE+Mybatis+Maven+Mysql+sprnig)