TaiZhouChangChu/Assets/Script/Exam/ReadExamData.cs

272 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
public class ReadExamData : MonoBehaviour
{
public static ReadExamData Instance;
private void Awake()
{
Instance = this;
}
void Start()
{
// ReadLearnTask();
DontDestroyOnLoad(this);
}
//读取考试设置
WWWForm Form;
UnityWebRequest Request_Title, Request_Detail;
string ExamTitle, ExamStr, SubStr, SubStr_One;
int i, a, b, c, d, e, readCount;
public int ReadResult;//0失败 1完成
//项目标题
public List<string> List_TaskTitle;
//项目内容
public List<Task> List_Task;
/// <summary>
/// 读取作业标题
/// </summary>
public void ReadTaskTitle()
{
StartCoroutine(GetTaskTitle());
}
string TaskTitle;
IEnumerator GetTaskTitle()
{
Debug.Log("请求所有课程列表" + PlayerPrefs.GetString("业务流程课程列表"));
Request_Title = UnityWebRequest.Get(PlayerPrefs.GetString("业务流程课程列表"));
yield return Request_Title.SendWebRequest();
if (Request_Title.isHttpError || Request_Title.isNetworkError)
Debug.Log("错误" + Request_Title.error);
else
{
List_TaskTitle = new List<string>();
TaskTitle = Request_Title.downloadHandler.text;
Debug.Log("获取所有课程列表" + TaskTitle);
//解析处理
TaskTitle = TaskTitle.Replace("\"", "");//去除"
TaskTitle = TaskTitle.Replace("\\", "");//去除\
TaskTitle = TaskTitle.Replace("]}", ",");
TaskTitle = TaskTitle.Substring(TaskTitle.IndexOf("data") + 6);//去掉头
while (TaskTitle.Contains(","))
{
a = TaskTitle.IndexOf(",");
SubStr = TaskTitle.Substring(0, a);//一条数据
TaskTitle = TaskTitle.Substring(a + 1);//裁切
//Debug.Log(SubStr);
List_TaskTitle.Add(SubStr);//列表添加
}
}
}
/// <summary>
/// 读取任务详细数据
/// </summary>
public void ReadTaskDetail(string title)
{
TaskTitle = title;
StartCoroutine(PostTaskDetail());
}
IEnumerator PostTaskDetail()//读取详细内容
{
Form = new WWWForm();
Form.AddField("title", TaskTitle);
Request_Detail = UnityWebRequest.Post(PlayerPrefs.GetString("业务流程基础数据"), Form);
yield return Request_Detail.SendWebRequest();
if (Request_Detail.isHttpError || Request_Detail.isNetworkError)
{
Debug.Log("错误" + Request_Detail.error);
ReadResult = -1;
}
else
{
List_Task = new List<Task>();
ExamStr = Request_Detail.downloadHandler.text;
//解析处理
ExamStr = ExamStr.Replace("\\\"", "");//去除\"
ExamStr = ExamStr.Substring(ExamStr.IndexOf("content") + 8);//去掉头
//Debug.Log(ExamStr);
while (ExamStr.Contains("{") && ExamStr.Contains("}"))
{
a = ExamStr.IndexOf("{");
b = ExamStr.IndexOf("}");
//取一条数据{}
SubStr = ExamStr.Substring(a + 1, b - a - 1);
//Debug.Log(SubStr);
//列表
Task tempTask = new Task();
c = SubStr.IndexOf("name:");
d = SubStr.IndexOf("code:");
e = SubStr.IndexOf("value:");
if (d - c > 6)
tempTask.PropName = SubStr.Substring(c + 5, d - 6);//数据1
if (e - d > 6)
tempTask.Code = SubStr.Substring(d + 5, e - d - 6);//数据2
if (e + 6 < SubStr.Length)
tempTask.Value = SubStr.Substring(e + 6);//数据3
//裁切
ExamStr = ExamStr.Substring(b + 1);
readCount++;
//Debug.Log("基础数据" + readCount + "" + tempTask.PropName + "/" + tempTask.Code + "/" + tempTask.Value);
List_Task.Add(tempTask);//列表添加
}
ReadResult = 1;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
ReadLearnTask();
}
}
/// <summary>
/// 读取学习任务
/// </summary>
public void ReadLearnTask()
{
StartCoroutine(GetLearnTask());
}
UnityWebRequest Request_Learn;
public List<string> List_LearnTaskTitle;//学习项目标题
string LearnTitle;
IEnumerator GetLearnTask()
{
Debug.Log("请求获取学习任务" + PlayerPrefs.GetString("获取学习任务") + "?userId=" + PlayerPrefs.GetString("id"));
Request_Learn = UnityWebRequest.Get(PlayerPrefs.GetString("获取学习任务") + "?userId=" + PlayerPrefs.GetString("id"));//----------uid
yield return Request_Learn.SendWebRequest();
if (Request_Learn.isHttpError || Request_Learn.isNetworkError)
Debug.Log("错误" + Request_Learn.error);
else
{
List_LearnTaskTitle = new List<string>();
LearnTitle = Request_Learn.downloadHandler.text;
Debug.Log("获取学习任务" + LearnTitle);
////解析处理
//ExamTitle = ExamTitle.Replace("\"", "");//去除"
//ExamTitle = ExamTitle.Replace("\\", "");//去除\
//ExamTitle = ExamTitle.Replace("]}", ",");
//ExamTitle = ExamTitle.Substring(ExamTitle.IndexOf("data") + 6);//去掉头
//while (ExamTitle.Contains(","))
//{
// a = ExamTitle.IndexOf(",");
// SubStr = ExamTitle.Substring(0, a);//一条数据
// ExamTitle = ExamTitle.Substring(a + 1);//裁切
// Debug.Log(SubStr);
// List_TaskTitle.Add(SubStr);//列表添加
//}
}
}
/// <summary>
/// 读取要进行的考试
/// </summary>
public void ReadExam()
{
Debug.Log("ReadExam");
StartCoroutine(GetExam());
}
UnityWebRequest Request_Exam;
string ExamItem;
public GameObject Panel_NoExam;
IEnumerator GetExam()
{
Debug.Log("查询考试" + PlayerPrefs.GetString("查询考试") + "?userId=" + PlayerPrefs.GetString("id"));
Request_Exam = UnityWebRequest.Get(PlayerPrefs.GetString("查询考试") + "?userId=" + PlayerPrefs.GetString("id"));
yield return Request_Exam.SendWebRequest();
if (Request_Exam.isHttpError || Request_Exam.isNetworkError)
Debug.Log("错误" + Request_Exam.error);
else
{
ExamItem = Request_Exam.downloadHandler.text;
Debug.Log("获取考试" + ExamItem);
//解析处理
ExamItem = ExamItem.Replace("\"", "");//去除"
if (ExamItem.Contains("examType:2"))
{
ExamItem = ExamItem.Substring(0, ExamItem.IndexOf("examType:2"));//去掉后段
ExamItem = ExamItem.Substring(ExamItem.LastIndexOf("id:") + 3);//去掉前段
PlayerPrefs.SetString("ExamId", ExamItem.Substring(0, ExamItem.IndexOf(",")));
ExamItem = ExamItem.Substring(ExamItem.IndexOf("examName:") + 9);
PlayerPrefs.SetString("ExamName", ExamItem.Substring(0, ExamItem.IndexOf(",")));
Debug.Log("考试id" + PlayerPrefs.GetString("ExamId"));
Debug.Log("考试名称:" + PlayerPrefs.GetString("ExamName"));
Panel_NoExam.SetActive(false);
ReadExamItem();
}
else
Panel_NoExam.SetActive(true);
}
}
public void ReadExamItem()
{
StartCoroutine(GetExamItem());
}
UnityWebRequest Request_ExamItem;
public List<string> List_ExamTaskTitle;//考核项目标题
int ExamIndex;
IEnumerator GetExamItem()
{
Debug.Log("请求考试项目" + PlayerPrefs.GetString("加入考试") + "?userId=" + PlayerPrefs.GetString("id") + "&examId=" + PlayerPrefs.GetString("ExamId"));
Request_ExamItem = UnityWebRequest.Get(PlayerPrefs.GetString("加入考试") + "?userId=" + PlayerPrefs.GetString("id") + "&examId=" + PlayerPrefs.GetString("ExamId"));
yield return Request_ExamItem.SendWebRequest();
if (Request_ExamItem.isHttpError || Request_ExamItem.isNetworkError)
Debug.Log("错误" + Request_ExamItem.error);
else
{
List_ExamTaskTitle = new List<string>();
ExamItem = Request_ExamItem.downloadHandler.text;
Debug.Log("获取考试项目" + ExamItem);
//解析处理
ExamItem = ExamItem.Replace("\"", "");//去除"
ResetExamPlayerPrefs();//初始化考试存储
ExamIndex = 1;
while (ExamItem.Contains("deviceName:"))
{
a = ExamItem.IndexOf("deviceName:");
ExamItem = ExamItem.Substring(a + 11);
b = ExamItem.IndexOf(",");
PlayerPrefs.SetString("Exam_" + ExamIndex, ExamItem.Substring(0, b));
Debug.Log("添加考试项目:" + ExamItem.Substring(0, b));
ExamIndex++;
}
if (ExamIndex > 1)//有一项
{
PlayerPrefs.SetString("EnterTime", DateTime.Now.ToString());
SceneManager.LoadScene("Item");
}
}
}
/// <summary>
/// 初始化考试存储
/// </summary>
void ResetExamPlayerPrefs()
{
for (ExamIndex = 1; ExamIndex <= 10; ExamIndex++)
{
PlayerPrefs.SetString("Exam_" + ExamIndex, "");
PlayerPrefs.SetInt("UseTime_" + ExamIndex, 0);
PlayerPrefs.SetInt("Score_" + ExamIndex, 0);
PlayerPrefs.SetInt("Join_" + ExamIndex, 0);
}
}
[Serializable]
/// <summary>
/// 任务详细类
/// </summary>
public class Task
{
public string PropName;//属性名称
public string Code;//属性代码
public string Value;//数值
}
}