login.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <view>
  3. <view class="bg" :style="{backgroundImage: 'url('+imageURL+')'}">
  4. <view class="avatar_class">
  5. <u-avatar size="80" :src="src"></u-avatar>
  6. </view>
  7. <view class="alert_class">欢迎登录</view>
  8. <view class="loginBox">
  9. <view style="margin-top: 200rpx;">
  10. <view style="margin-bottom:60rpx;">
  11. <u--input class="input_class" v-model="form.username" shape="circle" height='40' prefixIcon='account-fill' label=" " placeholder="请输入用户名" :showAction="false"></u--input>
  12. <!-- #ifdef MP-WEIXIN -->
  13. <view style="height:20rpx"></view>
  14. <!-- #endif -->
  15. <u--input class="input_class" v-model="form.password" shape="circle" height='40' prefixIcon='lock' type="password" label=" " placeholder="请输入密码" :showAction="false"></u--input>
  16. </view>
  17. <u-button size="medium" shape='true' :loading="isLoading" type="primary" @tap="login">登录</u-button>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import * as API_item from '@/api/login.js';
  25. export default {
  26. data() {
  27. return {
  28. form: {
  29. username: '',
  30. password: ''
  31. },
  32. imageURL:'/static/login/登录bg.jpg',
  33. src: '/static/login/1.jpg',
  34. isLoading: false,
  35. }
  36. },
  37. methods: {
  38. async login() {
  39. const _this = this;
  40. _this.isLoading = true;
  41. if(!this.form.username){
  42. _this.isLoading = false;
  43. uni.showToast({ // 提示错误信息
  44. title: '请输入用户名',
  45. icon:'error',
  46. duration: 3000
  47. })
  48. }if(!this.form.password){
  49. _this.isLoading = false;
  50. uni.showToast({ // 提示错误信息
  51. title: '请输入密码',
  52. icon:'error',
  53. duration: 3000
  54. })
  55. }else{
  56. API_item.GetUserToken(this.form).then(res=>{
  57. if(res.code == 101){
  58. _this.isLoading = false;
  59. uni.setStorageSync('token', res.data);
  60. uni.showToast({ // 提示错误信息
  61. title: '登录成功',
  62. icon:'success',
  63. duration: 3000
  64. })
  65. API_item.GetUserInfo().then(e=>{
  66. if(e.code == 101){
  67. uni.setStorageSync('userInfo', e.data);
  68. let url = uni.getStorageSync("URL")
  69. // if(url){
  70. // uni.navigateTo({ // 跳转到首页
  71. // url: url
  72. // })
  73. // }else{
  74. uni.switchTab({ // 跳转到首页
  75. url: '/pages/home/home'
  76. })
  77. // }
  78. }else{
  79. uni.showToast({ // 提示错误信息
  80. title: e.message,
  81. icon:'error',
  82. duration: 3000
  83. })
  84. }
  85. })
  86. }else{
  87. _this.isLoading = false;
  88. uni.showToast({ // 提示错误信息
  89. title: res.message,
  90. icon:'error',
  91. duration: 3000
  92. })
  93. }
  94. })
  95. }
  96. }
  97. },
  98. getUserInfo(){
  99. API_item.GetUserInfo().then(res=>{
  100. if(uni.getStorage('token')){
  101. if(res.code == 101){
  102. // uni.switchTab({ // 跳转到首页
  103. // url: '/pages/home/home'
  104. // })
  105. }else{
  106. }
  107. }
  108. })
  109. }
  110. }
  111. </script>
  112. /*scoped 仅对当前页面生效*/
  113. <style lang="scss">
  114. .bg {
  115. width: 100vw;
  116. height: 100vh;
  117. background-size: 100% 100%;
  118. display: flex;
  119. flex-direction: column;
  120. justify-content: center;
  121. align-items: center;
  122. .avatar_class{
  123. position: absolute;
  124. top:300rpx;
  125. z-index: 9;
  126. }
  127. .alert_class{
  128. position: absolute;
  129. top:460rpx;
  130. z-index: 9;
  131. color: rgba(40, 39, 39, 0.7);
  132. }
  133. .loginBox {
  134. width: 500rpx;
  135. height: 600rpx;
  136. position: relative;
  137. background-color: rgba(255, 255, 255, 0.7);
  138. margin: auto;
  139. border-radius: 30rpx;
  140. padding: 30rpx;
  141. margin-top: 350rpx;
  142. .input_class{
  143. margin-top: 30rpx !important;
  144. }
  145. }
  146. }
  147. </style>