wangguan
2020-10-29 33df6342bf773286a8876abbe0e43321fb7b93d2
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using KTGMGemClient;
using UnityEngine.UI;
using LitJson;
using UnityEngine.Networking;
using Protobuf;
using Google.Protobuf;
 
public class LoginUI : MonoBehaviour
{
    private AsyncOperation async = null;
    bool isLogining;
    private JsonData loginData;
 
    // Start is called before the first frame update
    void Start()
    {
        TDAA_SDKManager.Ins.SDKInit(ChannelID.Gm.ToString());
        TDAA_SDKManager.Ins.Statistics(1);//成功加载登陆界面的人数
        isLogining = false;
 
        transform.Find("Panel/Button").GetComponent<Button>().onClick.AddListener(OnClickLoginBtn);
        transform.Find("Panel/Button (1)").GetComponent<Button>().onClick.AddListener(OnClickResetBtn);
 
        if (Application.platform == RuntimePlatform.Android)
        {
            //GetetDeviceIMEI();//获取安卓手机IMEI
            imei0 = TDAA_SDKManager.Ins.GetDeviceId();//使用设备ID
        }
        else if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            imei0 = "MyTestGemBattle123";
        }
 
 
 
    }
 
    private void OnEnable()
    {
        SocketEvent.Ins.Add<IMessage>((int)Opcode.LoginS2C, LoginS2C);
 
    }
 
    private void OnDestroy()
    {
        SocketEvent.Ins.Remove<IMessage>((int)Opcode.LoginS2C, LoginS2C);
 
    }
 
    #region 获得安卓手机上的IMEI号
    public string imei0 = "";
    public string imei1 = "";
    public string meid = "";
 
    void GetetDeviceIMEI()
    {
        var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        var context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        var telephoneyManager = context.Call<AndroidJavaObject>("getSystemService", "phone");
        imei0 = telephoneyManager.Call<string>("getImei", 0);//如果手机双卡 双待  就会有两个MIEI号
        imei1 = telephoneyManager.Call<string>("getImei", 1);
        meid = telephoneyManager.Call<string>("getMeid");//电信的手机 是MEID
 
        CommonDebugHelper.Debug($"获取到了安卓手机的IMEI!!! imei0:{imei0}  imei1:{imei1}  meid:{meid}");
    }
 
    #endregion
 
    private void OnClickResetBtn(){
        Debug.LogError("清空了所有数据");
        PlayerPrefs.DeleteAll();
        TDAA_SDKManager.Ins.Reset();
    }
 
 
    private void OnClickLoginBtn()
    {
        TDAA_SDKManager.Ins.Statistics(2);//埋点
        LoginRequest();
    }
 
    /// <summary>
    /// 登录请求处理
    /// </summary>
    /// <param name="req"></param>
    private void LoginRequest()
    {
        if (!isLogining)
        {
            Debug.Log("登录");
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                Debug.Log("编辑器直接登录");
 
                //TDAA_SDKManager.Ins.Login();
                TDAA_SDKManager.Ins.Statistics(3);//埋点
 
                //StartCoroutine(loginMy());
                StartCoroutine(LoadScene());
                return;
            }
 
            isLogining = true;
            string url = GameConfig.IsDebug ? GameConfig.TestLoginUrl : GameConfig.LoginUrl;
            JsonData data = new JsonData();
            data["account"] = imei0;
            data["password"] = "123456";
            data["channelid"] = (int)ChannelID.Gm;
            byte[] postBytes = System.Text.Encoding.Default.GetBytes(data.ToJson());
            StartCoroutine(Post(url, postBytes));
        }
    }
 
    IEnumerator Post(string url, byte[] postBytes)
    {
        UnityWebRequest request = UnityWebRequest.Post(url, "POST");
        request.uploadHandler = new UploadHandlerRaw(postBytes);
        request.downloadHandler = new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");
        yield return request.SendWebRequest();
 
        if (request.isDone)
        {
            string text = request.downloadHandler.text;
            loginData = JsonMapper.ToObject(text);
            if (loginData.Keys.Contains("errorcode"))
            {
                JsonData errorData = loginData["errorcode"];
                isLogining = false;
                CommonDebugHelper.DebugSocket(int.Parse(errorData.ToString()));
 
                yield break;
            }
 
            if (!string.IsNullOrEmpty(text))
            {
                EventCenter.Ins.Add<bool>((int)KTGMGemClient.EventType.MasterSocketConnectResult, MasterSocketConnectSuccess);
                MasterSocket.Ins.MasterIp = loginData["masterip"].ToString();
                MasterSocket.Ins.MasterPort = int.Parse(loginData["masterport"].ToString());
                MasterSocket.Ins.StartConnect();
 
                // System.DateTime dt = GameUtils.GetTime(loginData["timestamp"].ToString());
                // GameConfig.dateTime = dt;
                // GameConfig.timeSpan = dt - System.DateTime.Now;
            }
            else
            {
                isLogining = false;
                //UIManager.Instance.ShowWarningWind("没有登录数据返回,text:" + text);
                //CommonDebugHelper.DebugError("--------------------- 没有登录数据返回 ---------------------text:" + text);
            }
        }
        else
        {
            isLogining = false;
            //UIManager.Instance.ShowWarningWind(request.error);
 
            //CommonDebugHelper.DebugError("登录失败 request.error" + request.error);
        }
    }
 
    private void MasterSocketConnectSuccess(bool res)
    {
        if (res)
        {
            Login_C2S login = new Login_C2S();
            login.Username = loginData["username"].ToString();
            login.Channel = (int)ChannelID.Gm;
            login.SessionKey = loginData["sessionKey"].ToString();
            MasterSocket.Ins.SendMsg(Opcode.LoginC2S, login);
        }
        else
        {
            CommonDebugHelper.DebugError("Socket 链接失败了");
        }
    }
 
    private void LoginS2C(IMessage msg)
    {
        Login_S2C login = (Login_S2C)msg;
        if (login.Errorcode == 0)
        {
            Debug.Log("--------------------- 登录成功 ---------------------");
            //UserDataMsg userData = login.Userdata;
 
            //TDAA_SDKManager.Ins.Login();
            TDAA_SDKManager.Ins.Statistics(3);//埋点
 
            //StartCoroutine(loginMy());
            StartCoroutine(LoadScene());
 
        }
        else
        {
            CommonDebugHelper.DebugSocket(login.Errorcode);
            isLogining = false;
        }
    }
 
 
    IEnumerator LoadScene()
    {
        async = SceneManager.LoadSceneAsync(GameConfig.NextSceneName);
        async.allowSceneActivation = true;
        /*while (!async.isDone)
        {
            
            if (async.progress < 0.9f)
                progressValue = async.progress;
            else
                progressValue = 1.0f;
            slider.value = progressValue;
            progress.text = (int)(slider.value * 100) + " %";
            
            if (async.progress >= 0.89)
            {
                Debug.Log("进展时间分别是:" + async.progress + "," + totalTime);
                if( totalTime >= 3.0f )
                    async.allowSceneActivation = true;
                
                progress.text = "按任意键继续";
                if (Input.anyKeyDown)
                {
                    async.allowSceneActivation = true;
                }
 
            }
 
            //yield return null;
        }*/
        yield return null;
    }
 
 
    IEnumerator loginMy()
    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = SceneManager.LoadSceneAsync(GameConfig.NextSceneName);
        op.allowSceneActivation = false;
        while (op.progress < 0.9f) //此处如果是 <= 0.9f 则会出现死循环所以必须小0.9
        {
            toProgress = (int)op.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();//ui渲染完成之后
            }
        }
        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }
        op.allowSceneActivation = true;
        yield break;
 
    }
 
    private void SetLoadingPercentage(int displayProgress)
    {
        CommonDebugHelper.Debug($"当前进度{displayProgress}%");
 
    }
 
}