移动系统liao
2025-02-08 8c8404c55a863844cc674a8bedb2cfdb92c1c288
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
124
125
126
127
128
129
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
170
171
172
173
174
using cylsg.Authorization;
using cylsg.Core;
using cylsg.Model.UserModel;
using cylsg.utility.Extend;
using EzWechat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace cylsg.Application
{
    /// <summary>
    /// 登录
    /// </summary>
    [DynamicApiController]
    [ApiDescriptionSettings("Default@1")]
    public class LogoIn
    {
        private readonly IWechatService _wechatService;
 
        private readonly IEzAuthorizationService _ezAuthorizationService;
        public LogoIn(IWechatService wechatService,
            IEzAuthorizationService ezAuthorizationService)
        {
            _wechatService = wechatService;
            _ezAuthorizationService = ezAuthorizationService;
 
        }
        /// <summary>
        /// 快速登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [QueryParameters]
        public async Task<TokenInfo> LogoinFast(string jscode)
        {
 
            var opeid = await _wechatService.GetOpenID(jscode);
            var UserRes = new BaseRepository<User>();
 
 
            var user = await UserRes.GetFirstAsync(x => x.WxOpenId == opeid);
            if (user == null)
            {
                throw Oops.Oh("没找到用户,需要重新注册登录");
            }
            var jwt = new EzJwtModel()
            {
                //ITCode = user.ItCode, 不在明文中使用Itcode 
                NickName = user.Nickname,
                UserID = user.Id,
 
 
            };
 
 
            return   _ezAuthorizationService.CreateToken<EzJwtModel>(jwt);
 
 
 
 
 
        }
 
 
        /// <summary>
        /// 注册用户并登录
        /// </summary>       
        /// <param name="Param">logoinjscode</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TokenInfo> CreateUser(UserInfoIn Param)
        {
 
            var opeid = await _wechatService.GetOpenID(Param.JsCode);
            var UserRes = new BaseRepository<User>();
 
 
            var user = await UserRes.GetFirstAsync(x => x.WxOpenId == opeid);
            if (user == null)
            {
                //没有用户 ,需要新建用户
                var phone = await _wechatService.GetPhone(Param.Bindgetphonenumber);
 
 
                 user = await UserRes.GetFirstAsync(x => x.ItCode == phone);
                if(user == null) { 
                user = new User
                {
 
                    Avatar = phone,
                    Nickname = Param.Nickname??phone.PrivacyStr(),
                    name = Param.Name ?? phone,
                    Phone = phone,
                    ItCode = phone,
                    PassWord = "123456",
                    WxOpenId = opeid,
                    CreateBy="微信注册登录",
                    CreateTime = DateTime.UtcNow,
 
 
                };
 
 
              user=    await UserRes.InsertReturnEntityAsync(user);
                }
                else
                {
                    user.WxOpenId = opeid;
                    await UserRes.EzUpdateAsync(user);
                }
 
 
            }
            var jwt = new EzJwtModel()
            {
               // ITCode = user.ItCode,
                NickName = user.Nickname,
                UserID = user.Id,
 
 
            };
 
 
            return _ezAuthorizationService.CreateToken<EzJwtModel>(jwt);
 
 
 
 
 
 
 
        }
 
      
 
    }
    /// <summary>
    /// 创建用户模型
    /// </summary>
    
      public class  UserInfoIn()
    {
        /// <summary>
        /// 头像
        /// </summary>
        public string? Avatar { get; set; }
        /// <summary>
        /// 昵称
        /// </summary>
        public string? Nickname { get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        public string? Phone { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name { get; set; }
 
        /// <summary>
        /// 手机号换取码
        /// </summary>
        [Required]
        public string Bindgetphonenumber {  get; set; }
        /// <summary>
        /// jscode
        /// </summary>
        [Required]        
        public string JsCode { get; set;}
    }
}