index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <view class="login-box">
  3. <image class="logoImg" src="@/static/logo.png"></image>
  4. <image class="loginImg" src="@/static/login.png"></image>
  5. <view class="loginContent">
  6. <p class="title">企业数字应用平台</p>
  7. <uni-forms :modelValue="formData" :rules="rules" ref="form">
  8. <uni-forms-item name="enterpriseCode">
  9. <uni-easyinput type="text" prefixIcon="vip-filled" v-model="formData.enterpriseCode"
  10. placeholder="请输入企业简称" />
  11. </uni-forms-item>
  12. <uni-forms-item name="username">
  13. <uni-easyinput type="text" prefixIcon="person" v-model="formData.username" placeholder="请输入用户名" />
  14. </uni-forms-item>
  15. <uni-forms-item name="password">
  16. <uni-easyinput type="password" prefixIcon="locked" v-model="formData.password"
  17. placeholder="请输入密码" />
  18. </uni-forms-item>
  19. <view class="form-code">
  20. <view class="input-box">
  21. <uni-forms-item name="code">
  22. <uni-easyinput type="text" prefixIcon="locked-filled" v-model="formData.code"
  23. placeholder="请输入验证码" />
  24. </uni-forms-item>
  25. </view>
  26. <view class="img-box">
  27. <image :src="codeUrl" class="img_code" @click="getImgCode"></image>
  28. </view>
  29. </view>
  30. </uni-forms>
  31. <button type="primary" class="loginBtn" @click="login">登 录</button>
  32. </view>
  33. <u-toast ref="uToast"></u-toast>
  34. </view>
  35. </template>
  36. <script>
  37. import $baseUrl from '@/https/index.js';
  38. export default {
  39. data() {
  40. return {
  41. formData: {
  42. enterpriseCode: '',
  43. username: '',
  44. password: '',
  45. code: '',
  46. uuid: ''
  47. },
  48. rules: {
  49. enterpriseCode: {
  50. rules: [{
  51. required: true,
  52. errorMessage: '请输入企业简称',
  53. }, ],
  54. },
  55. username: {
  56. rules: [{
  57. required: true,
  58. errorMessage: '请输入用户名',
  59. }, ],
  60. },
  61. password: {
  62. rules: [{
  63. required: true,
  64. errorMessage: '请输入密码',
  65. }, ],
  66. },
  67. code: {
  68. rules: [{
  69. required: true,
  70. errorMessage: '请输入验证码',
  71. }, ],
  72. }
  73. },
  74. codeUrl: '',
  75. }
  76. },
  77. onShow() {
  78. this.getImgCode()
  79. uni.hideLoading()
  80. const enterpriseCode = uni.getStorageSync('enterpriseCode') // 获取缓存中的企业简称
  81. if (enterpriseCode) {
  82. this.formData.enterpriseCode = enterpriseCode
  83. }
  84. const username = uni.getStorageSync('username') // 获取缓存中的用户名
  85. if (username) {
  86. this.formData.username = username
  87. }
  88. const password = uni.getStorageSync('password') // 获取缓存中的密码
  89. if (password) {
  90. this.formData.password = password
  91. }
  92. },
  93. methods: {
  94. getImgCode() {
  95. const url = '/captchaImage'
  96. uni.request({
  97. method: 'get',
  98. url: $baseUrl.url + url,
  99. })
  100. .then((response) => {
  101. const res = response[1].data
  102. if (res.code === 200) {
  103. this.codeUrl = 'data:image/gif;base64,' + res.img
  104. this.formData.uuid = res.uuid
  105. }
  106. }).catch((reject) => {
  107. console.log('请求失败')
  108. })
  109. },
  110. // 登录
  111. login(form) {
  112. this.$refs.form.validate().then(res => {
  113. const url = '/login'
  114. uni.request({
  115. method: 'post',
  116. url: $baseUrl.url + url,
  117. data: this.formData,
  118. })
  119. .then((response) => {
  120. const res = response[1].data
  121. if (res.code === 200) {
  122. uni.setStorageSync('token', res.token);
  123. uni.setStorageSync('enterpriseCode', this.formData.enterpriseCode);
  124. uni.setStorageSync('username', this.formData.username);
  125. uni.setStorageSync('password', this.formData.password);
  126. uni.showLoading({
  127. title: '登录中...'
  128. });
  129. setTimeout(function() {
  130. uni.hideLoading();
  131. uni.reLaunch({
  132. url: "/pages/index/index"
  133. })
  134. }, 2000);
  135. } else {
  136. this.$refs.uToast.show({
  137. type: 'error',
  138. message: res.msg,
  139. })
  140. this.getImgCode()
  141. this.formData.code = ''
  142. }
  143. }).catch((reject) => {
  144. console.log('请求失败')
  145. })
  146. }).catch(err => {
  147. console.log('表单错误信息:', err);
  148. })
  149. },
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. .login-box {
  155. background-color: #fff;
  156. height: 100%;
  157. }
  158. .logoImg{
  159. z-index: 1;
  160. width: 320rpx;
  161. height: 80rpx;
  162. position: absolute;
  163. top: 10rpx;
  164. left: 10rpx;
  165. }
  166. .loginImg {
  167. width: 100%;
  168. }
  169. .loginContent {
  170. width: 80%;
  171. margin: auto;
  172. text-align: center;
  173. .title {
  174. font-size: 44rpx;
  175. font-weight: bold;
  176. color: #3f7fff;
  177. margin: 20rpx 0 60rpx 0;
  178. }
  179. /deep/.uni-forms-item .is-required {
  180. display: none;
  181. }
  182. /deep/.uni-forms-item__label {
  183. padding: 0;
  184. }
  185. /deep/.uni-easyinput__content.is-input-border {
  186. height: 90rpx;
  187. border-radius: 50rpx;
  188. padding: 20rpx;
  189. }
  190. .loginBtn {
  191. font-size: 38rpx;
  192. height: 100rpx;
  193. line-height: 100rpx;
  194. border-radius: 50rpx;
  195. width: 100%;
  196. background-image: linear-gradient(to right, #3f7fff, #5cb7ec);
  197. border: none;
  198. margin-top: 40rpx;
  199. }
  200. }
  201. .form-code {
  202. display: flex;
  203. justify-content: space-between;
  204. .input-box {
  205. width: 60%;
  206. }
  207. .img-box {
  208. width: 40%;
  209. height: 80rpx;
  210. margin-left: 2%;
  211. .img_code {
  212. width: 100%;
  213. height: 100%;
  214. }
  215. }
  216. }
  217. </style>