zrg
2024-04-29 40d8d68aff0e4ffd4bd4b003d1a719db12b2838b
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web.Http;
using WebAPI.Models;
 
namespace WebAPI.Controllers
{
    public class PlateBindingController : ApiController
    {
        private json objJsonResult = new json();
        public DataSet ds = new DataSet();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
 
        /// <summary>
        /// 保存平板绑定
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [Route("Save/Xt_PlateBinding")]
        [HttpPost]
        public object Save([FromBody] JObject msg)
        {
            try
            {
                DataSet ds;
                var _value = msg["msg"].ToString();
                string msg3 = _value.ToString();
                string[] sArray = msg3.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                string msg1 = sArray[0].ToString();
                string user = sArray[1].ToString();
 
                string filePath = "Configuration";
                bool isAppend = true;
 
                filePath = $@"{filePath}\PlateBinding.txt";
 
                filePath = "D:\\" + filePath;
 
                if (!System.IO.Directory.Exists(Path.GetDirectoryName(filePath)))
                {
                    System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                }
                //判断是否已经存在平板绑定文件,若已存在则删除,重新创建
                bool fileExists = System.IO.File.Exists(filePath);
 
                //存在 则删除该文件
                if (fileExists)
                {
                    File.Delete(filePath);                    
                }
                //创建文件
 
                using (StreamWriter writer = new StreamWriter(filePath, isAppend))
                {
                    //存在的时候才写一行
                    if (fileExists && isAppend)
                    {
                        writer.WriteLine();
                    }
 
                    var content = msg1 is string ? msg1 : JsonConvert.SerializeObject(msg1);
                    writer.WriteLine($"{DateTime.Now}");
                    writer.WriteLine("");
                    msg1 = msg1.Replace("{","").Replace("}","").Replace("\"","");
                    string[] arr = msg1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < arr.Length; i++)
                    {
                        writer.WriteLine(arr[i]);
                    }
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "平板绑定信息写入成功!";
                objJsonResult.data = "";
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "写入平板绑定信息过程中发生异常!";
                objJsonResult.data = "";
                return objJsonResult;
            }
        }
 
        #region 获取平板绑定文件数据
 
        [Route("Get/Xt_PlateBinding")]
        [HttpGet]
        public object Get(string user)
        {
            try
            {
                //根据指定路径,读取文件内容,返回数据为数组格式
                string[] ConfigFileInfo = File.ReadAllLines(@"D:\Configuration\PlateBinding.txt");
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ConfigFileInfo;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
    }
}