小程序登录、用户信息相关接口调整说明:https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801考虑到近期开发者对小程序登录、用户信息相关接口调整的相关反馈,为优化开发者调整接口的体验,回收wx.getUserInfo接口可获取用户授权的个人信息能力的截止时间由2021年4月13日调整至2021年4月28日24时。

    所以无法使用wx.getUserInfo来做微信授权了,而新增加了getUserProfile
    使用步骤

    1. <button type="primary" @click="loginTest">授权登录</button>
    2. loginTest() { // 获取用户信息
    3. var that = this
    4. let code = '';
    5. wx.login({
    6. success: (res) => {
    7. code = res.code
    8. console.log(res.code);
    9. }
    10. })
    11. uni.getUserProfile({
    12. desc: '登录',
    13. success: async (res) => {
    14. // 后端接口
    15. const data = await that.api('/appWxLogin', 'POST', {
    16. code: code,
    17. encryptedData:res.encryptedData,
    18. iv:res.iv
    19. })
    20. console.log(data);
    21. },
    22. fail: (res) => {
    23. console.log(res)
    24. }
    25. });
    26. },

    项目代码:

    1. <template>
    2. <view>
    3. <image src="../../static/logo-2.png" mode="widthFix" class="logo"></image>
    4. <view class="register-container">
    5. <input type="text" placeholder="输入你的邀请码" class="register-code" maxlength="6" v-model="registerCode" />
    6. <view class="register-desc">管理员创建员工证账号之后,你可以从你的个人邮箱中获得注册邀请码</view>
    7. <button class="register-btn" @click="register()">执行注册</button>
    8. </view>
    9. </view>
    10. </template>
    11. <script>
    12. export default {
    13. data() {
    14. return {
    15. registerCode:""
    16. };
    17. },
    18. methods: {
    19. register: function() {
    20. let that=this
    21. if(that.registerCode==null||that.registerCode.length==0){
    22. uni.showToast({
    23. icon:"none",
    24. title:"邀请码不能为空"
    25. })
    26. return
    27. }
    28. else if(/^[0-9]{6}$/.test(that.registerCode)==false){
    29. uni.showToast({
    30. icon:"none",
    31. title:"邀请码必须是6位数字"
    32. })
    33. return
    34. }
    35. let code = '';
    36. wx.login({
    37. success: (res) => {
    38. code = res.code
    39. console.log(res.code);
    40. }
    41. })
    42. // wx.login({
    43. // provider: 'weixin',
    44. // success: function(resp) {
    45. // console.log(resp.code)
    46. // let code = resp.code;
    47. uni.getUserProfile({
    48. desc: '微信用户登录',
    49. success: function(resp) {
    50. let nickName = resp.userInfo.nickName;
    51. let avatarUrl = resp.userInfo.avatarUrl;
    52. console.log(nickName);
    53. console.log(avatarUrl);
    54. let data={
    55. code:code,
    56. nickname:nickName,
    57. photo:avatarUrl,
    58. registerCode:that.registerCode
    59. }
    60. that.ajax(that.url.register,"POST",data,function(resp){
    61. let permission=resp.data.permission
    62. uni.setStorageSync("permission",permission)
    63. console.log(permission)
    64. //跳转到index页面
    65. // uni.switchTab({
    66. // url:"../index/index"
    67. // })
    68. })
    69. }
    70. });
    71. // }
    72. // });
    73. }
    74. }
    75. };
    76. </script>
    77. <style lang="less">
    78. @import url('register.less');
    79. </style>