index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view class="content">
  3. <view class="user">
  4. <view class="user-avatar">
  5. <image class="user-avatar-image" :src="avatar"></image>
  6. </view>
  7. <view class="user-info">{{username}}</view>
  8. </view>
  9. <view class="nav-list">
  10. <view class="nav-list-item" @tap="openServerSetting">
  11. <view class="nav-list-text">设置服务器地址</view>
  12. <uni-icons type="right"></uni-icons>
  13. </view>
  14. </view>
  15. <view class="flex padding-lr-20" v-if="isLogin">
  16. <button class="grow-1" size="mini" type="warn" @tap="logOff">注销</button>
  17. </view>
  18. <view class="flex padding-lr-20" v-else>
  19. <button class="grow-1" size="mini" type="primary" @tap="login">登录</button>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import extend from '../../utils/extend.js';
  25. import token from '../../utils/token.js';
  26. import user from '../../utils/user.js';
  27. let userinfo = null;
  28. export default {
  29. data() {
  30. return {
  31. avatar: '/static/logo.png',
  32. username: '',
  33. isLogin: false,
  34. }
  35. },
  36. async onShow() {
  37. if (token.has()) {
  38. userinfo = user.get();
  39. this.isLogin = true;
  40. this.username = userinfo.userName;
  41. } else {
  42. let res = await token.getByServer();
  43. if (res.success) {
  44. let infoRes = user.getByServer();
  45. if (infoRes.success) {
  46. userinfo = infoRes.data;
  47. this.isLogin = true;
  48. this.username = userinfo.userName;
  49. } else {
  50. token.clear();
  51. }
  52. }
  53. }
  54. },
  55. methods: {
  56. openLogin() {
  57. uni.navigateTo({
  58. url: '/pages/login/index'
  59. });
  60. },
  61. openServerSetting() {
  62. uni.navigateTo({
  63. url: '/pages/login/serverSetting'
  64. });
  65. },
  66. logOff() {
  67. let _this = this;
  68. uni.showModal({
  69. content: '要注销登录吗?',
  70. success(r) {
  71. if (r.confirm) {
  72. token.clear();
  73. user.clear();
  74. user.clearLoginInfo();
  75. _this.login();
  76. }
  77. }
  78. })
  79. },
  80. login() {
  81. uni.navigateTo({
  82. url: '/pages/login/index?type=switchTab&page=/pages/my/index'
  83. });
  84. }
  85. }
  86. }
  87. </script>
  88. <style lang="scss">
  89. .content {
  90. position: relative;
  91. background-color: #efefef;
  92. height: 100vh;
  93. }
  94. .user {
  95. position: relative;
  96. display: flex;
  97. flex-direction: column;
  98. justify-content: center;
  99. align-items: center;
  100. background-color: #ffffff;
  101. padding: 30upx;
  102. border-radius: 0 0 20upx 20upx;
  103. .user-avatar {
  104. position: relative;
  105. display: flex;
  106. width: 150upx;
  107. height: 150upx;
  108. justify-content: center;
  109. align-items: center;
  110. overflow: hidden;
  111. border-radius: 80upx;
  112. .user-avatar-image {
  113. width: 150upx;
  114. height: 150upx;
  115. }
  116. }
  117. .user-info {
  118. margin-top: 30upx;
  119. }
  120. }
  121. </style>