login.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <view style="padding: 20rpx;">
  3. <view class="box" style="padding: 60rpx 30rpx;">
  4. <view style="font-size: 40rpx;">欢迎登录</view>
  5. <view style="margin: 60rpx 0;">
  6. <uni-forms ref="formRef" :modelValue="form" :rules="rules" validateTrigger="blur">
  7. <uni-forms-item name="username" required>
  8. <uni-easyinput prefixIcon="person" v-model="form.username" placeholder="请输入账号" />
  9. </uni-forms-item>
  10. <uni-forms-item name="password" required>
  11. <uni-easyinput type="password" prefixIcon="locked" v-model="form.password" placeholder="请输入密码" />
  12. </uni-forms-item>
  13. <uni-forms-item>
  14. <button @click="login" type="primary" style="width: 100%; height: 70rpx; line-height: 70rpx; background-color: #006eff; border-color: #006eff;">登 录</button>
  15. </uni-forms-item>
  16. </uni-forms>
  17. </view>
  18. <view style="text-align: right;">
  19. 还没有账号?请 <navigator url="/pages/register/register" style="display: inline; margin-left: 5rpx; color: #006eff;">注册</navigator>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. form: { role: 'USER' },
  29. rules: {
  30. // 对字段进行必填验证
  31. username: {
  32. rules:[
  33. // 校验不能为空
  34. {
  35. required: true,
  36. errorMessage: '请输入账号',
  37. }
  38. ],
  39. },
  40. password: {
  41. rules:[
  42. // 校验不能为空
  43. {
  44. required: true,
  45. errorMessage: '请输入密码',
  46. }
  47. ],
  48. },
  49. }
  50. }
  51. },
  52. methods: {
  53. login() {
  54. this.$refs.formRef.validate().then(res => {
  55. this.$request.post('/login', this.form).then(res => {
  56. if (res.code === '200') { // 登录成功
  57. uni.setStorageSync('xm-user', res.data)
  58. uni.showToast({
  59. icon: 'success',
  60. title: '登录成功'
  61. })
  62. setTimeout(() => {
  63. uni.switchTab({
  64. url: '/pages/index/index'
  65. })
  66. }, 500)
  67. } else {
  68. uni.showToast({
  69. icon: 'none',
  70. title: res.msg
  71. })
  72. }
  73. })
  74. }).catch(err => {
  75. console.log('err', err);
  76. })
  77. }
  78. }
  79. }
  80. </script>
  81. <style>
  82. </style>