index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <view class="page-container uni-column">
  3. <view class="title uni-row" v-if="loggedUsers.length > 0">
  4. <text>点击头像切换用户</text>
  5. <uni-icons class="icon-gear" type="gear-filled" size="60" color="#1684fc" @click="handleGear"></uni-icons>
  6. </view>
  7. <view class="baseUrl uni-row" v-if="showInput">
  8. <input v-model="baseUrl" placeholder="服务器地址" />
  9. <button @click="handleSetting">设置</button>
  10. </view>
  11. <view v-for="(item, index) in loggedUsers" class="item-user uni-row" @click="handleSelectUser(item)"
  12. @longpress="handleLongPressUser(item)">
  13. <view class="user-avatar uni-row"><text class="label">{{ item.nickName.charAt(0) }}</text></view>
  14. <view class="user-info">
  15. <view class="nickname">
  16. <text class="label">{{item.nickName}}</text>
  17. </view>
  18. <view class="username">
  19. <text class="label">{{item.userName}}</text>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="add-user-btn uni-row" @click="handleShowLoginDialog(null)">
  24. <view class="icon uni-row">
  25. <text class="label">+</text>
  26. </view>
  27. <view class="btn-label">
  28. <text class="label">添加新账号</text>
  29. </view>
  30. </view>
  31. <dialog-login ref="loginDialog" />
  32. </view>
  33. </template>
  34. <script setup>
  35. import {
  36. onMounted,
  37. ref
  38. } from 'vue'
  39. import {
  40. onLoad,
  41. onReady
  42. } from '@dcloudio/uni-app'
  43. import {
  44. getUserInfo,
  45. getNickNameByUserName
  46. } from '@/api/login/index.js'
  47. // 登录过的用户
  48. const loggedUsers = ref([])
  49. const loginDialog = ref(null)
  50. const userInfo = ref({})
  51. const users = ref([])
  52. const nickName = ref('')
  53. const baseUrl = ref(null)
  54. const showInput = ref(false)
  55. onLoad(() => {
  56. init();
  57. // getUser();
  58. })
  59. function init() {
  60. uni.getStorageInfo({
  61. success: function(res) {
  62. for (let i = 0; i < res.keys.length; i++) {
  63. let storagekey = res.keys[i];
  64. uni.getStorage({
  65. key: storagekey,
  66. success: function(res) {
  67. if (storagekey !== 'token' && storagekey !== '__DC_STAT_UUID' &&
  68. storagekey !== 'baseUrl') {
  69. getNickNameByUserName(storagekey).then((response) => {
  70. if (response.code == 200) {
  71. loggedUsers.value.push({
  72. userName: storagekey,
  73. password: res.data,
  74. nickName: response.msg
  75. })
  76. }
  77. })
  78. }
  79. if(storagekey === 'baseUrl'){
  80. baseUrl.value = res.data;
  81. }
  82. }
  83. });
  84. }
  85. }
  86. });
  87. }
  88. function handleGear() {
  89. showInput.value = !showInput.value
  90. }
  91. function handleSetting() {
  92. // let reg = /^(?:(http|https|ftp):\/\/)?((?:[\w-]+\.)+[a-z0-9]+)((?:\/[^/?#]*)+)?(\?[^#]+)?(#.+)?$/i;
  93. // if (baseUrl.value !== null && baseUrl.value.trim() && reg.test(baseUrl.value)) {
  94. if (baseUrl.value) {
  95. uni.setStorage({
  96. key: 'baseUrl',
  97. data: baseUrl.value,
  98. success: function() {
  99. uni.showToast({
  100. icon: "success",
  101. title: "设置成功"
  102. })
  103. }
  104. });
  105. showInput.value = !showInput.value;
  106. } else {
  107. uni.showToast({
  108. icon: "error",
  109. title: "输入错误"
  110. })
  111. }
  112. }
  113. const handleShowLoginDialog = (user) => {
  114. let _user = user ?? {}
  115. // 调用子组件中的方法
  116. loginDialog.value.open(_user)
  117. }
  118. const handleSelectUser = (user) => {
  119. handleShowLoginDialog(user)
  120. }
  121. const handleLongPressUser = (user) => {
  122. console.log(user)
  123. uni.showModal({
  124. title: '提示', // 标题
  125. content: '是否删除此账号', // 提示内容
  126. cancelText: "取消", // 取消按钮的文字
  127. confirmText: "确认", // 确认按钮的文字
  128. //点击确定之后执行的代码
  129. success(res) {
  130. if (res.confirm) {
  131. uni.removeStorage({
  132. key: user.userName, // 键名
  133. success: () => {
  134. uni.showToast({
  135. title: '删除成功',
  136. icon: 'success',
  137. duration: 3000,
  138. });
  139. loggedUsers.value = [];
  140. init();
  141. }, // 成功回调函数
  142. fail: () => {
  143. }, // 失败回调函数
  144. })
  145. }
  146. }
  147. });
  148. }
  149. const handleAddUser = () => {
  150. uni.setStorage({
  151. key: 'logged-users',
  152. data: {
  153. users: ''
  154. },
  155. success: () => {}
  156. })
  157. }
  158. </script>
  159. <style lang="scss">
  160. .title {
  161. height: 128rpx;
  162. justify-content: center;
  163. align-items: center;
  164. }
  165. .icon-gear {
  166. position: fixed;
  167. right: 40rpx;
  168. font-size: 60rpx;
  169. }
  170. .baseUrl {
  171. border: 2rpx solid gray;
  172. width: 90%;
  173. margin: 0 auto;
  174. border-radius: 12rpx;
  175. input {
  176. width: 80%;
  177. padding: 16rpx 24rpx;
  178. }
  179. button {
  180. width: 20%;
  181. }
  182. }
  183. .item-user {
  184. margin: 32rpx;
  185. margin-top: 0;
  186. padding: 32rpx;
  187. background-color: #FFFFFF;
  188. align-items: center;
  189. .user-avatar {
  190. width: 128rpx;
  191. height: 128rpx;
  192. border-radius: 64rpx;
  193. background-color: #F1F2F3;
  194. justify-content: center;
  195. align-items: center;
  196. .label {
  197. font-size: 64rpx;
  198. }
  199. }
  200. .user-info {
  201. flex: 1;
  202. height: 96rpx;
  203. padding-left: 32rpx;
  204. .nickname {
  205. .label {
  206. font-weight: bold;
  207. font-size: 40rpx;
  208. }
  209. }
  210. .username {
  211. margin-top: 8rpx;
  212. .label {
  213. font-size: 32rpx;
  214. }
  215. }
  216. }
  217. }
  218. .add-user-btn {
  219. margin: 32rpx;
  220. padding: 32rpx;
  221. background-color: #FFFFFF;
  222. align-items: center;
  223. .icon {
  224. width: 128rpx;
  225. height: 128rpx;
  226. align-items: center;
  227. justify-content: center;
  228. background-color: #F1F2F3;
  229. border-radius: 64rpx;
  230. .label {
  231. color: #FFFFFF;
  232. font-size: 128rpx;
  233. }
  234. }
  235. .btn-label {
  236. flex: 1;
  237. padding-left: 32rpx;
  238. .label {
  239. font-size: 40rpx;
  240. }
  241. }
  242. }
  243. </style>