gerson
2024-08-11 b2b8e5ed16f139597b10452df0c467b6e7cde500
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// pages/login/login.js
 
// 导入封装通用模块方法
import { toast } from '@/utils/extendApi'
// 导入本地存储 api
import { setStorage } from '@/utils/storage'
// 导入接口 API 函数
import { loginMessageUser, loginVerifyMessage, PostLogin } from '@/api/account';
// 导入 ComponentWithStore 方法
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
// 导入 store 对象
import { accountStore } from '@/stores/accountStore'
 
import { STATIC_FILE_BASE_URL } from '@/config/constants'
import { verifyPhone } from '@/utils/toolsValidate';
const app = getApp();
// 使用 ComponentWithStore 方法替换 Component 方法构造页面
ComponentWithStore({
  // 让页面和 Store 对象建立关联
  storeBindings: {
    store: accountStore,
    fields: ['session', 'username'],
    actions: ['setSession', 'setUsername']
  },
  data: {
    activeTab: 'account',
    user: '',
    password: '',
    STATIC_FILE_BASE_URL,
    countNum: null,
    countTimer: null,
    phoneNumber: '',
    verifyCode: '',
  },
  lifetimes: {
    attached() {
      wx.hideHomeButton();
    },
 
  },
  pageLifetimes: {
    hide() {
      this.resetContent();
    }
  },
  methods: {
 
    resetContent() {
      switch (this.data.activeTab) {
        case 'account':
          this.setData({
            user: '',
            password: '',
          })
          break;
        case 'cellPhoneNumber':
          this.setData({
            phoneNumber: '',
            verifyCode: '',
          })
          this.stopCount();
          this.setData({
            countNum: null
          })
        default:
          break;
      }
 
    },
    tabChange: function (event) {
      this.setData({
        activeTab: event.detail.name
      })
      this.resetContent();
    },
    async getSMSClick() {
      if (this.data.countNum !== null) {
        return;
      }
      if (!this.data.phoneNumber) {
        toast({
          title: '请输入手机号码!'
        });
        return;
      }
      console.log(this.data.phoneNumber);
      const isValid = verifyPhone(this.data.phoneNumber)
      console.log(isValid);
      
      if (!isValid) {
        toast({
          title: '请输入正确的手机号码!'
        });
        return;
      }
      this.setData({
        verifyCode: ''
      })
      const res = await loginVerifyMessage({
        phone: this.data.phoneNumber
      },{
        noAuth: true
      })
      
      if (!res?.json_ok) {
        return;
      }
      this.startCount();
    },
    stopCount() {
      clearInterval(this.data.countTimer)
    },
 
    startCount() {
      this.stopCount();
      this.setData({
        countNum: 60
      })
      this.data.countTimer = setInterval(() => {
        if (this.data.countNum === 0) {
          this.setData({
            countNum: null
          })
          clearInterval(this.data.countTimer);
          return;
        }
        this.setData({
          countNum: this.data.countNum - 1
        })
      }, 1000);
    },
 
    handlePhoneNumName(phoneStr){
     return phoneStr.substr(0,3)+"****"+phoneStr.substr(7);
    },
 
    // 授权登录
    login: wx.$_.debounce(async function () {
 
      let res: any = null;
      switch (this.data.activeTab) {
        case 'account':
          if (!this.data.user) {
            toast({
              title: '请输入账号!'
            });
            return;
          }
          if (!this.data.password) {
            toast({
              title: '请输入密码!'
            });
            return;
          }
 
          res = await PostLogin({
            user: this.data.user,
            pass: this.data.password,
          }, {
            noAuth: true
          });
          setStorage('username', this.data.user);
          this.setUsername(this.data.user);
          app.globalData.user.phoneNum = '';
 
          break;
        case 'cellPhoneNumber':
 
          if (!this.data.phoneNumber) {
            toast({
              title: '请输入手机号码!'
            });
            return;
          }
          if (!verifyPhone(this.data.phoneNumber)) {
            toast({
              title: '请输入正确的手机号码!'
            });
            return;
          }
          if (!this.data.verifyCode) {
            toast({
              title: '请输入验证码!'
            });
            return;
          }
          res = await loginMessageUser({
            phone: this.data.phoneNumber,
            code: this.data.verifyCode,
          }, {
            noAuth: true
          });
          const phoneName = this.handlePhoneNumName(this.data.phoneNumber) 
          setStorage('username',phoneName);
          this.setUsername(phoneName);
          app.globalData.user.phoneNum = this.data.phoneNumber;
          break;
        default:
          break;
      }
      if (!res.json_ok || !res.hswatersession) {
        toast({
          title: res.json_msg
        })
        return;
      }
      setStorage('session', res.hswatersession);
      this.setSession(res.hswatersession)
 
      wx.switchTab({
        url: '/pages/question/question'
      });
    }, 500),
  }
})