Unity Http通信+MVC

2023-01-05  本文已影响0人  玄策丶

1.M层

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModelCloth : SingletonMono<ModelCloth>
{
    public ClothMsg data = new ClothMsg(); //布料查询数据
}
public class ClothMsg
{
    public List<ClothOne> listCloth = new List<ClothOne>(); //单个面料所有信息
}
public class ClothOne
{
    public string name;
    public string value;
}

2.C层
(1)接收数据

using LitJson;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class HttpGetMsgController_MLK : MonoBehaviour
{
    string strUrl_Cloth = "WebService.asmx/getmaterialbyid";
    private void Awake()
    {
        Instance = this;
    }
    /// <summary>
    /// 布匹信息接收,面料查询
    /// </summary>
    /// <returns></returns>
    public IEnumerator PostCloth(string id)
    {
        string url = AppConfig.HttpURL + strUrl_Cloth;

        JsonData data = new JsonData();
        data["key"] = AppConfig.Key;
        data["id"] = id;
        byte[] postBytes = System.Text.Encoding.Default.GetBytes(data.ToJson());

        UnityWebRequest request = new UnityWebRequest(url, "POST");

        request.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes);
        request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
        {
            Debug.LogError(request.error);
        }
        else
        {
            //Debug.Log(request.downloadHandler.text);
            HttpGetMsgHandle_MLK.Instance.AllClothHandle(request.downloadHandler.text);
        }
    }
}

(2)处理数据

using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class HttpGetMsgHandle_MLK : SingletonMono<HttpGetMsgHandle_MLK>
{
    /// <summary>
    /// 布匹信息处理
    /// </summary>
    /// <param name="str"></param>
    public void AllClothHandle(string str)
    {
        try
        {
            JsonData jsonData = JsonMapper.ToObject(str);
            if (jsonData.Count < 1)
            {
                return;
            }

            ModelCloth.Instance.data.listCloth.Clear();
            for (int i = 0; i < jsonData["data"].Count; i++)
            {
                ClothOne clothOne = new ClothOne();
                clothOne.name = EcpMsg(jsonData["data"][i]["name"]);
                clothOne.value = EcpMsg(jsonData["data"][i]["value"]);

                ModelCloth.Instance.data.listCloth.Add(clothOne);
            }
            //view显示 数据
            ViewCloth.Instance.SetClothMsg(ModelCloth.Instance.data);
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex);
        }
    }


    /// <summary>
    /// 将string转换为int
    /// </summary>
    /// <param name="field"></param>
    /// <returns></returns>
    int TransformationToInt(string field)
    {
        int tempNum = 0;
        try
        {
            tempNum = int.Parse(field);
        }
        catch (Exception)
        {

        }
        return tempNum;
    }
    /// <summary>
    /// 将string转换为float
    /// </summary>
    /// <param name="field"></param>
    /// <returns></returns>
    float TransformationToFloat(string field)
    {
        float tempNum = 0f;
        try
        {
            tempNum = float.Parse(field);
        }
        catch (Exception)
        {

        }
        return tempNum;
    }
    /// <summary>
    /// 将json数据转换为string
    /// </summary>
    /// <param name="jsonData"></param>
    /// <returns></returns>
    string EcpMsg(JsonData jsonData)
    {
        try
        {
            if (jsonData == null)
            {
                return "";
            }
            else
            {
                return jsonData.ToString();
            }
        }
        catch (Exception)
        {
            return "";
        }
    }

    //判断是否含有某字段
    public bool CheckParam(JsonData Json, string KeyName)
    {
        bool res = false;
        //判断是否有key
        if (((IDictionary)Json).Contains(KeyName))
        {
            //string valuestr = (string)Json[KeyName];
            res = true;
        }

        return res;
    }
}

3.V层

/***********************
* 遇见你的眉眼,如清风明月。
* Title:        
* Date:         2021
* Author:       玄策
* UnityVersion: 2019.2.4f1
* Func:           
* -   
***********************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ViewCloth : SingletonMono<ViewCloth>
{
    Transform Image_Cloth;

    public string str_ClothName; //面料按名字查询

    Text TextName;      //面料名称
    Text TextLocation;  //库位
    Text TextGH;        //缸号
    Text TextID;        //面料号
    Text TextUnit;      //单位
    Text TextMS;        //米数
    Text TextTXM;       //条形码
    Text TextDate;      //入库日期
    Text TextGYS;       //供应商
    Text TextCount;     //数量
    Text TextDWXS;      //单位系数
    Text TextFee;       //合计

    Button Btn_Search; //在库查询按钮

    private new void Awake()
    {
        Image_Cloth = GameObject.Find("Canvas").transform.Find("Image_Cloth");
        TextName = Image_Cloth.Find("TextName").GetComponent<Text>();
        TextLocation = Image_Cloth.Find("TextLocation").GetComponent<Text>();
        TextGH = Image_Cloth.Find("TextGH").GetComponent<Text>();
        TextID = Image_Cloth.Find("TextID").GetComponent<Text>();
        TextUnit = Image_Cloth.Find("TextUnit").GetComponent<Text>();
        TextMS = Image_Cloth.Find("TextMS").GetComponent<Text>();
        TextTXM = Image_Cloth.Find("TextTXM").GetComponent<Text>();
        TextDate = Image_Cloth.Find("TextDate").GetComponent<Text>();
        TextGYS = Image_Cloth.Find("TextGYS").GetComponent<Text>();
        TextCount = Image_Cloth.Find("TextCount").GetComponent<Text>();
        TextDWXS = Image_Cloth.Find("TextDWXS").GetComponent<Text>();
        TextFee = Image_Cloth.Find("TextFee").GetComponent<Text>();

        Btn_Search = Image_Cloth.Find("Button_Search").GetComponent<Button>();

    }
    void Start()
    {
        
        //在库查询按钮点击
        Btn_Search.onClick.AddListener(() =>
        {
            Image_Cloth.gameObject.SetActive(false);

            //调用网页端页面
            UIManager.Instance.browser.CallFunction("mlZkcx ", str_ClothName).Done();
            UIManager.Instance.ShowSearchMsg();
        });
    }

    public void SetClothMsg(ClothMsg msg)
    {
        TextName.text = msg.listCloth[0].name + ":";
        TextName.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[0].value;

        TextLocation.text = msg.listCloth[2].name + ":";
        TextLocation.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[2].value;

        TextGH.text = msg.listCloth[4].name + ":";
        TextGH.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[4].value;

        TextID.text = msg.listCloth[6].name + " : ";
        TextID.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[6].value;

        TextUnit.text = msg.listCloth[8].name + " : ";
        TextUnit.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[8].value;

        TextMS.text = msg.listCloth[10].name + " : ";
        TextMS.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[10].value;

        TextTXM.text = msg.listCloth[1].name + " : ";
        TextTXM.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[1].value;

        TextDate.text = msg.listCloth[3].name + " : ";
        TextDate.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[3].value;

        TextGYS.text = msg.listCloth[5].name + " : ";
        TextGYS.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[5].value;

        TextCount.text = msg.listCloth[7].name + " : ";
        TextCount.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[7].value;

        TextDWXS.text = msg.listCloth[9].name + " : ";
        TextDWXS.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[9].value;

        TextFee.text = msg.listCloth[11].name + " : ";
        TextFee.transform.Find("Text").GetComponent<Text>().text = msg.listCloth[11].value;


        Image_Cloth.gameObject.SetActive(true);
        Image_Cloth.position = Input.mousePosition;
    }
}

4.Click

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HighlightPlus;
using UnityEngine.EventSystems;

public class ClothClick : MonoBehaviour
{
    public string cloth_ID; //布料id
    public string cloth_Name; //布料名字
    public string Cloth_ID { get => cloth_ID; set => cloth_ID = value; }
    public string Cloth_Name { get => cloth_Name; set => cloth_Name = value; }

    private void OnMouseUpAsButton()
    {
        if (ZheDang.Instance.CheckGuiRaycastObjects())
        {
            return;
        }
        if (EventSystem.current.IsPointerOverGameObject() == true)
        {
            return;
        }
        if (!LeftCamsChange.Instance.isMouse)
        {
            return;
        }
        if (AppConfig.ReadSelfMsg)
        {
            //测试
            ClothOne clothOne = new ClothOne();
            clothOne.name = "面料名称";
            clothOne.value = "YB-2342单面布";
            ClothOne clothOne1 = new ClothOne();
            clothOne1.name = "条形码";
            clothOne1.value = "200293";
            ClothOne clothOne2 = new ClothOne();
            clothOne2.name = "库   位";
            clothOne2.value = "A1-2";
            ClothOne clothOne3 = new ClothOne();
            clothOne3.name = "入库时间";
            clothOne3.value = "2021-01-14";
            ClothOne clothOne4 = new ClothOne();
            clothOne4.name = "缸   号";
            clothOne4.value = "Y191217017";
            ClothOne clothOne5 = new ClothOne();
            clothOne5.name = "供应商";
            clothOne5.value = "烟台业林";
            ClothOne clothOne6 = new ClothOne();
            clothOne6.name = "面料号";
            clothOne6.value = "YL005267ZGF-YH4#";
            ClothOne clothOne7 = new ClothOne();
            clothOne7.name = "数   量";
            clothOne7.value = "42.7000";
            ClothOne clothOne8 = new ClothOne();
            clothOne8.name = "单   位";
            clothOne8.value = "米";
            ClothOne clothOne9 = new ClothOne();
            clothOne9.name = "系   数";
            clothOne9.value = "1.0";
            ClothOne clothOne10 = new ClothOne();
            clothOne10.name = "米   数";
            clothOne10.value = "42.7";
            ClothOne clothOne11 = new ClothOne();
            clothOne11.name = "合   计";
            clothOne11.value = "42.7";

            ModelCloth.Instance.data.listCloth.Add(clothOne);
            ModelCloth.Instance.data.listCloth.Add(clothOne1);
            ModelCloth.Instance.data.listCloth.Add(clothOne2);
            ModelCloth.Instance.data.listCloth.Add(clothOne3);
            ModelCloth.Instance.data.listCloth.Add(clothOne4);
            ModelCloth.Instance.data.listCloth.Add(clothOne5);
            ModelCloth.Instance.data.listCloth.Add(clothOne6);
            ModelCloth.Instance.data.listCloth.Add(clothOne7);
            ModelCloth.Instance.data.listCloth.Add(clothOne8);
            ModelCloth.Instance.data.listCloth.Add(clothOne9);
            ModelCloth.Instance.data.listCloth.Add(clothOne10);
            ModelCloth.Instance.data.listCloth.Add(clothOne11);

            ViewCloth.Instance.SetClothMsg(ModelCloth.Instance.data);
        }
        else
        {
            //正式
            if (Cloth_Name.Equals(""))
            {
                ViewCloth.Instance.str_ClothName = "";
            }
            else
            {
                ViewCloth.Instance.str_ClothName = Cloth_Name;
            }
            
            StartCoroutine(HttpGetMsgController_MLK.Instance.PostCloth(Cloth_ID));
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读