index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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']}}-{{index}}</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. // 登录过的用户
  39. const loggedUsers = ref([])
  40. const loginDialog = ref(null)
  41. onLoad(() => {
  42. init();
  43. })
  44. function init() {
  45. uni.getStorageInfo({
  46. success: function(res) {
  47. console.log(res.keys)
  48. for (let i = 0; i < res.keys.length; i++) {
  49. let storagekey = res.keys[i];
  50. console.log(storagekey)
  51. uni.getStorage({
  52. key: storagekey,
  53. success: function(res) {
  54. if (storagekey !== 'token' && storagekey !== '__DC_STAT_UUID') {
  55. loggedUsers.value.push({
  56. userName: storagekey,
  57. password: res.data
  58. })
  59. }
  60. }
  61. });
  62. }
  63. }
  64. });
  65. console.log(loggedUsers)
  66. }
  67. const handleShowLoginDialog = (user) => {
  68. let _user = user ?? {}
  69. // 调用子组件中的方法
  70. loginDialog.value.open(_user)
  71. }
  72. const handleSelectUser = (user) => {
  73. console.log(user)
  74. handleShowLoginDialog(user)
  75. }
  76. const handleLongPressUser = (user) => {
  77. console.log(user)
  78. uni.showModal({
  79. title: '提示', // 标题
  80. content: '是否删除此账号', // 提示内容
  81. cancelText: "取消", // 取消按钮的文字
  82. confirmText: "确认", // 确认按钮的文字
  83. //点击确定之后执行的代码
  84. success(res) {
  85. if (res.confirm) {
  86. uni.removeStorage({
  87. key: user.userName, // 键名
  88. success: () => {
  89. uni.showToast({
  90. title: '删除成功',
  91. icon: 'success',
  92. duration: 3000,
  93. });
  94. loggedUsers.value = [];
  95. init();
  96. }, // 成功回调函数
  97. fail: () => {
  98. }, // 失败回调函数
  99. })
  100. }
  101. }
  102. });
  103. }
  104. const handleAddUser = () => {
  105. uni.setStorage({
  106. key: 'logged-users',
  107. data: {
  108. users: ''
  109. },
  110. success: () => {
  111. console.log('success');
  112. }
  113. })
  114. }
  115. </script>
  116. <style lang="scss">
  117. .title {
  118. height: 128rpx;
  119. justify-content: center;
  120. align-items: center;
  121. }
  122. .item-user {
  123. margin: 32rpx;
  124. margin-top: 0;
  125. padding: 32rpx;
  126. background-color: #FFFFFF;
  127. align-items: center;
  128. .user-avatar {
  129. width: 128rpx;
  130. height: 128rpx;
  131. border-radius: 64rpx;
  132. background-color: #F1F2F3;
  133. justify-content: center;
  134. align-items: center;
  135. .label {
  136. font-size: 64rpx;
  137. }
  138. }
  139. .user-info {
  140. flex: 1;
  141. height: 96rpx;
  142. padding-left: 32rpx;
  143. .nickname {
  144. .label {
  145. font-weight: bold;
  146. font-size: 40rpx;
  147. }
  148. }
  149. .username {
  150. margin-top: 8rpx;
  151. .label {
  152. font-size: 32rpx;
  153. }
  154. }
  155. }
  156. }
  157. .add-user-btn {
  158. margin: 32rpx;
  159. padding: 32rpx;
  160. background-color: #FFFFFF;
  161. align-items: center;
  162. .icon {
  163. width: 128rpx;
  164. height: 128rpx;
  165. align-items: center;
  166. justify-content: center;
  167. background-color: #F1F2F3;
  168. border-radius: 64rpx;
  169. .label {
  170. color: #FFFFFF;
  171. font-size: 128rpx;
  172. }
  173. }
  174. .btn-label {
  175. flex: 1;
  176. padding-left: 32rpx;
  177. .label {
  178. font-size: 40rpx;
  179. }
  180. }
  181. }
  182. </style>