领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
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
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Practices.ServiceLocation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SoftArcs.WPFSmartLibrary.SmartUserControls;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Bro.UI.Model;
using Bro.Common.Helper;
 
namespace Bro.UI.ViewModel
{
    public class LoginViewModel : ViewModelBase
    {
        #region IDialogService
        public IDialogCoordinator Dialog
        {
            get
            {
                return SimpleIoc.Default.GetInstance<IDialogCoordinator>("Login");
            }
        }
        #endregion
 
        #region Public Properties
        private string version = "";
        public string Version
        {
            get => version;
            set => Set(ref version, value);
        }
 
        private string userName = "";
        public string UserName
        {
            get => userName;
            set => Set(ref userName, value);
        }
 
        private string password = "";
        public string Password
        {
            get => password;
            set => Set(ref password, value);
        }
 
        private string userImageSource = "";
        public string UserImageSource
        {
            get => userImageSource;
            set => Set(ref userImageSource, value);
        }
 
        private List<string> productionCodes = new List<string>();
        public List<string> ProductionCodes
        {
            get => productionCodes;
            set => Set(ref productionCodes, value);
        }
 
        private string selectedProductionCode = "";
        public string SelectedProductionCode
        {
            get => selectedProductionCode;
            set => Set(ref selectedProductionCode, value);
        }
        #endregion 
 
        public RelayCommand<SmartLoginOverlay> Cmmd_Submit { get; set; }
 
        public LoginViewModel()
        {
            Version = " V" + Assembly.GetExecutingAssembly().GetName().Version.ToString();
 
            //string codesStr = ConfigurationManager.AppSettings["ProductionCodes"];
            //if (!string.IsNullOrWhiteSpace(codesStr))
            //{
            //    ProductionCodes = JsonConvert.DeserializeObject<List<string>>(codesStr);
 
            //    SelectedProductionCode = ProductionCodes[0];
            //}
 
            //string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting.json");
            //if (File.Exists(configPath))
            //{
            //    using (StreamReader reader = new StreamReader(configPath, System.Text.Encoding.UTF8))
            //    {
            //        string dataStr = reader.ReadToEnd();
            //        JObject data = JsonConvert.DeserializeObject<JObject>(dataStr);
            //        ProductionCodes = JsonConvert.DeserializeObject<List<string>>(data.Value<string>("ProductionCodes"));
            //    }
            //}
 
            ProductionCodes = SettingHelper.GetProductionCodes();
            SelectedProductionCode = ProductionCodes[0];
 
            Cmmd_Submit = new RelayCommand<SmartLoginOverlay>(OnLoginSubmit, CanLoginSubmit);
        }
 
        private bool CanLoginSubmit(SmartLoginOverlay arg)
        {
            return !(string.IsNullOrWhiteSpace(UserName) || string.IsNullOrWhiteSpace(Password));
        }
 
        private void OnLoginSubmit(SmartLoginOverlay obj)
        {
            if (ValidateUser(this.UserName, this.Password))
            {
                SimpleIoc.Default.Register(() =>
                {
                    LoginInfo info = new LoginInfo();
                    info.UserName = UserName;
                    info.ProductionCode = SelectedProductionCode;
 
                    return info;
                }, true);
                Messenger.Default.Send<object>(null, "LoginOK");
            }
            else
            {
                Dialog.ShowMessageAsync(this, "登录失败", "用户名或密码错误,登录失败", MessageDialogStyle.Affirmative).ContinueWith(t =>
                {
                    UserName = Password = "";
                });
            }
        }
 
        private bool ValidateUser(string username, string password)
        {
            return userName.ToUpper() == "ADMIN" && password.ToUpper() == "ADMIN";
        }
    }
}