index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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" @click="handleShowGear" />
  5. <uni-icons v-if="showGear > 4" class="icon-gear" type="gear-filled" size="60"
  6. @click="handleGear"></uni-icons>
  7. </view>
  8. <view class="title uni-row" v-if="loggedUsers.length > 0">
  9. <text>点击头像切换用户</text>
  10. </view>
  11. <view class="baseUrl uni-row" v-if="showInput">
  12. <input v-model="baseUrl" placeholder="服务器地址" />
  13. <button @click="handleSetting">设置</button>
  14. </view>
  15. <view class="add-user-btn uni-row" @click="handleShowLoginDialog(null)">
  16. <view class="icon uni-row">
  17. <text class="label">+</text>
  18. </view>
  19. <view class="btn-label">
  20. <text class="label">添加新账号</text>
  21. </view>
  22. </view>
  23. <view v-for="(item, index) in loggedUsers" class="item-user uni-row" @click="handleSelectUser(item)"
  24. @longpress="handleLongPressUser(item)">
  25. <view class="user-avatar uni-row"><text class="label">{{ item.nickName.charAt(0) }}</text></view>
  26. <view class="user-info">
  27. <view class="nickname">
  28. <text class="label">{{item.nickName}}</text>
  29. </view>
  30. <view class="username">
  31. <text class="label">{{item.userName}}</text>
  32. </view>
  33. </view>
  34. </view>
  35. <dialog-login ref="loginDialog" />
  36. </view>
  37. </template>
  38. <script setup>
  39. import {
  40. onMounted,
  41. ref
  42. } from 'vue'
  43. import {
  44. onLoad,
  45. onReady
  46. } from '@dcloudio/uni-app'
  47. import {
  48. getUserInfo,
  49. getNickNameByUserName
  50. } from '@/api/login/index.js'
  51. import baseURL from '@/api/base/path.js'
  52. import {
  53. getAppInfoListByType
  54. } from '@/api/business/app.js'
  55. import appURL from '@/api/base/appPath.js'
  56. import upgrade from '@/uni_modules/lgen-upgrade/js_sdk/upgrade.js'
  57. // 登录过的用户
  58. const loggedUsers = ref([])
  59. const loginDialog = ref(null)
  60. const userInfo = ref({})
  61. const users = ref([])
  62. const nickName = ref('')
  63. const baseUrl = ref(null)
  64. const showInput = ref(false)
  65. const showGear = ref(0)
  66. const appInfo = ref({})
  67. const newAppInfo = ref({})
  68. const updateFlag = ref(true) // 是否更新标志,如果为false,不能登录
  69. onLoad(() => {})
  70. onReady(() => {
  71. initBaseUrl();
  72. // getUser();
  73. setTimeout(function() {
  74. checkUpdate().then(res => {
  75. if (!res) {
  76. updateFlag.value = res;
  77. uni.showModal({
  78. title: '更新',
  79. content: '发现新版本',
  80. showCancel: false,
  81. success: function(res) {
  82. if (res.confirm) {
  83. update();
  84. }
  85. }
  86. })
  87. }
  88. })
  89. },500)
  90. })
  91. function checkUpdate() {
  92. uni.getSystemInfo({
  93. success: function(res) {
  94. appInfo.value = res;
  95. }
  96. });
  97. console.log(appInfo.value);
  98. // false 版本号不一致,更新,true不需要更新
  99. let flag = getAppInfoListByType({
  100. type: appInfo.value.osName
  101. }).then(res => {
  102. console.log(res.rows[0]);
  103. newAppInfo.value = res.rows[0];
  104. if (res.rows[0]) {
  105. return appInfo.value.appWgtVersion === newAppInfo.value.versionName;
  106. } else {
  107. return true;
  108. }
  109. })
  110. return flag;
  111. }
  112. function update() {
  113. upgrade.downloadInstallApp(appURL + newAppInfo.value.url);
  114. }
  115. function init() {
  116. uni.getStorageInfo({
  117. success: function(res) {
  118. for (let i = 0; i < res.keys.length; i++) {
  119. let storagekey = res.keys[i];
  120. console.log(storagekey)
  121. uni.getStorage({
  122. key: storagekey,
  123. success: function(res) {
  124. if (storagekey !== 'token' && storagekey !== '__DC_STAT_UUID' &&
  125. storagekey !== 'baseUrl') {
  126. getNickNameByUserName(storagekey).then((response) => {
  127. if (response.code == 200) {
  128. loggedUsers.value.push({
  129. userName: storagekey,
  130. password: res.data,
  131. nickName: response.msg
  132. })
  133. }
  134. })
  135. }
  136. if (storagekey === 'baseUrl') {
  137. baseUrl.value = res.data;
  138. }
  139. }
  140. });
  141. }
  142. }
  143. });
  144. }
  145. function initBaseUrl() {
  146. uni.getStorage({
  147. key: 'baseUrl',
  148. fail: function() {
  149. uni.setStorageSync('baseUrl', baseURL);
  150. },
  151. complete: function() {
  152. init();
  153. }
  154. });
  155. }
  156. function handleShowGear() {
  157. showGear.value += 1;
  158. }
  159. function handleGear() {
  160. showGear.value += 1;
  161. if (showGear.value >= 7) {
  162. showGear.value = 0;
  163. }
  164. showInput.value = !showInput.value
  165. }
  166. function handleSetting() {
  167. // let reg = /^(?:(http|https|ftp):\/\/)?((?:[\w-]+\.)+[a-z0-9]+)((?:\/[^/?#]*)+)?(\?[^#]+)?(#.+)?$/i;
  168. // if (baseUrl.value !== null && baseUrl.value.trim() && reg.test(baseUrl.value)) {
  169. if (baseUrl.value) {
  170. uni.setStorage({
  171. key: 'baseUrl',
  172. data: baseUrl.value,
  173. success: function() {
  174. uni.showToast({
  175. icon: "success",
  176. title: "设置成功"
  177. })
  178. }
  179. });
  180. showInput.value = !showInput.value;
  181. } else {
  182. uni.showToast({
  183. icon: "error",
  184. title: "输入错误"
  185. })
  186. }
  187. }
  188. const handleShowLoginDialog = (user) => {
  189. // 判断更新否,没更新不能登录
  190. if(!updateFlag.value){
  191. uni.showToast({
  192. icon: 'none',
  193. title: '请更新后再登录'
  194. })
  195. setTimeout(function(){
  196. checkUpdate().then(res => {
  197. if (!res) {
  198. updateFlag.value = res;
  199. uni.showModal({
  200. title: '更新',
  201. content: '发现新版本',
  202. showCancel: false,
  203. success: function(res) {
  204. if (res.confirm) {
  205. update();
  206. }
  207. }
  208. })
  209. }
  210. })
  211. },1500)
  212. }else{
  213. let _user = user ?? {}
  214. // 调用子组件中的方法
  215. loginDialog.value.open(_user)
  216. }
  217. }
  218. const handleSelectUser = (user) => {
  219. handleShowLoginDialog(user)
  220. }
  221. const handleLongPressUser = (user) => {
  222. console.log(user)
  223. uni.showModal({
  224. title: '提示', // 标题
  225. content: '是否删除此账号', // 提示内容
  226. cancelText: "取消", // 取消按钮的文字
  227. confirmText: "确认", // 确认按钮的文字
  228. //点击确定之后执行的代码
  229. success(res) {
  230. if (res.confirm) {
  231. uni.removeStorage({
  232. key: user.userName, // 键名
  233. success: () => {
  234. uni.showToast({
  235. title: '删除成功',
  236. icon: 'success',
  237. duration: 3000,
  238. });
  239. loggedUsers.value = [];
  240. init();
  241. }, // 成功回调函数
  242. fail: () => {
  243. }, // 失败回调函数
  244. })
  245. }
  246. }
  247. });
  248. }
  249. const handleAddUser = () => {
  250. uni.setStorage({
  251. key: 'logged-users',
  252. data: {
  253. users: ''
  254. },
  255. success: () => {}
  256. })
  257. }
  258. </script>
  259. <style lang="scss">
  260. .page-container {
  261. overflow: auto;
  262. }
  263. .logo {
  264. width: 120rpx;
  265. height: 120rpx;
  266. margin: 0 auto;
  267. }
  268. .title {
  269. height: 80rpx;
  270. justify-content: center;
  271. align-items: center;
  272. color: #999999;
  273. }
  274. .icon-gear {
  275. position: fixed;
  276. right: 40rpx;
  277. font-size: 60rpx;
  278. }
  279. .baseUrl {
  280. border: 2rpx solid gray;
  281. width: 90%;
  282. margin: 0 auto;
  283. border-radius: 12rpx;
  284. align-items: center;
  285. input {
  286. width: 70%;
  287. padding: 16rpx 24rpx;
  288. }
  289. button {
  290. width: 30%;
  291. }
  292. }
  293. .item-user {
  294. margin: 32rpx;
  295. margin-top: 0;
  296. padding: 32rpx;
  297. background-color: #FFFFFF;
  298. align-items: center;
  299. .user-avatar {
  300. width: 128rpx;
  301. height: 128rpx;
  302. border-radius: 64rpx;
  303. background-color: #F1F2F3;
  304. justify-content: center;
  305. align-items: center;
  306. .label {
  307. font-size: 64rpx;
  308. }
  309. }
  310. .user-info {
  311. flex: 1;
  312. height: 96rpx;
  313. padding-left: 32rpx;
  314. .nickname {
  315. .label {
  316. font-weight: bold;
  317. font-size: 40rpx;
  318. }
  319. }
  320. .username {
  321. margin-top: 8rpx;
  322. .label {
  323. font-size: 32rpx;
  324. }
  325. }
  326. }
  327. }
  328. .add-user-btn {
  329. margin: 32rpx;
  330. padding: 32rpx;
  331. background-color: #FFFFFF;
  332. align-items: center;
  333. .icon {
  334. width: 128rpx;
  335. height: 128rpx;
  336. align-items: center;
  337. justify-content: center;
  338. background-color: #F1F2F3;
  339. border-radius: 64rpx;
  340. .label {
  341. color: #FFFFFF;
  342. font-size: 128rpx;
  343. }
  344. }
  345. .btn-label {
  346. flex: 1;
  347. padding-left: 32rpx;
  348. .label {
  349. font-size: 40rpx;
  350. }
  351. }
  352. }
  353. </style>