index.vue 5.6 KB

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