index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="page-container uni-column">
  3. <view class="title uni-column" v-if="loggedUsers.length > 0">
  4. <text>点击头像切换用户</text>
  5. </view>
  6. <view v-for="(item, index) in loggedUsers" class="item-user uni-row" @click="handleSelectUser(item)"
  7. @longpress="handleLongPressUser(item)">
  8. <view class="user-avatar uni-row"><text class="label">{{item.nickname}}</text></view>
  9. <view class="user-info">
  10. <view class="nickname">
  11. <text class="label">{{item.nickName}}</text>
  12. </view>
  13. <view class="username">
  14. <text class="label">{{item.userName}}</text>
  15. </view>
  16. </view>
  17. </view>
  18. <view class="add-user-btn uni-row" @click="handleShowLoginDialog(null)">
  19. <view class="icon uni-row">
  20. <text class="label">+</text>
  21. </view>
  22. <view class="btn-label">
  23. <text class="label">添加新账号</text>
  24. </view>
  25. </view>
  26. <dialog-login ref="loginDialog" />
  27. </view>
  28. </template>
  29. <script setup>
  30. import {
  31. onMounted,
  32. ref
  33. } from 'vue'
  34. import {
  35. onLoad,
  36. onReady
  37. } from '@dcloudio/uni-app'
  38. import {
  39. getUserInfo
  40. } from '@/api/login/index.js'
  41. // 登录过的用户
  42. const loggedUsers = ref([])
  43. const loginDialog = ref(null)
  44. const userInfo = ref({})
  45. const users = ref([])
  46. const nickName = ref('')
  47. onLoad(() => {
  48. init();
  49. // getUser();
  50. })
  51. function init() {
  52. uni.getStorageInfo({
  53. success: function(res) {
  54. console.log(res.keys)
  55. for (let i = 0; i < res.keys.length; i++) {
  56. let storagekey = res.keys[i];
  57. console.log(storagekey)
  58. uni.getStorage({
  59. key: storagekey,
  60. success: function(res) {
  61. getUserInfo().then((res)=> {
  62. userInfo.value = res.data;
  63. var nick = userInfo.value.nickName;
  64. if (storagekey !== 'token' && storagekey !== '__DC_STAT_UUID') {
  65. loggedUsers.value.push({
  66. userName: storagekey,
  67. password: res.data,
  68. nickName: nick
  69. })
  70. }
  71. })
  72. }
  73. });
  74. }
  75. }
  76. });
  77. }
  78. const handleShowLoginDialog = (user) => {
  79. let _user = user ?? {}
  80. // 调用子组件中的方法
  81. loginDialog.value.open(_user)
  82. }
  83. const handleSelectUser = (user) => {
  84. handleShowLoginDialog(user)
  85. }
  86. const handleLongPressUser = (user) => {
  87. console.log(user)
  88. uni.showModal({
  89. title: '提示', // 标题
  90. content: '是否删除此账号', // 提示内容
  91. cancelText: "取消", // 取消按钮的文字
  92. confirmText: "确认", // 确认按钮的文字
  93. //点击确定之后执行的代码
  94. success(res) {
  95. if (res.confirm) {
  96. uni.removeStorage({
  97. key: user.userName, // 键名
  98. success: () => {
  99. uni.showToast({
  100. title: '删除成功',
  101. icon: 'success',
  102. duration: 3000,
  103. });
  104. loggedUsers.value = [];
  105. init();
  106. }, // 成功回调函数
  107. fail: () => {
  108. }, // 失败回调函数
  109. })
  110. }
  111. }
  112. });
  113. }
  114. const handleAddUser = () => {
  115. uni.setStorage({
  116. key: 'logged-users',
  117. data: {
  118. users: ''
  119. },
  120. success: () => {
  121. }
  122. })
  123. }
  124. </script>
  125. <style lang="scss">
  126. .title {
  127. height: 128rpx;
  128. justify-content: center;
  129. align-items: center;
  130. }
  131. .item-user {
  132. margin: 32rpx;
  133. margin-top: 0;
  134. padding: 32rpx;
  135. background-color: #FFFFFF;
  136. align-items: center;
  137. .user-avatar {
  138. width: 128rpx;
  139. height: 128rpx;
  140. border-radius: 64rpx;
  141. background-color: #F1F2F3;
  142. justify-content: center;
  143. align-items: center;
  144. .label {
  145. font-size: 64rpx;
  146. }
  147. }
  148. .user-info {
  149. flex: 1;
  150. height: 96rpx;
  151. padding-left: 32rpx;
  152. .nickname {
  153. .label {
  154. font-weight: bold;
  155. font-size: 40rpx;
  156. }
  157. }
  158. .username {
  159. margin-top: 8rpx;
  160. .label {
  161. font-size: 32rpx;
  162. }
  163. }
  164. }
  165. }
  166. .add-user-btn {
  167. margin: 32rpx;
  168. padding: 32rpx;
  169. background-color: #FFFFFF;
  170. align-items: center;
  171. .icon {
  172. width: 128rpx;
  173. height: 128rpx;
  174. align-items: center;
  175. justify-content: center;
  176. background-color: #F1F2F3;
  177. border-radius: 64rpx;
  178. .label {
  179. color: #FFFFFF;
  180. font-size: 128rpx;
  181. }
  182. }
  183. .btn-label {
  184. flex: 1;
  185. padding-left: 32rpx;
  186. .label {
  187. font-size: 40rpx;
  188. }
  189. }
  190. }
  191. </style>