当前位置:   article > 正文

uniapp获取微信用户信息登录_uniapp获取微信号

uniapp获取微信号

 想要获取用户信息需要先使用wx.login功能

  1. wx.login({
  2. success: res => {
  3. if (res.code) {
  4. // 获取 code 成功后,通过微信开放接口获取用户 openid
  5. wx.request({
  6. url: '后台接口',
  7. data: {
  8. code: res.code,
  9. },
  10. success: res => {
  11. console.log(res.data.openid);
  12. this.openid = res.data.openid
  13. uni.setStorageSync('openid', res.data.openid)
  14. }
  15. });
  16. }
  17. },
  18. fail: err => {
  19. console.log(err);
  20. }
  21. });

然后使用按钮的open-type获取用户的头像,想要获取完整用户信息可以使用getUserInfo。

  1. <view class="">
  2. // 调用微信开放能力
  3. <button open-type="chooseAvatar" @chooseavatar="chooseAvatar"
  4. style="width:190rpx;height: 190rpx;border-radius: 50%;padding: 0;margin-top: 20px;">
  5. <image :src="image" class="imageAsdf" style="width: 200rpx;height: 200rpx;border-radius: 100%;" />
  6. </button>
  7. </view>

获取用户昵称

  1. <view style="width: 60%;margin: auto;margin-top: 10px;">
  2. <input id="nickname-input" v-model="nickname" class="authorization white" type="nickname"
  3. placeholder="请输入用户昵称" style="width: 100%;text-align: center;" @change="change">
  4. </view>

 保存头像和昵称

  1. change(e) {
  2. this.nickname = e.detail.value
  3. },
  4. chooseAvatar(e) {
  5. console.log(e);
  6. this.image = e.detail.avatarUrl
  7. },

登录判断:

  1. // 登录
  2. logins() {
  3. let that = this;
  4. if (that.nickname == '') {
  5. uni.showToast({
  6. icon: 'none',
  7. title: '请输入用户昵称',
  8. // duration: 2000
  9. });
  10. } else if (that.image == '') {
  11. uni.showToast({
  12. icon: 'none',
  13. title: '请上传用户头像',
  14. // duration: 2000
  15. });
  16. }
  17. if (that.nickname != '' && that.image != '') {
  18. uni.request({
  19. url: '登录接口',
  20. //参数
  21. data: {
  22. openid: that.openid,
  23. value:value
  24. },
  25. dataType: 'json',
  26. method: "POST",
  27. success(res) {
  28. console.log(res);
  29. if (res.data.err == '请求成功') {
  30. console.log(res.data.userid);
  31. uni.setStorageSync('userid', res.data.userid);
  32. uni.showToast({
  33. title: '登录成功',
  34. duration: 2000
  35. });
  36. // 跳转路径
  37. let path = uni.getStorageSync('paths');
  38. setTimeout(() => {
  39. uni.reLaunch({
  40. url:`/pages/${path}/${path}`
  41. })
  42. }, 2000)
  43. } else {
  44. uni.showToast({
  45. title: '登录失败',
  46. icon:'none',
  47. duration: 2000
  48. });
  49. }
  50. }
  51. })
  52. }
  53. }

效果演示:

 

 

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