duheng
2025-03-20 bc0ed5b6cfda6c72c06f451b77da8518c41ab210
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using Newtonsoft.Json.Linq;
using Yw;
using Yw.Auth;
using Yw.Untity;
using Yw.WinFrmUI;
 
namespace HStation.WinFrmUI
{
    public partial class PersonalCenterDlg : DevExpress.XtraEditors.XtraForm
    {
        public PersonalCenterDlg()
        {
            InitializeComponent();
            this.Load += PersonalCenterDlg_Load;
            this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
        }
 
        private const string _smsTemplate = "hzkw_sms_template";//手机号登录模板
        private const string _vxTemplate = "hzkw_wx_template";//微信登录模板
        private const string _software = "HStation_XHS_DESKTOP";//软件编码
 
        private long _wxId;
        private long _phoneId;
 
        //初始化
        private async void PersonalCenterDlg_Load(object? sender, EventArgs e)
        {
            this.txtEditUserName.Text = LoginUserInfo.UserName;
            this.txtEditAdminType.Text = LoginUserInfo.AdminType.GetDisplayText();
            this.txtAccountName.Text = LoginUserInfo.LoginAccountName;
            this.textAccountType.Text = LoginUserInfo.LoginTypeName;
            var allPhones = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.GetSmsByUserID(LoginUserInfo.UserID);
            var allWeChat = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.GetWechatByUserID(LoginUserInfo.UserID);
            if (allPhones != null && allPhones.Any())
            {
                _phoneId = allPhones.First().ID;
                this.buttonEditPhone.EditValue = allPhones.First().Credential;
            }
            if (allWeChat != null && allWeChat.Any())
            {
                _wxId = allWeChat.First().ID;
                JObject jObject = JObject.Parse(allWeChat.First().ExtraInfo);
                if (jObject != null)
                {
                    string nickname = (string)jObject["nickname"];
                    if (nickname != null)
                    {
                        this.buttonEditWeChat.EditValue = nickname;
                    }
                }
            }
        }
 
        public class PhoneData
        {
            public long ID { get; set; }
            public string Phone { get; set; }
        }
 
        public class WxData
        {
            public long ID { get; set; }
            public string Wxid { get; set; }
        }
 
        //修改密码
        private void BtnEditPwd_Click(object sender, EventArgs e)
        {
            var dlg = new UpdatePwdDlg();
            dlg.SetBindingData(LoginUserInfo.LoginAccountID);
            dlg.ShowDialog();
        }
 
        //微信绑定
        private async Task<long> WechatBinding(string code)
        {
            var loginType = await BLLFactory<Yw.BLL.UserLoginType>.Instance.GetByIdentifier(LoginType.Wechat);
            if (loginType != null)
            {
                var tokenInfo = await BLLFactory<Yw.BLL.ToolWechat>.Instance.GetTokenInfo(code, _vxTemplate);
                if (tokenInfo == null)
                {
                    return default;
                }
                var userInfo = await BLLFactory<Yw.BLL.ToolWechat>.Instance.GetUserInfo(tokenInfo.access_token, tokenInfo.openid);
                if (userInfo == null)
                {
                    return default;
                }
 
                var isExist = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.IsExist
                    (
                        Yw.WinFrmUI.LoginUserInfo.CorpID,
                        loginType.ID,
                        _vxTemplate,
                        tokenInfo.openid
                    );
                if (isExist)
                {
                    TipFormHelper.ShowError("账户已存在!");
                    return default;
                }
 
                var vmo = new Yw.Vmo.AddUserLoginAccountVmo()
                {
                    CorpID = Yw.WinFrmUI.LoginUserInfo.CorpID,
                    UserID = Yw.WinFrmUI.LoginUserInfo.UserID,
                    LoginTypeID = loginType.ID,
                    Identifier = _vxTemplate,
                    Credential = tokenInfo.openid,
                    IfVerified = true,
                    ExtraInfo = JsonHelper.Object2Json(userInfo)
                };
                var id = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.Insert(vmo);
                if (id > 0)
                {
                    TipFormHelper.ShowSucceed("绑定成功!");
                    return id;
                }
                else
                {
                    TipFormHelper.ShowError("绑定失败!");
                    return default;
                }
            };
            return default;
        }
 
        //手机绑定
        private async Task<long> PhoneBinding(string number)
        {
            var loginType = await BLLFactory<Yw.BLL.UserLoginType>.Instance.GetByIdentifier(LoginType.SMS);
            if (loginType != null)
            {
                var isExist = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.IsExist
                    (
                        Yw.WinFrmUI.LoginUserInfo.CorpID,
                        loginType.ID,
                        _smsTemplate,
                        number
                    );
                if (isExist)
                {
                    TipFormHelper.ShowError("账户已存在!");
                    return default;
                }
                var vmo = new Yw.Vmo.AddUserLoginAccountVmo()
                {
                    CorpID = Yw.WinFrmUI.LoginUserInfo.CorpID,
                    UserID = Yw.WinFrmUI.LoginUserInfo.UserID,
                    LoginTypeID = loginType.ID,
                    Identifier = _smsTemplate,
                    Credential = number,
                    IfVerified = true
                };
                var id = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.Insert(vmo);
                if (id > 0)
                {
                    TipFormHelper.ShowSucceed("绑定成功!");
                    return id;
                }
                else
                {
                    TipFormHelper.ShowError("绑定失败!");
                    return default;
                }
            }
            return default;
        }
 
        //个人信息保存
        private async void BtnSave_Click(object sender, EventArgs e)
        {
            var vmo = await BLLFactory<Yw.BLL.User>.Instance.GetByID(LoginUserInfo.UserID);
            vmo.Name = this.txtEditUserName.Text;
            var bol = await BLLFactory<Yw.BLL.User>.Instance.Update(vmo);
            if (!bol)
            {
                TipFormHelper.ShowError("更新失败!");
                return;
            }
            else
            {
                LoginUserInfo.UserName = this.txtEditUserName.Text;
                TipFormHelper.ShowSucceed("更新成功!");
            }
        }
 
        /*
                //添加手机号
                private void btnAddPhone_Click(object sender, EventArgs e)
                {
                    var dlg = new AddPhoneNumberDlg();
                    dlg.ReloadDataEvent += async (phoneNumber) =>
                    {
                        var id = await PhoneBinding(phoneNumber);
                        if (id > 0)
                        {
                            _phoneNuberBinding.Add(new PhoneData { Phone = phoneNumber, ID = id });
                            this.gridControl1.RefreshDataSource();
                        }
                    };
                    dlg.ShowDialog();
                }*/
 
        /*     //添加微信
             private void btnAddWx_Click(object sender, EventArgs e)
             {
                 var dlg = new WechatBindingDlg();
                 dlg.SetBindingData();
                 dlg.CodeReloadData += async (code) =>
                 {
                     var id = await WechatBinding(code);
                     if (id > 0)
                     {
                         _WxBinding.Add(new WxData { Wxid = code, ID = id });
                         this.gridControl2.RefreshDataSource();
                     }
                 };
                 dlg.ShowDialog();
             }*/
 
        private async void buttonEditPhone_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString() == "import")
            {
                if (this.buttonEditPhone.EditValue != null)
                {
                    TipFormHelper.ShowWarn("已存在手机号");
                    return;
                }
                var dlg = new AddPhoneNumberDlg();
                dlg.ReloadDataEvent += async (phoneNumber) =>
                {
                    var id = await PhoneBinding(phoneNumber);
                    if (id > 0)
                    {
                        _phoneId = id;
                        this.buttonEditPhone.EditValue = phoneNumber;
                    }
                };
                dlg.ShowDialog();
            }
            else if (e.Button.Tag.ToString() == "delete")
            {
                if (_phoneId > 0)
                {
                    var bol = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.DeleteByID(_phoneId);
                    if (!bol)
                    {
                        TipFormHelper.ShowError("删除失败!");
                        return;
                    }
                    TipFormHelper.ShowSucceed("删除成功!");
                    _phoneId = default;
                    this.buttonEditPhone.EditValue = null;
                }
            }
        }
 
        private async void buttonEditWeChat_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString() == "import")
            {
                if (this.buttonEditWeChat.EditValue != null)
                {
                    TipFormHelper.ShowWarn("已存在微信");
                    return;
                }
                var dlg = new WechatBindingDlg();
                dlg.SetBindingData();
                dlg.CodeReloadData += async (code) =>
                {
                    var id = await WechatBinding(code);
                    if (id > 0)
                    {
                        _wxId = id;
                        var allWeChat = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.GetWechatByUserID(LoginUserInfo.UserID);
                        if (allWeChat != null && allWeChat.Any())
                        {
                            JObject jObject = JObject.Parse(allWeChat.First().ExtraInfo);
                            if (jObject != null)
                            {
                                string nickname = (string)jObject["nickname"];
                                if (nickname != null)
                                {
                                    this.buttonEditWeChat.EditValue = nickname;
                                }
                            }
                        }
                    }
                };
                dlg.ShowDialog();
            }
            else if (e.Button.Tag.ToString() == "delete")
            {
                if (_wxId > 0)
                {
                    var bol = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.DeleteByID(_wxId);
                    if (!bol)
                    {
                        TipFormHelper.ShowError("删除失败!");
                        return;
                    }
                    TipFormHelper.ShowSucceed("删除成功!");
                    _wxId = default;
                    this.buttonEditWeChat.EditValue = null;
                }
            }
        }
    }
}