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
<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="11" type="number" 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 { customerLogin, customerGetUserInfo, customerWxAuthorize, customerSendLoginSms } 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 {
  data () {
    return {
      form: { phone: '', code: '' },
      downTime: 0,
      devMockTip: devWechatMock.enabled ? `开发模式:模拟 openid ${devWechatMock.openId}` : ''
    }
  },
  onShow () {
    uni.setStorageSync('userType', 1)
    runWechatOAuthFlow({
      authorizeApi: customerWxAuthorize,
      onSuccess: (res) => {
        if (res.data.openid) this.setOpenId(res.data.openid)
        if (res.data.token) {
          this.setToken(res.data.token)
          this.setUserType(1)
          customerGetUserInfo().then(r => this.setUserInfo(r.data))
          uni.redirectTo({ url: '/pages/customer/index' })
        }
      }
    })
  },
  methods: {
    ...mapMutations(['setToken', 'setUserInfo', 'setOpenId', 'setUserType']),
    goRoleSelect () {
      uni.redirectTo({ url: '/pages/roleSelect?switch=1' })
    },
    onLogin () {
      if (!this.form.phone || !this.form.code) {
        return uni.showToast({ title: '请填写手机号和验证码', icon: 'none' })
      }
      customerLogin({ ...this.form, openid: this.$store.state.openId, userType: 1 }).then(res => {
        if (res.code === 200) {
          this.setToken(res.data)
          this.setUserType(1)
          customerGetUserInfo().then(r => this.setUserInfo(r.data))
          uni.redirectTo({ url: '/pages/customer/index' })
        }
      })
    },
    sendSms () {
      requestLoginSmsCode(this, this.form.phone, customerSendLoginSms)
    }
  }
}
</script>