login.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view class="normal-login-container">
  3. <view class="login-form-content">
  4. <view class="input-item flex align-center">
  5. <view class="iconfont icon-user icon"></view>
  6. <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
  7. </view>
  8. <view class="input-item flex align-center">
  9. <view class="iconfont icon-password icon"></view>
  10. <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
  11. </view>
  12. <view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
  13. <view class="iconfont icon-code icon"></view>
  14. <input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
  15. <view class="login-code">
  16. <image :src="codeUrl" @click="getCode" class="login-code-img"></image>
  17. </view>
  18. </view>
  19. <view class="action-btn">
  20. <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import { getCodeImg } from '@/api/login'
  27. export default {
  28. data() {
  29. return {
  30. codeUrl: "",
  31. captchaEnabled: true,
  32. // 用户注册开关
  33. register: false,
  34. globalConfig: getApp().globalData.config,
  35. loginForm: {
  36. username: "admin",
  37. password: "admin123",
  38. code: "",
  39. uuid: '',
  40. type:'app'
  41. }
  42. }
  43. },
  44. created() {
  45. this.getCode()
  46. },
  47. methods: {
  48. // 获取图形验证码
  49. getCode() {
  50. getCodeImg().then(res => {
  51. console.log(res)
  52. this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
  53. if (this.captchaEnabled) {
  54. this.codeUrl = 'data:image/gif;base64,' + res.data.img
  55. this.loginForm.uuid = res.data.uuid
  56. }
  57. })
  58. },
  59. // 登录方法
  60. async handleLogin() {
  61. if (this.loginForm.username === "") {
  62. this.$modal.msgError("请输入您的账号")
  63. } else if (this.loginForm.password === "") {
  64. this.$modal.msgError("请输入您的密码")
  65. } else if (this.loginForm.code === "" && this.captchaEnabled) {
  66. this.$modal.msgError("请输入验证码")
  67. } else {
  68. this.$modal.loading("登录中,请耐心等待...")
  69. this.pwdLogin()
  70. }
  71. },
  72. // 密码登录
  73. async pwdLogin() {
  74. this.loginForm.type='app'
  75. this.$store.dispatch('Login', this.loginForm).then(() => {
  76. this.$modal.closeLoading()
  77. this.loginSuccess()
  78. }).catch(() => {
  79. if (this.captchaEnabled) {
  80. this.getCode()
  81. }
  82. })
  83. },
  84. // 登录成功后,处理函数
  85. loginSuccess(result) {
  86. // 设置用户信息
  87. this.$store.dispatch('GetInfo').then(res => {
  88. this.$tab.reLaunch('/pages/index')
  89. })
  90. }
  91. }
  92. }
  93. </script>
  94. <style lang="scss">
  95. page {
  96. background-color: #ffffff;
  97. }
  98. .normal-login-container {
  99. width: 100%;
  100. .logo-content {
  101. width: 100%;
  102. font-size: 21px;
  103. text-align: center;
  104. padding-top: 15%;
  105. image {
  106. border-radius: 4px;
  107. }
  108. .title {
  109. margin-left: 10px;
  110. }
  111. }
  112. .login-form-content {
  113. text-align: center;
  114. margin: 20px auto;
  115. margin-top: 15%;
  116. width: 80%;
  117. .input-item {
  118. margin: 20px auto;
  119. background-color: #f5f6f7;
  120. height: 45px;
  121. border-radius: 20px;
  122. .icon {
  123. font-size: 38rpx;
  124. margin-left: 10px;
  125. color: #999;
  126. }
  127. .input {
  128. width: 100%;
  129. font-size: 14px;
  130. line-height: 20px;
  131. text-align: left;
  132. padding-left: 15px;
  133. }
  134. }
  135. .login-btn {
  136. margin-top: 40px;
  137. height: 45px;
  138. }
  139. .reg {
  140. margin-top: 15px;
  141. }
  142. .xieyi {
  143. color: #333;
  144. margin-top: 20px;
  145. }
  146. .login-code {
  147. height: 38px;
  148. float: right;
  149. .login-code-img {
  150. height: 38px;
  151. position: absolute;
  152. margin-left: 10px;
  153. width: 200rpx;
  154. }
  155. }
  156. }
  157. }
  158. </style>