飞道的博客

uniapp实现微信小程序登录功能

438人阅读  评论(0)

第一步 我们先把页面画出来


  
  1. <template>
  2. <!-- #ifdef MP-WEIXIN -->
  3. <view v-if="isCanUse">
  4. <view>
  5. <view class='headers'>
  6. <image src='../../static/img/logo.png'> </image>
  7. </view>
  8. <view class='content'>
  9. <view>申请获取以下权限 </view>
  10. <text>获得你的公开信息(昵称,头像、地区等) </text>
  11. </view>
  12. <button style="background: #E10A07;background-color: #E10A07;" class='bottom' type='primary' open-type="getUserInfo"
  13. withCredentials= "true" lang= "zh_CN" @ getuserinfo= "wxGetUserInfo">
  14. 授权登录
  15. </button>
  16. </view>
  17. </view>
  18. <!-- #endif -->
  19. </template>

第二步再methods中写下三个方法


  
  1. //第一授权获取用户信息===》按钮触发
  2. wxGetUserInfo() {
  3. let _this = this;
  4. uni.getUserInfo({
  5. provider: 'weixin',
  6. success: function(infoRes) {
  7. let nickName = infoRes.userInfo.nickName; //昵称
  8. let avatarUrl = infoRes.userInfo.avatarUrl; //头像
  9. try {
  10. uni.setStorageSync( 'isCanUse', false); //记录是否第一次授权 false:表示不是第一次授权
  11. if (_this.$queue.getData( "openid")) {
  12. //有openid就直接更新昵称和头像
  13. _this.getWeixinInfo(nickName, avatarUrl);
  14. } else {
  15. //没有openid就直接执行登录
  16. _this.login()
  17. }
  18. } catch (e) {}
  19. },
  20. fail(res) {}
  21. });
  22. },
  23. //登录 这个方法用于获取用户昵称和头像同步给后台接口
  24. getWeixinInfo(nickName, avatarUrl) {
  25. uni.showLoading({
  26. title: '登录中...'
  27. });
  28. this.$Request.postJson( "/user/mp/update", {
  29. invitation: this.invitation,
  30. unionid: this.$queue.getData( "unionid"),
  31. imageUrl: avatarUrl,
  32. nickName: nickName,
  33. openid: this.$queue.getData( "openid")
  34. }).then(res => {
  35. if (res.status === 0) {
  36. this.$queue.setData( "token", res.data.uuid);
  37. this.$queue.setData( "userId", res.data.userId);
  38. this.$queue.setData( "mobile", _this.phoneData);
  39. this.getUserInfo(res.data.userId, res.data.uuid);
  40. } else {
  41. uni.hideLoading();
  42. uni.showModal({
  43. showCancel: false,
  44. title: '登录失败',
  45. content: res.msg,
  46. });
  47. }
  48. });
  49. },
  50. //使用到的登录接口
  51. login() {
  52. let _this = this;
  53. uni.showLoading({
  54. title: '登录中...'
  55. });
  56. // 1.wx获取登录用户code
  57. uni.login({
  58. provider: 'weixin',
  59. success: function(loginRes) {
  60. //获取到code 拿着code去请求后台接口换openid和unionid 这里说到unionid只有关联了微信开放平台才会有
  61. let code = loginRes.code;
  62. _this.$Request.getT( "/wx/mp/login?code=" + code).then(res => {
  63. if (res.status === 0) {
  64. _this.$queue.setData( "openid", res.data.open_id)
  65. _this.$queue.setData( "unionid", res.data.unionid)
  66. uni.hideLoading()
  67. //非第一次授权获取用户信息
  68. uni.getUserInfo({
  69. provider: 'weixin',
  70. success: function(infoRes) { //获取用户信息后向调用信息更新方法
  71. let nickName = infoRes.userInfo.nickName; //昵称
  72. let avatarUrl = infoRes.userInfo.avatarUrl; //头像
  73. if (nickName) {
  74. _this.getWeixinInfo(nickName, avatarUrl);
  75. }
  76. }
  77. });
  78. }
  79. });
  80. },
  81. });
  82. },

具体可以参考这个:https://ext.dcloud.net.cn/plugin?id=1798


转载:https://blog.csdn.net/weixin_39706415/article/details/111588465
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场