Blame view

sys/Hh.Mes.Service/WebService/Base/SysAppService.cs 6.15 KB
1
2
3
using ApkInfo;
using Google.Protobuf.WellKnownTypes;
using Hh.Mes.Common.log;
赖素文 authored
4
using Hh.Mes.Common.Request;
5
using Hh.Mes.Pojo.System;
赖素文 authored
6
7
8
9
10
11
12
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
13
14
using System.Data;
using System.Diagnostics;
赖素文 authored
15
using System.IO;
16
17
18
using System.IO.Compression;
using System.Text;
using System.Threading;
赖素文 authored
19
20
21
22
23
24
25
26

namespace Hh.Mes.Service.WebService.Base
{
    /// <summary>
    /// app管理服务
    /// </summary>
    public class SysAppService : RepositorySqlSugar<sys_app>
    {
27
28
        private string appId { get; set; }
赖素文 authored
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
        #region app管理
        /// <summary>
        /// //获取列表
        /// </summary>
        public Response Load(PageReq pageRequest, sys_app model)
        {
            int total = 0;
            var sysdata = Context.Queryable<sys_app>().OrderBy(x => x.id, OrderByType.Desc).ToOffsetPage(pageRequest.page, pageRequest.limit, ref total);
            return new Response()
            {
                Result = sysdata,
                Count = total
            };
        }


        /// <summary>
        /// //获取列表
        /// </summary>
        public string Download()
        {
            var filePath = Context.Queryable<sys_app>().OrderBy(x => x.id, OrderByType.Desc).Select(x => x.filePath).First();
            return filePath != null ? filePath : "";
        }


        /// <summary>
        /// 删除
        /// </summary>
        public dynamic DelByIds(int[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = Context.Deleteable<sys_app>().In(ids).ExecuteCommand() > 0;
                if (!result)
                {
                    response.Message = "删除失败";
                }
                return response;

            });
        }
73
74
75
76
77
78
79
80
81
82
83
84
85
        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(sys_app model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                response.Status = Update(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }
赖素文 authored
86
87
88
89
90


        /// <summary>
        /// 导入 文件格式:xxx-v1.apk
        /// </summary>
91
        public dynamic Import(string http, int port, IFormFileCollection files, IHostingEnvironment environment)
赖素文 authored
92
93
94
95
96
97
98
99
100
101
102
103
104
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                #region before
                if (files.Count != 1)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = "请选择上传一个apk文件";
                    return response;
                }
                var fileName = Path.GetFileName(files[0].FileName);
105
                if (fileName.IndexOf(".apk", StringComparison.Ordinal) == -1)
赖素文 authored
106
107
108
109
110
111
112
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = "请选择.apk文件的正确格式:xxx-v1.apk";
                    return response;
                }
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(files[0].FileName);// 没有扩展名的文件名 Default
HuXiYu authored
113
                //var ver = double.Parse(fileNameWithoutExtension.Split("-")[1].Replace("v", ""));
赖素文 authored
114
115
116
                #endregion

                #region apk 文件上传
HuXiYu authored
117
                var upFloder = Path.Combine(environment.ContentRootPath + "\\wwwroot", uploadFolder+"\\APP");
赖素文 authored
118
119
120
121
122
123
124
125
126
127
128
                if (!Directory.Exists(upFloder)) Directory.CreateDirectory(upFloder);
                var suffix = Path.GetExtension(files[0].FileName);
                var newFileName = Guid.NewGuid().ToString("N") + suffix;
                var filePath = Path.Combine(upFloder, newFileName);
                using (var fs = File.Create(filePath))
                {
                    files[0].CopyTo(fs);
                    fs.Flush();
                }
                #endregion
129
130
                ApkDecoder apkDecoder = new ApkDecoder(filePath, environment.ContentRootPath);
                Thread.Sleep(500);
赖素文 authored
131
                #region 数据保存到数据库
132
133
                var url =http + "://" + base.GetDictionaryDictValue("UrlApp", "GetUrl") + ":" + port;
                if (apkDecoder.AppVersionCode != null && apkDecoder.PkgName != null)
赖素文 authored
134
                {
HuXiYu authored
135
136
137
138
139
140
141
142
143
                    //通过appid标识以及版本号判断是否已存在该版本(不能通过文件名称判断,因为此值用户可随意更改)
                    var isExtFile = Context.Queryable<sys_app>().Any(x => x.appId == apkDecoder.PkgName && x.ver >= Convert.ToDouble(apkDecoder.AppVersionCode));
                    if (isExtFile)
                    {
                        response.Code = 500;
                        response.Status = false;
                        response.Message = $"文件名:{fileName},已存在该版本或者不能小于历史版本号!";
                        return response;
                    }
144
145
146
147
148
149
150
151
152
                    sys_app model = new sys_app
                    {
                        appType = "",
                        filePath = url + "/UploadFile/APP/" + newFileName,
                        ver = Convert.ToInt32(apkDecoder.AppVersionCode),
                        fileName = fileName,
                        keys = Guid.NewGuid(),
                        createBy = sysWebUser?.Account,
                        createTime = DateTime.Now,
HuXiYu authored
153
                        appId = apkDecoder.PkgName,
154
155
156
157
                    };
                    response.Status = base.Insert(model);
                    response.Code = response.Status ? 200 : 500;
                }
赖素文 authored
158
159
160
161
162
                #endregion
                return response;
            });
        }
        #endregion
163
164
165
166
167

        private void apkDecoder_InfoParsed(ApkDecoder apkDecoder)
        {
            this.appId = apkDecoder.PkgName;
        }
赖素文 authored
168
169
    }
}