doum
2026-06-18 93de43267e1663031fe5dc2f5ae40d128a182a76
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<template>
  <view class="cu-login">
    <view class="cu-auth-topbar">
      <view class="cu-auth-topbar__btn" @click="goRoleSelect">
        <u-icon name="reload" color="#2080f7" size="22" />
      </view>
    </view>
 
    <view class="cu-login__brand">
      <view class="cu-login__title">欢迎登录</view>
      <view class="cu-login__sub">阜宁文体中心 · 运维服务平台</view>
    </view>
 
    <view v-if="devMockTip" class="cu-login__tip">{{ devMockTip }}</view>
 
    <view class="cu-input-wrap">
      <input v-model="form.phone" maxlength="18" placeholder="请输入手机号" />
    </view>
    <view class="cu-input-wrap">
      <input v-model="form.code" placeholder="请输入验证码" />
      <view v-if="downTime == 0" class="cu-sms-btn" @click="sendSms">获取验证码</view>
      <view v-else class="cu-sms-btn cu-sms-btn--disabled">{{ downTime }}s</view>
    </view>
 
    <view class="cu-btn cu-btn--primary" @click="onLogin">登录</view>
  </view>
</template>
 
<script>
import {
  loginPost,
  getUserInfo,
  sendSMsPost,
  ywWxAuthorize,
  getRecordByUserPoint
} from '@/api'
import { devWechatMock } from '@/utils/config.js'
import { runWechatOAuthFlow } from '@/utils/wechatAuth.js'
import { requestLoginSmsCode } from '@/utils/loginSms.js'
import { mapMutations } from 'vuex'
 
export default {
  name: 'login',
  data () {
    return {
      form: { phone: '', code: '' },
      ywinfo: {},
      downTime: 0,
      code: '',
      devMockTip: devWechatMock.enabled ? `开发模式:模拟 openid ${devWechatMock.openId}` : ''
    }
  },
  onLoad (option) {
    uni.setStorageSync('userType', 0)
    const ywinfo = uni.getStorageSync('ywinfo') || {}
    if (ywinfo.ywid && (ywinfo.type || ywinfo.type == 0)) {
      this.ywinfo = ywinfo
      uni.setStorageSync('ywinfo', {})
    }
    if (option.ywid || option.ywid == 0) {
      uni.setStorageSync('ywinfo', {
        type: option.type,
        ywid: option.ywid
      })
    }
  },
  onShow () {
    const that = this
    runWechatOAuthFlow({
      authorizeApi: ywWxAuthorize,
      fallbackCode: this.code,
      onSuccess: (res) => {
        if (res.data.openid) {
          that.$store.commit('setOpenId', res.data.openid)
        }
        if (res.data.token && res.data.token != '') {
          that.$store.commit('setToken', res.data.token)
          getUserInfo().then(ress => {
            that.$store.commit('setUserInfo', ress.data)
          })
          const ywinfo = this.ywinfo
          if (ywinfo.ywid && (ywinfo.type || ywinfo.type == 0)) {
            getRecordByUserPoint({ pointCode: ywinfo.ywid }).then(res => {
              if (res.data && res.data.id) {
                uni.redirectTo({ url: '/pages/polling/point?id=' + res.data.id })
              } else {
                uni.redirectTo({ url: '/pages/polling/empty?message=' + res.message })
              }
            })
          } else {
            setTimeout(() => {
              uni.redirectTo({ url: '/pages/index' })
            }, 300)
          }
        }
      }
    })
  },
  methods: {
    ...mapMutations(['setToken', 'setUserInfo']),
    goRoleSelect () {
      uni.redirectTo({ url: '/pages/roleSelect?switch=1' })
    },
    onLogin () {
      const { form } = this
      if (!form.phone) return uni.showToast({ title: '手机号不能为空', icon: 'none' })
      if (!form.code) return uni.showToast({ title: '验证码不能为空', icon: 'none' })
 
      loginPost({
        ...form,
        openid: this.$store.state.openId
      }).then(res => {
        if (res.code === 200) {
          this.setToken(res.data)
          getUserInfo().then(ress => {
            this.setUserInfo(ress.data)
            const ywinfo = this.ywinfo
            if (ywinfo.ywid && (ywinfo.type || ywinfo.type == 0)) {
              getRecordByUserPoint({ pointCode: ywinfo.ywid }).then(res => {
                if (res.data && res.data.id) {
                  uni.redirectTo({ url: '/pages/polling/point?id=' + res.data.id })
                } else {
                  uni.redirectTo({ url: '/pages/polling/empty?message=' + res.message })
                }
              })
            } else {
              uni.redirectTo({ url: '/pages/index' })
            }
          })
        }
      })
    },
    sendSms () {
      requestLoginSmsCode(this, this.form.phone, sendSMsPost, { phone: this.form.phone, userType: 0 })
    }
  }
}
</script>