• 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. 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>

    获取用户昵称

    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. }

  • 相关阅读:
    常用web服务器性能相关概念
    【数据结构】堆排序及TOP-K问题
    LeetCode 视频演示:0654. 最大二叉树(视频做了近2h~_~)
    web前端期末大作业 基于HTML+CSS+JavaScript程序员个人博客模板(web学生作业源码)
    701、450、二叉搜索树的插入和删除
    包管理器
    信息学奥赛一本通:1153:绝对素数
    大根堆的创建(视频讲解)
    Unity使用Visual Studio Code 调试
    小程序2.
  • 原文地址:https://blog.csdn.net/tong200617/article/details/133837963