login.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <view class="login">
  3. <view class="login-box">
  4. <view class="login-box-title">
  5. <view>智慧停车</view>
  6. <view>运营数据系统</view>
  7. </view>
  8. <view class="login-box-form">
  9. <view class="login-box-form-box">
  10. <u--form :model="form" :rules="rules" ref="uForm">
  11. <u-form-item label="" prop="phoneNumber">
  12. <u--input
  13. v-model="form.phoneNumber"
  14. :prefixIcon="require('@/static/icons/job-number-icon.svg')"
  15. placeholder="请输入手机号"
  16. :maxlength="11"
  17. type="number"
  18. border="none"
  19. class="custom-input"
  20. :prefixIconStyle="{ marginRight: '10px' }"
  21. fontSize="14px"
  22. color="#ffffff"
  23. placeholderStyle="color: #fff"
  24. />
  25. </u-form-item>
  26. <u-form-item label="" prop="password">
  27. <u--input
  28. v-model="form.password"
  29. :prefixIcon="require('@/static/icons/password-icon.svg')"
  30. placeholder="请输入密码"
  31. :password="true"
  32. border="none"
  33. class="custom-input"
  34. :prefixIconStyle="{ marginRight: '10px' }"
  35. fontSize="14px"
  36. color="#ffffff"
  37. placeholderStyle="color: #fff"
  38. />
  39. </u-form-item>
  40. <u-form-item>
  41. <u-checkbox-group v-model="checked">
  42. <u-checkbox
  43. name="remeber"
  44. activeColor="#f05"
  45. iconSize="18px"
  46. label="记住密码"
  47. labelColor="#fff"
  48. />
  49. </u-checkbox-group>
  50. </u-form-item>
  51. </u--form>
  52. </view>
  53. <view class="login-box-form-btn">
  54. <u-button
  55. class="login-btn"
  56. type="primary"
  57. text="登录"
  58. :loading="loading"
  59. @click="submitLogin"
  60. ></u-button>
  61. </view>
  62. </view>
  63. </view>
  64. <u-toast ref="uToast"></u-toast>
  65. </view>
  66. </template>
  67. <script>
  68. export default {
  69. data() {
  70. return {
  71. form: {
  72. phoneNumber: '',
  73. password: ''
  74. },
  75. checked: [],
  76. loading: false,
  77. rules: {
  78. phoneNumber: {
  79. required: true,
  80. message: '请输入手机号',
  81. trigger: ['change', 'blur']
  82. },
  83. password: {
  84. required: true,
  85. message: '请输入密码',
  86. trigger: ['change', 'blur']
  87. }
  88. }
  89. };
  90. },
  91. onShow() {
  92. uni.getStorage({
  93. key: 'userLoginInfo',
  94. success: (res) => {
  95. let userLoginInfo = JSON.parse(res.data)
  96. if (userLoginInfo) {
  97. const { telephone, loginPwd, checked } = userLoginInfo
  98. this.form.phoneNumber = telephone
  99. this.form.password = loginPwd
  100. if (checked) {
  101. this.checked = ['remeber']
  102. }
  103. }
  104. }
  105. })
  106. },
  107. methods: {
  108. /**
  109. * 登录提交
  110. */
  111. submitLogin() {
  112. this.$refs.uForm
  113. .validate()
  114. .then((res) => {
  115. this.loading = true;
  116. const form = {
  117. ...this.form
  118. };
  119. uni.$u.api
  120. .loginApi({
  121. telephone: form.phoneNumber,
  122. loginPwd: form.password
  123. })
  124. .then((res) => {
  125. if (res.code === 200) {
  126. uni.$u.vuex('vuex_token', res.data.token);
  127. this.$u.vuex('vuex_user', res.data);
  128. this.$u.vuex('vuex_isLogin', true);
  129. if (this.checked.includes('remeber')) {
  130. const loginInfo = {
  131. telephone: form.phoneNumber,
  132. loginPwd: form.password,
  133. checked: true
  134. }
  135. uni.setStorage({
  136. key: 'userLoginInfo',
  137. data: JSON.stringify(loginInfo)
  138. })
  139. }
  140. this.$refs.uToast.show({
  141. loading: true,
  142. message: '登录成功!',
  143. type: 'success',
  144. complete() {
  145. uni.$u.route({
  146. url: 'pages/index/index',
  147. type: 'redirect'
  148. });
  149. }
  150. });
  151. } else {
  152. this.$refs.uToast.show({
  153. loading: true,
  154. message: res.msg || '登录失败',
  155. type: 'error'
  156. });
  157. }
  158. })
  159. .catch((err) => {
  160. this.loading = false;
  161. });
  162. })
  163. .catch((errors) => {
  164. uni.$u.toast(errors[0].message || '必填项不能为空!');
  165. });
  166. }
  167. }
  168. };
  169. </script>
  170. <style lang="scss">
  171. page {
  172. height: 100vh;
  173. background: linear-gradient(183deg, #2d82fb 0%, #1558f8 100%);
  174. }
  175. </style>
  176. <style lang="scss" scoped>
  177. @import './login.scss';
  178. </style>