CaptchaController.cs 1.2 KB

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7. /* Http控制器-验证码 */
  8. public class CaptchaController : JCUnityLib.Singleton<CaptchaController>
  9. {
  10. int[] builtInCaptchaList = {3825, 4472, 9483};
  11. public IEnumerator GetCaptcha(Image targetRenderImage, Action<int> cb)
  12. {
  13. if (targetRenderImage.sprite == null) {
  14. int codeA = builtInCaptchaList[UnityEngine.Random.Range(0, 3)];
  15. targetRenderImage.sprite = Resources.Load<Sprite>("Captcha/" + codeA);
  16. if (cb != null) cb(codeA);
  17. }
  18. int code = UnityEngine.Random.Range(1000, 10000);
  19. using (UnityWebRequest uwr = new UnityWebRequest(CommonConfig.gateServerURL + "/api/createCaptcha?code=" + code, UnityWebRequest.kHttpVerbGET)) {
  20. uwr.downloadHandler = new DownloadHandlerTexture();
  21. yield return uwr.SendWebRequest();
  22. Texture2D texture = DownloadHandlerTexture.GetContent(uwr);
  23. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  24. targetRenderImage.sprite = sprite;
  25. if (cb != null) cb(code);
  26. }
  27. }
  28. }