From 704ca53bc07ed339d84a62c66fac0620d252ef18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?old=E6=98=93?= <156663459@qq.com> Date: Mon, 18 Sep 2023 17:35:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=B0=83=E6=95=B4=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Front/CustomerController.cs | 28 +++++++- New_College.Api/New_College.Api.csproj | 3 + New_College.Api/New_College.xml | 2 +- New_College.Api/appsettings.json | 8 +-- New_College.Api/start.sh | 23 +++++++ New_College.Common/Helper/WeixinHelper.cs | 66 ++++++++++++++++++- .../IV_CustomerInfoServices.cs | 4 ++ .../Request/DecryptUserInfoRequest.cs | 8 +++ New_College.Model/WeixinAuthInfo.cs | 42 ++++++++++++ New_College.Services/D_LongIdMapServices.cs | 4 +- .../V_CustomerInfoServices.cs | 18 +++++ 11 files changed, 197 insertions(+), 9 deletions(-) create mode 100644 New_College.Api/start.sh diff --git a/New_College.Api/Controllers/Front/CustomerController.cs b/New_College.Api/Controllers/Front/CustomerController.cs index c189e95..a964c1b 100644 --- a/New_College.Api/Controllers/Front/CustomerController.cs +++ b/New_College.Api/Controllers/Front/CustomerController.cs @@ -1,15 +1,20 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Threading.Tasks; +using Autofac.Core; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using MySqlX.XDevAPI.Common; using New_College.AuthHelper.OverWrite; using New_College.IServices; using New_College.Model; +using New_College.Model.Models; using New_College.Model.Request; using New_College.Model.ViewModels; +using Org.BouncyCastle.Asn1.Ocsp; namespace New_College.Api.Controllers.Front { @@ -32,7 +37,7 @@ namespace New_College.Api.Controllers.Front /// [HttpPost] [AllowAnonymous] - public MessageModel GetWeixinAuthinfo(WeixinLogin login) + public MessageModel GetWxOpenId(WeixinLogin login) { var response = new CustomerInfoResult(); try @@ -82,7 +87,28 @@ namespace New_College.Api.Controllers.Front } + [AllowAnonymous] + [DisplayName("获取微信用户电话号码")] + public async Task> GetWxPhone(DecryptUserPhoneRequest request) + { + var accessToken = _services.access_Token(); + var reqUserPhoneNumber = new WxaBusinessGetUserPhoneNumberRequest() + { + code = request.code, + access_Token = accessToken.access_token, + }; + var repUserPhoneNumber = _services.WxaBusinessGetUserPhoneNumber(reqUserPhoneNumber); + var customerinfo = (await _services.Query(e => e.OpenId == request.openId)).FirstOrDefault(); + customerinfo.Phone = repUserPhoneNumber.phone_info.phoneNumber; + var result = repUserPhoneNumber.errcode == 0 ? await _services.Update(customerinfo) : false; + return new MessageModel() + { + msg = result ? "修改成功" : "保存失败", + response = repUserPhoneNumber.phone_info.phoneNumber, + success = result ? true : false, + }; + } /// diff --git a/New_College.Api/New_College.Api.csproj b/New_College.Api/New_College.Api.csproj index cc53dd2..07755ee 100644 --- a/New_College.Api/New_College.Api.csproj +++ b/New_College.Api/New_College.Api.csproj @@ -86,6 +86,9 @@ Always + + Always + diff --git a/New_College.Api/New_College.xml b/New_College.Api/New_College.xml index c4ad3e9..3e47b9d 100644 --- a/New_College.Api/New_College.xml +++ b/New_College.Api/New_College.xml @@ -108,7 +108,7 @@ - + 获取微信openid diff --git a/New_College.Api/appsettings.json b/New_College.Api/appsettings.json index 0d5b119..bce4af3 100644 --- a/New_College.Api/appsettings.json +++ b/New_College.Api/appsettings.json @@ -1,11 +1,11 @@ { "Weixin": { - "Appid": "wx6d433f70416c37d4", - "Secret": "d7cb43cd2546f04f87c04d6ffd5fa9be", + "Appid": "wx05e75004e657527c", + "Secret": "9383aa5517e1d6bf23404dce9c86b0f1", //MCHID - "MCHID": "1600580140", + "MCHID": "1614987630", //MCHKey - "KEY": "sa6328ahwqiuyoa923zaswq7ed021mns", + "KEY": "brapuk6fon0wachiMlth2t3lb4a0h7ji", //NotifyUrl "NotifyUrl": "http://*:8082/api/WeixinPay/PayNotify" }, diff --git a/New_College.Api/start.sh b/New_College.Api/start.sh new file mode 100644 index 0000000..4b3a9ed --- /dev/null +++ b/New_College.Api/start.sh @@ -0,0 +1,23 @@ +#! /bin/bash + +echo "newcollage-api..." +sleep 3 +docker stop newcollage-api +sleep 2 +docker rm newcollage-api + +docker rmi newcollage-api +echo "" +echo "newcollage-api packing..." +sleep 3 + +docker build -t newcollage-api . +echo "" + +echo "newcollage-api running..." +sleep 3 + +docker run \ + -p 8083:8083 \ + --name newcollage-api \ + -d newcollage-api diff --git a/New_College.Common/Helper/WeixinHelper.cs b/New_College.Common/Helper/WeixinHelper.cs index e33dd8a..ad3da18 100644 --- a/New_College.Common/Helper/WeixinHelper.cs +++ b/New_College.Common/Helper/WeixinHelper.cs @@ -3,7 +3,9 @@ using System.Collections.Generic; using System.Net.Http; using System.Security.Cryptography; using System.Text; +using Essensoft.AspNetCore.Payment.Alipay.Domain; using Newtonsoft.Json; +using NPOI.SS.Formula.Functions; namespace New_College.Common { @@ -16,7 +18,7 @@ namespace New_College.Common /// /// /// - public static T AuthCode2Session(string appid, string secret, string code) where T :new() + public static T AuthCode2Session(string appid, string secret, string code) where T : new() { var authinfo = new T(); try @@ -42,6 +44,68 @@ namespace New_College.Common } + /// + /// 获取AccessToken + /// + /// + /// + /// + /// + public static T GetAccessToken(string appid, string secret) where T : new() + { + + var authinfo = new T(); + try + { + + string requestUri = string.Format("https://api.weixin.qq.com/cgi-bin/token?appid={0}&secret={1}&grant_type=client_credential", appid, secret); + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true + }; + + using (HttpClient httpClient = new HttpClient(httpClientHandler)) + { + var responseStr = httpClient.GetAsync(requestUri).Result.Content.ReadAsStringAsync().Result; + var obj = JsonConvert.DeserializeObject(responseStr); + return obj; + } + } + catch (Exception ex) + { + throw ex; + } + } + + public static T WxaBusinessGetUserPhoneNumber(string access_token, string code) where T : new() + { + var authinfo = new T(); + try + { + string requestUri = string.Format("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={0}", access_token); + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true + }; + + using (HttpClient httpClient = new HttpClient(httpClientHandler)) + { + var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new WxaBusinessModel() { code = code }), Encoding.UTF8, "application/json"); + var responseStr = httpClient.PostAsync(requestUri, content).Result.Content.ReadAsStringAsync().Result; + var obj = JsonConvert.DeserializeObject(responseStr); + return obj; + } + } + catch (Exception ex) + { + throw ex; + } + } + + private class WxaBusinessModel + { + public string code { get; set; } + } /** * * 统一下单 diff --git a/New_College.IServices/IV_CustomerInfoServices.cs b/New_College.IServices/IV_CustomerInfoServices.cs index 4e98412..19d0d02 100644 --- a/New_College.IServices/IV_CustomerInfoServices.cs +++ b/New_College.IServices/IV_CustomerInfoServices.cs @@ -40,6 +40,10 @@ namespace New_College.IServices Weixinauthinfo AuthCode2Session(string Code); + + Access_Token access_Token(); + + WxaBusinessGetUserPhoneNumberResponse WxaBusinessGetUserPhoneNumber(WxaBusinessGetUserPhoneNumberRequest request); string GetWeixinPhone(GetPhoneInfo phoneInfo); Task> Logined(LoginQuery query); diff --git a/New_College.Model/Request/DecryptUserInfoRequest.cs b/New_College.Model/Request/DecryptUserInfoRequest.cs index b7e91b7..dd4f246 100644 --- a/New_College.Model/Request/DecryptUserInfoRequest.cs +++ b/New_College.Model/Request/DecryptUserInfoRequest.cs @@ -22,4 +22,12 @@ namespace New_College.Model.Request public string AreaName { get; set; } } + + + public class DecryptUserPhoneRequest + { + public string openId { get; set; } + public string code { get; set; } + } + } diff --git a/New_College.Model/WeixinAuthInfo.cs b/New_College.Model/WeixinAuthInfo.cs index 5142ea9..520a7a5 100644 --- a/New_College.Model/WeixinAuthInfo.cs +++ b/New_College.Model/WeixinAuthInfo.cs @@ -14,6 +14,48 @@ namespace New_College.Model } + + public class Access_Token + { + public string access_token { get; set; } + + public int expires_in { get; set; } + + } + + + public class WxaBusinessGetUserPhoneNumberResponse + { + public int errcode { get; set; } + public string errmsg { get; set; } + public Phone_Info phone_info { get; set; } + } + + public class WxaBusinessGetUserPhoneNumberRequest + { + public string access_Token { get; set; } + + public string code { get; set; } + + } + + + + public class Phone_Info + { + public string phoneNumber { get; set; } + public string purePhoneNumber { get; set; } + public int countryCode { get; set; } + public Watermark watermark { get; set; } + } + + public class Watermark + { + public int timestamp { get; set; } + public string appid { get; set; } + } + + public class GetPhoneInfo { public string encryptedData { get; set; } diff --git a/New_College.Services/D_LongIdMapServices.cs b/New_College.Services/D_LongIdMapServices.cs index ad327ef..aeb5e9f 100644 --- a/New_College.Services/D_LongIdMapServices.cs +++ b/New_College.Services/D_LongIdMapServices.cs @@ -168,7 +168,7 @@ namespace New_College.Services { var majorcategories = (await d_MajorCategoryRepository.Query(x => x.Type == query.SchoolLevel && x.IsDelete == false)).Select(x => new uniMajorSelect() { Id = x.Id, Name = x.Name }); ; var majorclasses = (await d_MajorClassRepository.Query(x => x.IsDelete == false)).Select(x => new uniMajorClassSelect() { Id = x.Id, Name = x.Name, TradeId = x.TradeId }); - var majorinfo = (await d_MajorRepository.Query(x => x.IsDelete == false)).Select(s => new uniMajorSelect() { Id = s.CategoryClass_Id, Name = s.Name }).ToList(); + var majorinfo = (await d_MajorRepository.Query(x => x.IsDelete == false&&x.Type!=3)).Select(s => new uniMajorSelect() { Id = s.CategoryClass_Id, Name = s.Name }).ToList(); List list = new List() { }; foreach (var item in majorcategories) { @@ -565,7 +565,7 @@ namespace New_College.Services { var majorclasses = (await d_MajorClassRepository.Query(x => x.IsDelete == false)).Select(x => new uniMajorClassSelect() { Id = x.Id, Name = x.Name, TradeId = x.TradeId }); var nowmajorname = majorclasses.Where(x => x.Id == query.categoryclassid).Select(x => x.Name)?.FirstOrDefault(); - var newmajorinfo = await d_MajorRepository.Query(x => x.IsDelete == false && x.CategoryClass_Id == query.categoryclassid); + var newmajorinfo = await d_MajorRepository.Query(x => x.IsDelete == false && x.CategoryClass_Id == query.categoryclassid && x.Type != 3); info = newmajorinfo.Where(x => x.Name != nowmajorname).Select(x => new uniMajorInfo() { Id = x.Id, Name = x.MajorName }).ToList(); } return new MessageModel>() diff --git a/New_College.Services/V_CustomerInfoServices.cs b/New_College.Services/V_CustomerInfoServices.cs index 3ea2ef7..cadcbcf 100644 --- a/New_College.Services/V_CustomerInfoServices.cs +++ b/New_College.Services/V_CustomerInfoServices.cs @@ -295,6 +295,24 @@ namespace New_College.Services } + /// + /// 获取accesstoken + /// + /// + public Access_Token access_Token() + { + return WeixinHelper.GetAccessToken(WeixinConfig.Appid, WeixinConfig.Secret); + } + + /// + /// + /// + /// + /// + public WxaBusinessGetUserPhoneNumberResponse WxaBusinessGetUserPhoneNumber(WxaBusinessGetUserPhoneNumberRequest request) + { + return WeixinHelper.WxaBusinessGetUserPhoneNumber(request.access_Token, request.code); + } public string GetWeixinPhone(GetPhoneInfo phoneInfo) { string phone = string.Empty;