祖安之光
2025-08-07 ad5c4cc086a40708e66040574ff1d465b66304b6
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
const app = getApp();
const loginTop = '../../static/loginBg.png';
const loginPic = '../../static/loginPic.png';
const api = require('../../utils/api');
const cloudApi = require('../../utils/cloudApi')
Page({
  data: {
    userName: '',
    loginTop: loginTop,
    loginPic: loginPic,
    userFocused: false,
    codeFocused: false,
    form: {
      username: '',
      password: '',
      code: '',
      uuid: ''
    },
    isPwd: true,
    isLogining: false,
    saveAccount: false
  },
  
  onShow() {
    
  },
  
  onLoad() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    console.log(wx.getStorageSync('isSave'),'get1')
    if(wx.getStorageSync('isSave')){
      this.setData({
        saveAccount: true,
        form: {
          username: wx.getStorageSync('username') || '',
          password: wx.getStorageSync('pwd') || '',
          code: '',
          uuid: ''
        }
      })
    }
  },
  
  base64Encode(e) {
    const utf8Bytes = unescape(encodeURIComponent(e));
    const array = new Uint8Array(utf8Bytes.length);
    for (let i = 0; i < utf8Bytes.length; i++) {
      array[i] = utf8Bytes.charCodeAt(i);
    }
    return wx.arrayBufferToBase64(array);
  },
  
  // 输入框实现双向绑定
  onFormInput(e) {
    const field = e.currentTarget.dataset.field; // 获取字段名
    this.setData({
      [field]: e.detail.value  // 动态更新对应字段
    });
  },
  switchChange(e){
    this.setData({ saveAccount: e.detail.value })
    wx.setStorageSync('isSave',e.detail.value)
  },
  Login(){
    if(this.data.form.username == ''){
      wx.showToast({
        title: '请输入用户名',
        duration: 1000
      })
      return
    }
    if(this.data.form.password == ''){
      wx.showToast({
        title: '请输入密码',
        duration: 1000
      })
      return
    }
    this.setData({
      isLogining: true
    });
    
    const {username, password, code, uuid} = this.data.form;
    const data = {username, password, code, uuid};
    data.password = this.base64Encode(data.password);
    wx.removeStorageSync('tk');
 
    // api请求
    api.login(data).then(res => {
      console.log(res,'res')
      if (res.code === 200) {
        this.setData({
          isLogining: false
        });
        wx.setStorageSync("username", this.data.form.username);
        wx.setStorageSync("pwd", this.data.form.password);
        wx.setStorageSync("tk", res.data.token);
        wx.setStorageSync("uid", res.data.id);
        wx.setStorageSync('user', res.data);
        wx.switchTab({
          url: '/pages/tabBar/firstPage/firstPage'
        });
      } else {
        wx.showToast({
          icon: "none",
          title: res.message
        });
        this.setData({
          isLogining: false
        });
      }
    }).catch(err => {
      this.setData({
        'form.password': '',
        isLogining: false
      });
    });
  },
  
  focusUser(){
    this.setData({
      userFocused: true,
      codeFocused: false
    });
  },
  
  focusCode(){
    this.setData({
      codeFocused: true,
      userFocused: false
    });
  },
  
  togglePwd(){
    this.setData({
      isPwd: !this.data.isPwd
    });
  }
});