Blame view

sys/Hh.Mes.Service/WebService/Base/SysAppService.cs 6.12 KB
赖素文 authored
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
using ApkInfo;
using Google.Protobuf.WellKnownTypes;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
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;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Threading;

namespace Hh.Mes.Service.WebService.Base
{
    /// <summary>
    /// app管理服务
    /// </summary>
    public class SysAppService : RepositorySqlSugar<sys_app>
    {
        private string appId { get; set; }

        #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;

            });
        }

        /// <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;
            });
        }


        /// <summary>
        /// 导入 文件格式:xxx-v1.apk
        /// </summary>
91
        public dynamic Import(string http, int port, IFormFileCollection files,  string contentRootPath)
赖素文 authored
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
        {
            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);
                if (fileName.IndexOf(".apk", StringComparison.Ordinal) == -1)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = "请选择.apk文件的正确格式:xxx-v1.apk";
                    return response;
                }
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(files[0].FileName);// 没有扩展名的文件名 Default
                //var ver = double.Parse(fileNameWithoutExtension.Split("-")[1].Replace("v", ""));
                #endregion

                #region apk 文件上传
117
                var upFloder = Path.Combine(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
                ApkDecoder apkDecoder = new ApkDecoder(filePath, contentRootPath);
赖素文 authored
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
                Thread.Sleep(500);
                #region 数据保存到数据库
                var url =http + "://" + base.GetDictionaryDictValue("UrlApp", "GetUrl") + ":" + port;
                if (apkDecoder.AppVersionCode != null && apkDecoder.PkgName != null)
                {
                    //通过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;
                    }
                    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,
                        appId = apkDecoder.PkgName,
                    };
                    response.Status = base.Insert(model);
                    response.Code = response.Status ? 200 : 500;
                }
                #endregion
                return response;
            });
        }
        #endregion

        private void apkDecoder_InfoParsed(ApkDecoder apkDecoder)
        {
            this.appId = apkDecoder.PkgName;
        }
    }
}