index.vue 5.9 KB

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