index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="main" style="background-image: url('../../static/login.png')">
  3. <view class="one">
  4. <text>随访小程序</text>
  5. <text>登录</text>
  6. </view>
  7. <view class="two">
  8. <uni-forms ref="valiForm" :rules="rules" :modelValue="form" label-position="top">
  9. <uni-forms-item label="账号" required name="account">
  10. <uni-easyinput v-model="form.account" placeholder="请输入账号" />
  11. </uni-forms-item>
  12. <uni-forms-item label="密码" required name="password">
  13. <uni-easyinput type="password" v-model="form.password" placeholder="请输入密码" />
  14. </uni-forms-item>
  15. </uni-forms>
  16. <button class="button" type="primary" @click="onSubmit('valiForm')">登录</button>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. form: {
  25. account: '',
  26. password: ''
  27. },
  28. // 校验规则
  29. rules: {
  30. account: {
  31. rules: [{
  32. required: true,
  33. errorMessage: '账号不能为空'
  34. }]
  35. },
  36. password: {
  37. rules: [{
  38. required: true,
  39. errorMessage: '密码不能为空'
  40. }]
  41. }
  42. },
  43. }
  44. },
  45. onShow: function(e) {
  46. const that = this;
  47. that.searchToken();
  48. },
  49. methods: {
  50. searchToken() {
  51. const that = this;
  52. try {
  53. const res = uni.getStorageSync('token');
  54. if (res) {
  55. uni.reLaunch({
  56. url: `/pages/home/index`
  57. })
  58. }
  59. } catch (e) {
  60. uni.showToast({
  61. title: err.errmsg,
  62. icon: 'error',
  63. duration: 2000
  64. });
  65. }
  66. },
  67. // 登录
  68. onSubmit(ref) {
  69. const that = this;
  70. that.$refs[ref].validate().then(async form => {
  71. const arr = await that.$api(`/admin/login`, 'POST', form);
  72. if (arr.errcode == '0') {
  73. uni.setStorage({
  74. key: 'token',
  75. data: arr.data,
  76. success: function(res) {
  77. uni.reLaunch({
  78. url: `/pages/home/index`
  79. })
  80. },
  81. fail: function(err) {
  82. console.log(err);
  83. }
  84. })
  85. } else {
  86. uni.showToast({
  87. title: arr.errmsg,
  88. icon: 'none'
  89. })
  90. }
  91. })
  92. },
  93. }
  94. }
  95. </script>
  96. <style lang="scss" scoped>
  97. .main {
  98. display: flex;
  99. flex-direction: column;
  100. width: 100vw;
  101. height: 100vh;
  102. background-size: cover;
  103. background-repeat: no-repeat;
  104. background-position: center;
  105. .one {
  106. padding: 30vw 10vw 20vw 10vw;
  107. text-align: center;
  108. font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  109. font-size: var(--font20Size);
  110. }
  111. .two {
  112. padding: 2vw;
  113. .button {
  114. background: #007aff;
  115. border-radius: 30px;
  116. }
  117. }
  118. }
  119. </style>