lvjincheng 4 роки тому
батько
коміт
00bc1b1aeb

+ 155 - 0
Assets/BowArrow/Scripts/Manager/LoginView/LoginView.cs

@@ -0,0 +1,155 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+using System.Text.RegularExpressions;
+
+public class LoginView : MonoBehaviour
+{
+    //登录方式-左侧切换按钮-按钮纹理
+    [SerializeField] Sprite[] loginModeSprites;
+    //登录方式-左侧切换按钮
+    [SerializeField] GameObject loginNormalTab;
+    [SerializeField] GameObject loginPhoneTab;
+    //用户登录
+    [SerializeField] GameObject loginInUser;
+    [SerializeField] GameObject loginInPWD;
+    [SerializeField] GameObject loginInCaptcha1;
+    //手机登录
+    [SerializeField] GameObject loginInPhone;
+    [SerializeField] GameObject loginInCode;
+    [SerializeField] GameObject loginValidTime;
+    [SerializeField] GameObject loginInCaptcha2;
+    [SerializeField] Text loginTip;
+    //协议栏
+    [SerializeField] Transform agreementTF;
+
+    int loginMode = 1;
+
+    void Start()
+    {
+        InitInputLimit();
+        InitAgreementOnClickListeners();
+        SelectLoginMode(1);
+    }
+
+    void InitInputLimit() {
+        GameObject[] inputNodes = {loginInUser, loginInPWD};
+        foreach (var inputNode in inputNodes) {
+            InputField inputField = GetInputField(inputNode);
+            inputField.onValueChanged.AddListener(delegate(string text) {
+                Match match = new Regex("[^A-Za-z0-9]").Match(text);
+                if (match.Success) {
+                    inputField.text = text.Replace(match.Value, "");
+                }
+            });       
+        }
+    }
+
+    void InitAgreementOnClickListeners() {
+        agreementTF.Find("TextA").GetComponent<Button>().onClick.AddListener(delegate() {
+            GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AgreementView"));
+            o.GetComponent<AgreementView>().EnterUserAgreement();
+        });
+        agreementTF.Find("TextB").GetComponent<Button>().onClick.AddListener(delegate() {
+            GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AgreementView"));
+            o.GetComponent<AgreementView>().EnterPrivacyAgreement();
+        });
+    }
+
+    public void SelectLoginMode(int mode) {
+        loginMode = mode;
+        if (loginMode == 1) {
+            loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[1];
+            loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[0];
+            loginInUser.SetActive(true);
+            loginInPWD.SetActive(true);
+            loginInCaptcha1.SetActive(true);
+            loginInPhone.SetActive(false);
+            loginInCode.SetActive(false);
+            loginValidTime.SetActive(false);
+            loginInCaptcha2.SetActive(false);
+            if (CaptchaController.ins.captcha_Login < 0) {
+                StartCoroutine(CaptchaController.ins.GetCaptcha(
+                    CaptchaController.CaptchaType.Login,
+                    loginInCaptcha1.transform.Find("CodeImage").GetComponent<Image>()
+                ));
+            }
+        }
+        else if (loginMode == 2) {
+            loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[0];
+            loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[1];
+            loginInUser.SetActive(false);
+            loginInPWD.SetActive(false);
+            loginInCaptcha1.SetActive(false);
+            loginInPhone.SetActive(true);
+            loginInCode.SetActive(true);
+            loginValidTime.SetActive(true);
+            loginInCaptcha2.SetActive(true);
+            if (CaptchaController.ins.captcha_LoginPhone < 0) {
+                StartCoroutine(CaptchaController.ins.GetCaptcha(
+                    CaptchaController.CaptchaType.LoginPhone,
+                    loginInCaptcha2.transform.Find("CodeImage").GetComponent<Image>()
+                ));
+            }
+        }
+        loginTip.GetComponent<TextAutoLanguage>().SetText(0);
+    }
+
+    InputField GetInputField(GameObject inputNode) {
+        return inputNode.transform.Find("InputField").GetComponent<InputField>();
+    }
+
+    public void login() {
+        if (loginMode == 1) {
+            LoginNormal(); 
+        } else if (loginMode == 2) {
+            LoginByPhone();
+        }
+    }
+
+    void LoginNormal() {
+        InputField user = GetInputField(loginInUser);
+        if (user.text.Trim().Length == 0) {
+            loginTip.color = Color.yellow;
+            loginTip.GetComponent<TextAutoLanguage>().SetText(41);
+            return;
+        }
+        InputField pwd = GetInputField(loginInPWD);
+        if (pwd.text.Trim().Length == 0) {
+            loginTip.color = Color.yellow;
+            loginTip.GetComponent<TextAutoLanguage>().SetText(42);
+            return;
+        }
+        // InputField captcha = GetInputField(loginInCaptcha1);
+        // if (captcha.text.Trim().Length == 0) {
+        //     loginTip.color = Color.yellow;
+        //     loginTip.GetComponent<TextAutoLanguage>().SetText(42);
+        //     return;
+        // }
+        // UserInfos userInfos = GetUserInfos();
+        // foreach (var userInfo in userInfos.list)
+        // {
+        //     if (userInfo.user == user.text) {
+        //         if (userInfo.pwd == pwd.text) {
+        //             loginTip.color = Color.green;
+        //             loginTip.GetComponent<TextAutoLanguage>().SetText(43);
+        //             myUserInfo = userInfo;
+        //             PlayerPrefs.SetString("LoginRecord_User_" + userInfo_version, userInfo.user);
+        //             PlayerPrefs.SetString("LoginRecord_PWD_" + userInfo_version, userInfo.pwd);
+        //             SceneManager.LoadScene("Home", LoadSceneMode.Single);               
+        //         } else {
+        //             loginTip.color = Color.red;
+        //             loginTip.GetComponent<TextAutoLanguage>().SetText(44);
+        //         }
+        //         return;
+        //     }
+        // }
+        // loginTip.color = Color.yellow;
+        // loginTip.GetComponent<TextAutoLanguage>().SetText(45);
+    }
+
+    void LoginByPhone() {
+
+    }
+}

+ 11 - 0
Assets/BowArrow/Scripts/Manager/LoginView/LoginView.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 3b861397e7115d14f9ce799e60a6287c
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 208 - 0
Assets/BowArrow/Scripts/Manager/LoginView/RegisterView.cs

@@ -0,0 +1,208 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+using UnityEngine.SceneManagement;
+using UnityEngine.Networking;
+using System.Text.RegularExpressions;
+using DG.Tweening;
+using Newtonsoft.Json;
+
+/* 注册界面 */
+public class RegisterView : MonoBehaviour
+{
+    [SerializeField] GameObject registerInUser;
+    [SerializeField] GameObject registerInPWD1;
+    [SerializeField] GameObject registerInPWD2;
+    [SerializeField] GameObject registerInCaptcha;
+    [SerializeField] GameObject registerInNickname;
+    [SerializeField] GameObject registerInGender;
+    [SerializeField] GameObject registerInBirthday;
+    [SerializeField] GameObject registerInLocation;
+    [SerializeField] GameObject btnNext;
+    [SerializeField] GameObject btnSave;
+    [SerializeField] Text registerTip;
+
+    void OnEnable() 
+    {
+        InitPage();
+    }
+
+    void Start()
+    {
+        InitInputLimit();
+    }
+
+    void InitInputLimit() {
+        GameObject[] inputNodes = {registerInUser, registerInPWD1, registerInPWD2};
+        foreach (var inputNode in inputNodes) {
+            InputField inputField = GetInputField(inputNode);
+            inputField.onValueChanged.AddListener(delegate(string text) {
+                Match match = new Regex("[^A-Za-z0-9]").Match(text);
+                if (match.Success) {
+                    inputField.text = text.Replace(match.Value, "");
+                }
+            });       
+        }
+    }
+
+    void InitPage(bool isNext = false) {
+        registerInUser.SetActive(!isNext);
+        registerInPWD1.SetActive(!isNext);
+        registerInPWD2.SetActive(!isNext);
+        registerInCaptcha.SetActive(!isNext);
+        btnNext.SetActive(!isNext);
+        registerInNickname.SetActive(isNext);
+        registerInGender.SetActive(isNext);
+        registerInBirthday.SetActive(isNext);
+        registerInLocation.SetActive(isNext);
+        btnSave.SetActive(isNext);
+        if (!isNext) {
+            if (CaptchaController.ins.captcha_Register < 0) {
+                StartCoroutine(CaptchaController.ins.GetCaptcha(
+                    CaptchaController.CaptchaType.Register,
+                    registerInCaptcha.transform.Find("CodeImage").GetComponent<Image>()
+                ));
+            }
+        }
+    }
+
+    InputField GetInputField(GameObject inputNode) {
+        return inputNode.transform.Find("InputField").GetComponent<InputField>();
+    }
+
+    private string usrRecord = "";
+    private string pwdRecord = "";
+    public void RegisterNext()
+    {
+        InputField user = GetInputField(registerInUser);
+        if (user.text.Length < 6) {
+            registerTip.color = Color.red;
+            registerTip.GetComponent<TextAutoLanguage>().SetText(46);
+            return;
+        }
+        InputField pwd1 = GetInputField(registerInPWD1);
+        if (pwd1.text.Length < 6) {
+            registerTip.color = Color.red;
+            registerTip.GetComponent<TextAutoLanguage>().SetText(47);
+            return;
+        }
+        InputField pwd2 = GetInputField(registerInPWD2);
+        if (pwd1.text != pwd2.text) {
+            registerTip.color = Color.red;
+            registerTip.GetComponent<TextAutoLanguage>().SetText(48);
+            return;
+        }
+        InputField captcha = GetInputField(registerInCaptcha);
+        if (!captcha.text.Equals(CaptchaController.ins.captcha_Register.ToString())) {
+            Debug.Log("验证码错误");
+            return;
+        }
+        usrRecord = user.text;
+        pwdRecord = pwd1.text;
+        StartCoroutine(LoginController.ins.Register(
+            usrRecord, pwdRecord, (res) => {
+                Debug.Log(res.msg);
+                if (res.code == 0) {
+                    InitPage(true); //前往完善用户信息
+                }
+            }
+        ));
+        // InputField nickname = GetInputField(registerInNickname);
+        // if (nickname.text.Trim().Length == 0) {
+        //     registerTip.color = Color.yellow;
+        //     registerTip.GetComponent<TextAutoLanguage>().SetText(49);
+        //     return;
+        // }
+        // UserInfos userInfos = GetUserInfos();
+        // foreach (var userInfo in userInfos.list)
+        // {
+        //     if (userInfo.user == user.text) {
+        //         registerTip.color = Color.yellow;
+        //         registerTip.GetComponent<TextAutoLanguage>().SetText(50);
+        //         return;
+        //     }
+        // }
+        // UserInfo userInfo1 = new UserInfo();
+        // userInfo1.user = user.text;
+        // userInfo1.pwd = pwd1.text;
+        // userInfo1.nickname = nickname.text;
+        // Transform toggleGroup = registerInGender.transform.Find("ToggleGroup");
+        // for (int i = 0; i < toggleGroup.childCount; i++) {
+        //     if (toggleGroup.GetChild(i).GetComponent<Toggle>().isOn) {
+        //         userInfo1.gender = i == 0 ? 1 : 2;
+        //         break;
+        //     }
+        // }
+        // userInfos.list.Add(userInfo1);
+        // SetUserInfos(userInfos);
+        // registerTip.color = Color.green;
+        // registerTip.GetComponent<TextAutoLanguage>().SetText(51);
+        // //自动跳转到登录并填写登录信息
+        // GetInputField(loginInUser).text = userInfo1.user;
+        // GetInputField(loginInPWD).text = userInfo1.pwd;
+        // SelectLoginMode(1);
+        // Invoke("showLoginView", 0.5f);
+    }
+
+    public void RegisterSave() {
+        InputField nickname = GetInputField(registerInNickname);
+        if (nickname.text.Trim().Length == 0) {
+            registerTip.color = Color.yellow;
+            registerTip.GetComponent<TextAutoLanguage>().SetText(49);
+            return;
+        }
+        int gender = 0;
+        Transform toggleGroup = registerInGender.transform.Find("ToggleGroup");
+        for (int i = 0; i < toggleGroup.childCount; i++) {
+            if (toggleGroup.GetChild(i).GetComponent<Toggle>().isOn) {
+                gender = i == 0 ? 1 : 2;
+                break;
+            }
+        }
+        InputField birthday = GetInputField(registerInBirthday);
+        if (birthday.text.Length != 10) {
+            Debug.Log("未填写出生日期");
+            return;
+        }
+        InputField location = GetInputField(registerInLocation);
+        if (location.text.Length == 0) {
+            Debug.Log("未填写所在地区");
+            return;
+        }
+        StartCoroutine(LoginController.ins.CompleteUserInfo(
+            usrRecord, pwdRecord, nickname.text, gender, birthday.text,
+            countryCode, stateCode, cityCode 
+            ,(res) => {
+                Debug.Log(res.msg);
+                if (res.code == 0) {
+                    GameObject.FindObjectOfType<LoginMgr>().showLoginView();
+                }
+            }
+        ));
+    }
+
+    [SerializeField] GameObject datePickerPrefab;
+    public void OpenDatePicker() {
+        GameObject o = GameObject.Instantiate(datePickerPrefab);
+        o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
+            GetInputField(registerInBirthday).text = picker.GetSelectDateStr();
+        };
+    }
+
+    [SerializeField] GameObject locationPickerPrefab;
+    private String countryCode = "", stateCode = "", cityCode = "";
+    public void OpenLocationPicker() {
+        GameObject o = GameObject.Instantiate(locationPickerPrefab);
+        o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
+            countryCode = info.GetCountryRegion().Item2;
+            stateCode = info.GetState().Item2;
+            cityCode = info.GetCity().Item2;
+            GetInputField(registerInLocation).text = 
+                info.GetCountryRegion().Item1 + " " + 
+                info.GetState().Item1 + " " + 
+                info.GetCity().Item1;
+        };
+    }
+}

+ 11 - 0
Assets/BowArrow/Scripts/Manager/LoginView/RegisterView.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: af0f12b4024593f43b87368562d2f957
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 8 - 0
Assets/BowArrow/Scripts/Network/Captcha.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 1f666136c5a747444853632e78da37c2
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 39 - 0
Assets/BowArrow/Scripts/Network/Captcha/CaptchaController.cs

@@ -0,0 +1,39 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.Networking;
+using UnityEngine.UI;
+
+//验证码控制器
+public class CaptchaController : Singleton<CaptchaController>
+{
+    public IEnumerator GetCaptcha(CaptchaType type, Image targetRenderImage)
+    {
+        int code = UnityEngine.Random.Range(1000, 10000);
+        UnityWebRequest uwr = new UnityWebRequest(CommonConfig.businessServerURI + "/api/createCaptcha?code=" + code, UnityWebRequest.kHttpVerbGET);
+        uwr.downloadHandler = new DownloadHandlerTexture();
+        yield return uwr.SendWebRequest();
+        Texture2D texture = DownloadHandlerTexture.GetContent(uwr);
+        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
+        switch (type) {
+            case CaptchaType.Login:
+                captcha_Login = code;
+                targetRenderImage.sprite = sprite;
+                break;
+            case CaptchaType.LoginPhone:
+                captcha_LoginPhone = code;
+                targetRenderImage.sprite = sprite;
+                break;
+            case CaptchaType.Register:
+                captcha_Register = code;
+                targetRenderImage.sprite = sprite;
+                break;
+        }
+    }
+    public enum CaptchaType {
+        Login, LoginPhone, Register
+    }
+    public int captcha_Login = -222222222;
+    public int captcha_LoginPhone = -222222222;
+    public int captcha_Register = -222222222;
+}

+ 11 - 0
Assets/BowArrow/Scripts/Network/Captcha/CaptchaController.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 183c05cde177bd747a60ab22276199d3
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 8 - 0
Assets/BowArrow/Scripts/Network/Login.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: dd49075531a907943bbc740d621c4ae7
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 48 - 0
Assets/BowArrow/Scripts/Network/Login/LoginController.cs

@@ -0,0 +1,48 @@
+using System.Collections;
+using System.Collections.Generic;
+using System;
+using UnityEngine;
+using UnityEngine.Networking;
+using Newtonsoft.Json;
+
+public class LoginController : Singleton<LoginController>
+{
+    public IEnumerator Register(string username, string password, Action<RequestResult> callback) {
+        string url = CommonConfig.businessServerURI + "/gameLogin/register";
+        WWWForm form = new WWWForm();
+        form.AddField("username", username);
+        form.AddField("password", password);
+        UnityWebRequest request = UnityWebRequest.Post(url, form);
+        yield return request.SendWebRequest();
+        RequestResult requestResult = JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text);
+        if (callback != null) callback(requestResult);
+    }
+
+    public IEnumerator CompleteUserInfo(
+        string username, string password, 
+        string nickname, int gender, string birthday,
+        string countryCode, string stateCode, string cityCode,
+        Action<RequestResult> callback
+    ) {
+        string url = CommonConfig.businessServerURI + "/gameLogin/completeUserInfo";
+        WWWForm form = new WWWForm();
+        form.AddField("username", username);
+        form.AddField("password", password);
+        form.AddField("nickname", nickname);
+        form.AddField("gender", gender);
+        form.AddField("birthday", birthday);
+        form.AddField("countryCode", countryCode);
+        form.AddField("stateCode", stateCode);
+        form.AddField("cityCode", cityCode);
+        UnityWebRequest request = UnityWebRequest.Post(url, form);
+        yield return request.SendWebRequest();
+        RequestResult requestResult = JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text);
+        if (callback != null) callback(requestResult);
+    }
+}
+
+public class RequestResult {
+    public int code;
+    public object data;
+    public string msg;
+}

+ 11 - 0
Assets/BowArrow/Scripts/Network/Login/LoginController.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 63a411a2f0f6022419125f350336aa7c
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: