|
@@ -12,6 +12,7 @@ using System.IO;
|
|
using Library;
|
|
using Library;
|
|
using LitJson;
|
|
using LitJson;
|
|
using MySystem.Models;
|
|
using MySystem.Models;
|
|
|
|
+using System.Net;
|
|
|
|
|
|
namespace MySystem.Areas.Api.Controllers
|
|
namespace MySystem.Areas.Api.Controllers
|
|
{
|
|
{
|
|
@@ -137,8 +138,111 @@ namespace MySystem.Areas.Api.Controllers
|
|
Obj.Add("province", province); //省
|
|
Obj.Add("province", province); //省
|
|
Obj.Add("city", city); //市
|
|
Obj.Add("city", city); //市
|
|
Obj.Add("district", district); //区
|
|
Obj.Add("district", district); //区
|
|
|
|
+ Obj.Add("accessToken", GetToken(user.Id));
|
|
return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
|
|
return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
|
|
}
|
|
}
|
|
|
|
+ public string GetToken(int UserId)
|
|
|
|
+ {
|
|
|
|
+ PlateformModels.WebCMSEntities db = new PlateformModels.WebCMSEntities();
|
|
|
|
+ PlateformModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PlateformModels.Users();
|
|
|
|
+ PlateformModels.UserMoveInfo userMove = db.UserMoveInfo.FirstOrDefault(m => m.UserId == UserId) ?? new PlateformModels.UserMoveInfo();
|
|
|
|
+
|
|
|
|
+ string basic = "kxs_app:MxYh7A9Gkngp5YxWwKkuKlBGUaAIvpTn";
|
|
|
|
+ basic = Convert.ToBase64String(Encoding.UTF8.GetBytes(basic));
|
|
|
|
+ Dictionary<string, string> header = new Dictionary<string, string>();
|
|
|
|
+ header.Add("Authorization", "Basic " + basic);
|
|
|
|
+ string url = "http://gateway.kexiaoshuang.com/v1/kxs/userServer/oauth2/token?scope=server&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer";
|
|
|
|
+ Dictionary<string, string> req = new Dictionary<string, string>();
|
|
|
|
+ req.Add("username", user.Mobile);
|
|
|
|
+ req.Add("password", userMove.LoginPwd);
|
|
|
|
+ string reqJson = Newtonsoft.Json.JsonConvert.SerializeObject(req);
|
|
|
|
+ function.WriteLog("reqJson:" + reqJson, "拦截器日志");
|
|
|
|
+ string jsonData = AesEncrypt(reqJson);
|
|
|
|
+ function.WriteLog(str: "jsonData:" + jsonData, "拦截器日志");
|
|
|
|
+ jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
|
|
|
|
+ string result = PostWebRequest(url, jsonData, header);
|
|
|
|
+ function.WriteLog(str: "result:" + result, "拦截器日志");
|
|
|
|
+ JsonData jsonObj = JsonMapper.ToObject(result);
|
|
|
|
+ if(jsonObj["status"].ToString() == "1")
|
|
|
|
+ {
|
|
|
|
+ return jsonObj["data"]["access_token"].ToString();
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
|
|
|
|
+ {
|
|
|
|
+ string ret = string.Empty;
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ function.WriteLog(DateTime.Now.ToString(), "请求开店宝API日志");
|
|
|
|
+ function.WriteLog(postUrl, "请求开店宝API日志");
|
|
|
|
+ function.WriteLog(paramData, "请求开店宝API日志");
|
|
|
|
+ byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
|
|
|
|
+ // 设置提交的相关参数
|
|
|
|
+ HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
|
|
|
|
+ System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
|
|
|
|
+ request.Method = "POST";
|
|
|
|
+ request.KeepAlive = false;
|
|
|
|
+ request.AllowAutoRedirect = true;
|
|
|
|
+ request.ContentType = "application/json";
|
|
|
|
+ foreach (string key in headers.Keys)
|
|
|
|
+ {
|
|
|
|
+ request.Headers.Add(key, headers[key]);
|
|
|
|
+ }
|
|
|
|
+ request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
|
|
|
|
+
|
|
|
|
+ request.ContentLength = postData.Length;
|
|
|
|
+
|
|
|
|
+ // 提交请求数据
|
|
|
|
+ Stream outputStream = request.GetRequestStream();
|
|
|
|
+ outputStream.Write(postData, 0, postData.Length);
|
|
|
|
+ outputStream.Close();
|
|
|
|
+ HttpWebResponse response;
|
|
|
|
+ Stream responseStream;
|
|
|
|
+ StreamReader reader;
|
|
|
|
+ string srcString;
|
|
|
|
+ response = request.GetResponse() as HttpWebResponse;
|
|
|
|
+ responseStream = response.GetResponseStream();
|
|
|
|
+ reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
|
|
|
|
+ srcString = reader.ReadToEnd();
|
|
|
|
+ ret = srcString; //返回值赋值
|
|
|
|
+ reader.Close();
|
|
|
|
+ function.WriteLog(srcString, "请求开店宝API日志");
|
|
|
|
+ }
|
|
|
|
+ catch (WebException ex)
|
|
|
|
+ {
|
|
|
|
+ HttpWebResponse response = (HttpWebResponse)ex.Response;
|
|
|
|
+ Stream myResponseStream = response.GetResponseStream();
|
|
|
|
+ //获取响应内容
|
|
|
|
+ StreamReader myStreamReader = new StreamReader(myResponseStream);
|
|
|
|
+ ret = myStreamReader.ReadToEnd();
|
|
|
|
+ myResponseStream.Close();
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ ret = "fail";
|
|
|
|
+ function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求开店宝API异常");
|
|
|
|
+ }
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+ public string AesEncrypt(string str)
|
|
|
|
+ {
|
|
|
|
+ if (string.IsNullOrEmpty(str)) return null;
|
|
|
|
+ Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
|
|
|
|
+ string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
|
|
|
|
+ string iv = "DYgjCEIMVrj2W9xN";
|
|
|
|
+
|
|
|
|
+ System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
|
|
|
|
+ {
|
|
|
|
+ Key = Encoding.UTF8.GetBytes(key),
|
|
|
|
+ IV = Encoding.UTF8.GetBytes(iv),
|
|
|
|
+ Mode = System.Security.Cryptography.CipherMode.CBC,
|
|
|
|
+ Padding = System.Security.Cryptography.PaddingMode.PKCS7
|
|
|
|
+ };
|
|
|
|
+ System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
|
|
|
|
+ Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
|
|
|
+ return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
|
|
|
+ }
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|