当前位置:   article > 正文

微信小程序授权登录wx.getUserProfile获取不到昵称及头像解决方案_getuserprofile怎么解决

getuserprofile怎么解决

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

 解决方案

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

 

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

 

 

 

 

wxml代码 

  1. <view class="index">
  2. <view class="head">
  3. <view class="headbox">
  4. <view class="position">
  5. <view class="face">
  6. <view class="img">
  7. <block wx:if="{{avatarimg}}">
  8. <image class="self" mode="widthFix" src="{{avatarimg}}"></image>
  9. </block>
  10. <block wx:else>
  11. <image mode="widthFix" src="headlogonew.png"></image>
  12. </block>
  13. </view>
  14. <block wx:if="{{nickname}}">
  15. <view class="name">{{nickname}}</view>
  16. </block>
  17. <block wx:else>
  18. <view class="name" bindtap="getUserProfile">点击登录/注册</view>
  19. </block>
  20. </view>
  21. <block wx:if="{{isShowPhone}}">
  22. <view class="dialog-mask"></view>
  23.   <view class="dialog-info">
  24. <view class="dialog-title">请点击获取授权选择绑定的手机号</view>
  25. <view class="dialog-footer"><button class="button" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取授权</button></view>
  26. </view>
  27. </block> 
  28. <block wx:if="{{isShowUserInfo}}">
  29. <view class="dialog-mask"></view>
  30.   <view class="dialog-info">
  31. <view class="dialog-title">请设置头像和昵称</view>
  32. <view class="dialog-footer dialog-line">
  33. <div class="set">
  34. <button open-type="chooseAvatar" bindchooseavatar="onChooseAvatar">
  35. <block wx:if="{{avatarimg}}">
  36. <image mode="widthFix" src="{{avatarimg}}"></image>
  37. </block>
  38. <block wx:else>
  39. <image mode="widthFix" src="/images/uploadface.png"></image>
  40. </block>
  41. </button>
  42. </div>
  43. <div class="set">
  44. <input placeholder="请输入您的昵称" type="nickname" value="{{nickname}}" bindblur="bindblur"></input>
  45. </div>
  46. <div class="set">
  47. <button class="button" bindtap="setBtnTap">保存</button>
  48. </div>
  49. </view>
  50. </view>
  51. </block> 
  52. <div class="clear"></div>
  53. </view>
  54. </view>
  55. </view>
  56. </view>

js代码

  1. /**
  2. * 页面的初始数据
  3. */
  4. data: {
  5. isShowPhone: false, //默认先不显示获取手机号
  6. isShowUserInfo: false,
  7. user_id: '',
  8. phone: '',
  9. avatarimg: '',
  10. nickname:'',
  11. },
  12. /** 获取昵称信息 */
  13. bindblur(e) {
  14. this.data.nickname = e.detail.value
  15. },
  16. /** 更换头像 */
  17. onChooseAvatar(e) {
  18. var that = this
  19. wx.getFileSystemManager().readFile({
  20. filePath: e.detail.avatarUrl, //选择图片返回的相对路径
  21. encoding: 'base64',
  22. success: res => {
  23. wx.request({
  24. url: app.SiteUrl + "imgupload",
  25. data: {
  26. file: 'data:image/png;base64,' + res.data,
  27. },
  28. header: {
  29. 'content-type': 'multipart/form-data'
  30. },
  31. success: function (res) {
  32. if (res.data.status == 'success') {
  33. that.setData({
  34. avatarimg: res.data.data.image_name,
  35. //isShowUserInfo: false,
  36. })
  37. } else {
  38. console.log('fail');
  39. }
  40. }
  41. })
  42. console.log('data:image/png;base64,' + res.data)
  43. }
  44. })
  45. },
  46. setBtnTap() {
  47. var that = this
  48. if (that.data.avatarimg.length == 0) {
  49. wx.showToast({
  50. title: '请上传头像!',
  51. icon: 'error',
  52. duration: 1500
  53. })
  54. } else if (that.data.nickname.length == 0) {
  55. wx.showToast({
  56. title: '请填写昵称!',
  57. icon: 'error',
  58. duration: 1500
  59. })
  60. } else {
  61. wx.request({
  62. url: app.SiteUrl + "setUserInfo",
  63. data: {
  64. user_id: that.data.user_id,
  65. nickname: that.data.nickname,
  66. avatarimg: that.data.avatarimg,
  67. },
  68. method: 'GET',
  69. header: {
  70. 'content-type': 'application/json'
  71. },
  72. success: function (res) {
  73. if (res.data.data.phone != '') {
  74. that.setData({
  75. isShowPhone: false,
  76. isShowUserInfo: false,
  77. nickname: that.data.nickname,
  78. })
  79. wx.setStorageSync('user_id', res.data.data.user_id);
  80. wx.setStorageSync('phone', res.data.data.phone);
  81. wx.setStorageSync('nickname', that.data.nickname);
  82. wx.setStorageSync('avatarimg', that.data.avatarimg);
  83. wx.showToast({
  84. title: '更新成功!',
  85. icon: 'success',
  86. duration: 1500
  87. });
  88. } else {
  89. that.setData({
  90. isShowUserInfo: false,
  91. isShowPhone: true,
  92. })
  93. }
  94. }
  95. })
  96. }
  97. },
  98. getUserProfile() {
  99. var that = this
  100. wx.showLoading({
  101. title: '获取授权中'
  102. })
  103. wx.getUserProfile({
  104. desc: '使用户得到更好的体验',
  105. success: (res) => {
  106. wx.hideLoading()
  107. wx.request({
  108. url: app.SiteUrl + "getopenid",
  109. data: {
  110. code: that.data.code
  111. },
  112. method: 'GET',
  113. header: {
  114. 'content-type': 'application/json'
  115. },
  116. success: function (res) {
  117. that.setData({
  118. session_key: res.data.data.session_key,
  119. openid: res.data.data.openid
  120. })
  121. wx.request({
  122. url: app.SiteUrl + "checkuser",
  123. data: {
  124. openid: res.data.data.openid
  125. },
  126. method: 'GET',
  127. header: {
  128. 'content-type': 'application/json'
  129. },
  130. success: function (res) {
  131. if (res.data.data.phone == '') {
  132. that.setData({
  133. isShowUserInfo: false,
  134. isShowPhone: true,
  135. })
  136. }else if (res.data.data.nickname == '') {
  137. that.setData({
  138. isShowUserInfo: true,
  139. isShowPhone: false,
  140. })
  141. }else {
  142. that.setData({
  143. isShowPhone: false,
  144. isShowUserInfo: false,
  145. user_id:res.data.data.user_id,
  146. phone:res.data.data.phone,
  147. nickname:res.data.data.nickname,
  148. avatarimg:res.data.data.avatarimg,
  149. })
  150. wx.setStorageSync('user_id', res.data.data.user_id);
  151. wx.setStorageSync('phone', res.data.data.phone);
  152. wx.setStorageSync('nickname', res.data.data.nickname);
  153. wx.setStorageSync('avatarimg', res.data.data.avatarimg);
  154. }
  155. }
  156. })
  157. }
  158. }),
  159. that.setData({
  160. })
  161. },
  162. fail: res => {
  163. wx.hideLoading()
  164. console.log("获取用户信息失败", res)
  165. }
  166. })
  167. },
  168. getLogin() {
  169. //code获取成功,保存为当前页面的全局变量code
  170. wx.login({
  171. success: res => {
  172. this.setData({
  173. code: res.code
  174. })
  175. },
  176. fail: res => {
  177. //失败
  178. }
  179. })
  180. },
  181. getPhoneNumber: function (e) {
  182. // 登录
  183. wx.showLoading({
  184. title: '加载中...'
  185. })
  186. var self = this
  187. wx.login({
  188. success: res => {
  189. wx.hideLoading()
  190. var encryptedData = encodeURI(e.detail.encryptedData) // 完整的加密数据
  191. var iv = e.detail.iv //加密算法的初始向量
  192. console.log('encryptedData', encryptedData)
  193. console.log('iv', iv)
  194. wx.request({
  195. url: app.SiteUrl + 'getPhoneNumber',
  196. data: {
  197. 'session_key': self.data.session_key,
  198. 'encryptedData': encryptedData,
  199. 'iv': iv,
  200. },
  201. method: 'GET',
  202. header: {
  203. 'content-type': 'application/json'
  204. },
  205. success: function (res) {
  206. wx.hideLoading()
  207. if (res.data.status == 1) {
  208. self.setData({
  209. phone: res.data.data.phoneNumber,
  210. isShowPhone: false,
  211. isShowUserInfo: false,
  212. })
  213. wx.setStorageSync('phone', res.data.data.phoneNumber);
  214. if (self.data.nickname == '') {
  215. wx.request({
  216. url: app.SiteUrl + 'login',
  217. data: {
  218. phone: self.data.phone,
  219. password: 888888,
  220. openid: self.data.openid,
  221. },
  222. success: function (res) {
  223. if (res.data != null) {
  224. wx.showToast({
  225. title: "" + res.data.msg + "",
  226. icon: 'loading',
  227. duration: 2000
  228. })
  229. self.setData({
  230. user_id: res.data.data.user_id,
  231. isShowPhone: false,
  232. isShowUserInfo: true,
  233. })
  234. wx.setStorageSync('user_id', res.data.data.user_id);
  235. }
  236. }
  237. });
  238. }
  239. }
  240. },
  241. fail: function (err) {
  242. console.log(err);
  243. }
  244. })
  245. }
  246. })
  247. },

TP5接口

  1. //登陆时先判断用户是否存在,OPENID唯一值
  2. public function api_checkuser() {
  3. $attr = $this->getattr();
  4. $openid = trim($attr['openid']);
  5. $usercount = db('user')->where(['openid' => $openid])->count();
  6. if ($usercount!=0) {
  7. $usercheck = db('user')->where(['openid' => $openid])->find();
  8. $newid = $usercheck['id'];
  9. $phone = $usercheck['phone'];
  10. $nickname = $usercheck['nickname'];
  11. $avatarimg=$usercheck['avatarimg'];
  12. $tiptext = "0";
  13. } else {
  14. $newid='';
  15. $phone = '';
  16. $nickname = '';
  17. $avatarimg='';
  18. $tiptext = "1";
  19. }
  20. $json = $this->message(1,$tiptext,["user_id" => $newid,"phone" => $phone,"nickname" => $nickname,"avatarimg" => $avatarimg]);
  21. return $this->returnjson($json, $attr['callback']);
  22. }
  23. //登陆/注册
  24. public function api_login() {
  25. $attr = $this->getattr();
  26. $phone = trim($attr['phone']);
  27. $password = trim($attr['password']);
  28. $openid = trim($attr['openid']);
  29. $nickname = trim($attr['nickname']);
  30. $avatarimg = trim($attr['avatarimg']);
  31. $gender = trim($attr['gender']);
  32. $params = [
  33. "phone" => $phone,
  34. "password" => md5($password),
  35. "openid" => $openid,
  36. "nickname" => $nickname,
  37. "avatarimg" => $avatarimg,
  38. "gender" => $gender,
  39. "status" => 1,
  40. "create_time" => time(),
  41. ];
  42. $newid = 0;
  43. try {
  44. $newid = db('user')->where(['openid' => $openid])->count() >= 1 ? true : false;
  45. if (!$newid) {
  46. $newid = db('user')->insert($params, false, true, "id");
  47. $tiptext = "注册成功";
  48. } else {
  49. $newid = db('user')->where(['openid' => $openid])->find();
  50. $newid = $newid['id'];
  51. $tiptext = "登陆成功";
  52. }
  53. } catch (Exception $ex1) {
  54. $json = $this->message(0, "网络错误,请刷新后再填写!");
  55. return $this->returnjson($json, $attr['callback']);
  56. } catch (PDOException $ex) {
  57. $json = $this->message(0, "网络错误,请刷新后再填写!");
  58. return $this->returnjson($json, $attr['callback']);
  59. }
  60. if ($newid <= 0) {
  61. $json = $this->message(0, "网络错误,请刷新后再填写!");
  62. return $this->returnjson($json, $attr['callback']);
  63. }
  64. $json = $this->message(1, $tiptext, ["user_id" => $newid]);
  65. return $this->returnjson($json, $attr['callback']);
  66. }
  67. //更新头像和昵称
  68. public function api_setUserInfo() {
  69. $attr = $this->getattr();
  70. $user_id = trim($attr['user_id']);
  71. $nickname = trim($attr['nickname']);
  72. $avatarimg = trim($attr['avatarimg']);
  73. $params = [
  74. "nickname" => $nickname,
  75. "avatarimg" => $avatarimg,
  76. ];
  77. $newid = 0;
  78. try {
  79. $newid = db('user')->where(['id' => $user_id])->count() >= 1 ? true : false;
  80. // echo($newid);
  81. if (!$newid) {
  82. // $newid = db('user')->update(['nickname' => 'thinkphp','avatarimg'=>1])->where('id',$user_id);
  83. // echo($newid);
  84. $tiptext = "更新失败";
  85. } else {
  86. // $tiptext = "更新失败";
  87. $newid = db('user')->where(['id'=>$user_id])->update($params);
  88. $tiptext = "更新成功";
  89. }
  90. } catch (Exception $ex1) {
  91. $json = $this->message(0, "网络错误,请刷新后再填写!");
  92. return $this->returnjson($json, $attr['callback']);
  93. } catch (PDOException $ex) {
  94. $json = $this->message(0, "网络错误,请刷新后再填写!");
  95. return $this->returnjson($json, $attr['callback']);
  96. }
  97. if ($newid <= 0) {
  98. $json = $this->message(0, "网络错误,请刷新后再填写!");
  99. return $this->returnjson($json, $attr['callback']);
  100. }
  101. $json = $this->message(1, $tiptext, ["user_id" => $newid]);
  102. return $this->returnjson($json, $attr['callback']);
  103. }

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

闽ICP备14008679号