login.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <view class="login-page">
  3. <uni-forms ref="loginForm" :model="form" :rules="rules">
  4. <uni-forms-item name="username">
  5. <uni-easyinput v-model="form.username" type="number" placeholder="请输入手机号" prefixIcon="account" :clearable="true" />
  6. </uni-forms-item>
  7. <uni-forms-item name="password">
  8. <uni-easyinput v-model="form.password" type="password" placeholder="请输入密码" prefixIcon="lock" :clearable="true" />
  9. </uni-forms-item>
  10. </uni-forms>
  11. <button type="primary" @click="submitForm">登录并绑定微信</button>
  12. </view>
  13. </template>
  14. <script>
  15. import { setToken } from '@/common/auth.js'
  16. import { login } from '@/api/login.js'
  17. import config from '@/config.js'
  18. export default {
  19. data() {
  20. return {
  21. form: {
  22. username: '15512341234', //15512341234
  23. password: 'sckj@2022', //sckj@2022
  24. },
  25. rules: {
  26. username: {
  27. rules: [
  28. { required: true, errorMessage: '手机号不能为空' },
  29. { format: 'number', errorMessage: '手机号应为数字' },
  30. ]
  31. },
  32. password: {
  33. rules: [
  34. { required: true, errorMessage: '密码不能为空' },
  35. ]
  36. }
  37. },
  38. }
  39. },
  40. methods: {
  41. submitForm() {
  42. this.$refs.loginForm.validate().then(() => {
  43. // #ifdef MP-WEIXIN
  44. uni.login({ // 获取用户登录凭证换取openid
  45. provider: 'weixin',
  46. success: (loginRes) => {
  47. const params = {
  48. ...this.form,
  49. code: loginRes.code,
  50. }
  51. login(params).then((res) => {
  52. const { code, data } = res
  53. if (code !== 200) return
  54. setToken(data.token.access_token)
  55. uni.setStorageSync('userInfo', data.sysUser)
  56. uni.reLaunch({ url: '/pages/index/index' })
  57. })
  58. }
  59. })
  60. // #endif
  61. }).catch(() => {})
  62. },
  63. }
  64. }
  65. </script>
  66. <style lang="scss" scoped>
  67. .login-page {
  68. width: 80%;
  69. margin: 35% auto;
  70. }
  71. </style>