get-user-info.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view style="background:#FFF; padding:40rpx;">
  6. <block v-if="hasUserInfo === false">
  7. <view class="uni-hello-text uni-center">
  8. <text>请点击下方按钮获取用户头像及昵称或手机号</text>
  9. </view>
  10. </block>
  11. <block v-if="hasUserInfo === true">
  12. <view class="uni-h4 uni-center uni-common-mt">{{userInfo.nickName || userInfo.nickname || userInfo.gender || userInfo.email || userInfo.phoneNumber}}</view>
  13. <view v-if="userInfo.avatarUrl || userInfo.avatar_url " style="padding:30rpx 0; text-align:center;">
  14. <image class="userinfo-avatar" :src="userInfo.avatarUrl||userInfo.avatar_url"></image>
  15. </view>
  16. </block>
  17. </view>
  18. <view class="uni-btn-v">
  19. <!-- #ifdef APP-PLUS || MP-ALIPAY || MP-TOUTIAO -->
  20. <button type="primary" :loading="btnLoading" @click="getUserInfo">获取用户信息</button>
  21. <!-- #endif -->
  22. <!-- #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-JD -->
  23. <button type="primary" open-type="getUserInfo" @getuserinfo="mpGetUserInfo">获取用户信息</button>
  24. <!-- #endif -->
  25. <button @click="clear">清空</button>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import {
  32. mapState,
  33. mapMutations,
  34. mapActions
  35. } from 'vuex'
  36. export default {
  37. data() {
  38. return {
  39. title: 'getUserInfo',
  40. hasUserInfo: false,
  41. userInfo: {},
  42. btnLoading: false
  43. }
  44. },
  45. computed: {
  46. ...mapState([
  47. 'loginProvider',
  48. 'isUniverifyLogin'
  49. ])
  50. },
  51. methods: {
  52. ...mapActions(['getPhoneNumber']),
  53. // 获取用户信息 API 在小程序可直接使用,在 5+App 里面需要先登录才能调用
  54. getUserInfo() {
  55. this.btnLoading = true;
  56. if (this.isUniverifyLogin) {
  57. // 一键登录
  58. this.getPhoneNumber(uni.getStorageSync('univerifyInfo')).then(phoneNumber => {
  59. this.hasUserInfo = true;
  60. this.userInfo = {
  61. phoneNumber
  62. };
  63. }).catch(err => {
  64. console.error('getUserInfo fail:', err);
  65. this.hasUserInfo = false;
  66. }).finally(() => {
  67. this.btnLoading = false;
  68. })
  69. return;
  70. }
  71. if(this.loginProvider === 'apple'){
  72. const nickname = uni.getStorageSync('apple_nickname')
  73. if(nickname){
  74. this.hasUserInfo = true;
  75. this.userInfo = { nickName:nickname }
  76. this.btnLoading = false;
  77. return;
  78. }
  79. }
  80. uni.getUserInfo({
  81. provider: this.loginProvider,
  82. success: (result) => {
  83. this.hasUserInfo = true;
  84. this.userInfo = result.userInfo;
  85. },
  86. fail: (error) => {
  87. console.log('getUserInfo fail', error);
  88. let content = error.errMsg;
  89. if (~content.indexOf('uni.login')) {
  90. content = '请在登录页面完成登录操作';
  91. }
  92. // #ifndef APP-PLUS
  93. uni.getSetting({
  94. success: (res) => {
  95. let authStatus = res.authSetting['scope.userInfo'];
  96. if (!authStatus) {
  97. uni.showModal({
  98. title: '授权失败',
  99. content: 'Hello uni-app需要获取您的用户信息,请在设置界面打开相关权限',
  100. success: (res) => {
  101. if (res.confirm) {
  102. uni.openSetting()
  103. }
  104. }
  105. })
  106. } else {
  107. uni.showModal({
  108. title: '获取用户信息失败',
  109. content: '错误原因' + content,
  110. showCancel: false
  111. });
  112. }
  113. }
  114. })
  115. // #endif
  116. // #ifdef APP-PLUS
  117. uni.showModal({
  118. title: '获取用户信息失败',
  119. content: '错误原因' + content,
  120. showCancel: false
  121. });
  122. // #endif
  123. },
  124. complete: () => {
  125. this.btnLoading = false;
  126. }
  127. });
  128. },
  129. mpGetUserInfo(result) {
  130. console.log('mpGetUserInfo', result);
  131. if (result.detail.errMsg !== 'getUserInfo:ok') {
  132. uni.showModal({
  133. title: '获取用户信息失败',
  134. content: '错误原因' + result.detail.errMsg,
  135. showCancel: false
  136. });
  137. return;
  138. }
  139. this.hasUserInfo = true;
  140. if(result.detail && result.detail.userInfo){
  141. this.userInfo = result.detail.userInfo;
  142. }else{
  143. // #ifdef MP-JD
  144. this.userInfo = result.detail.user_info;
  145. // #endif
  146. }
  147. },
  148. clear() {
  149. this.hasUserInfo = false;
  150. this.userInfo = {};
  151. }
  152. }
  153. }
  154. </script>
  155. <style>
  156. .userinfo-avatar {
  157. border-radius: 128rpx;
  158. width: 128rpx;
  159. height: 128rpx;
  160. }
  161. </style>