app.run.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. using BizCom;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using SiteCore.wechat;
  5. using SQLData;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Drawing;
  11. using System.Drawing.Imaging;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Security.Cryptography;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Web;
  19. using System.Web.SessionState;
  20. using Utils;
  21. using Utils.ImageUtils;
  22. using Utils.Serialization;
  23. namespace SiteCore.Handler
  24. {
  25. public partial class app
  26. {
  27. static int perPoint = 2000;
  28. public void runuser_login()
  29. {
  30. if (UrlPostParmsCheck("code"))
  31. {
  32. string code = GetPostString("code");
  33. //string uname = GetPostString("uname");
  34. //string pwd = GetPostString("pwd");
  35. string nickname = GetPostString("nickname");
  36. string avatarUrl = GetPostString("avatarUrl");
  37. int gender = GetPostInt("gender");
  38. //向微信服务端 使用登录凭证 code 获取 session_key 和 openid
  39. string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mini_Appid_run + "&secret=" + mini_Secret_run + "&js_code=" + code + "&grant_type=" + grant_type;
  40. string type = "utf-8";
  41. string json = GetUrltoHtml(url, type);//获取微信服务器返回字符串
  42. //微信服务器验证成功
  43. JObject jo = (JObject)JsonConvert.DeserializeObject(json);
  44. try
  45. {
  46. string openid = jo["openid"].ToString();
  47. string session_key = jo["session_key"].ToString();
  48. if (openid == "" || session_key == "")
  49. {
  50. conError("无法登录1");
  51. return;
  52. }
  53. //pwd = SecurityHelper.EncryptMD5(pwd);//加密
  54. SRunUser entity = SRunUser.GetByWeixinMiniOpenId(openid);
  55. bool isnew = false;
  56. if (entity == null)
  57. {
  58. entity = new SRunUser();
  59. entity.miniopenid = openid;
  60. entity.openid = openid;
  61. isnew = true;
  62. }
  63. entity.Sex = gender;
  64. entity.NickName = nickname;
  65. if (avatarUrl != "")
  66. {
  67. Thread oThread = new Thread(delegate ()
  68. {
  69. HttpHelper http = new HttpHelper();
  70. HttpItem item = new HttpItem()
  71. {
  72. KeepAlive = true,
  73. Accept = "image/webp,image/*,*/*;q=0.8",
  74. URL = avatarUrl,
  75. ResultType = ResultType.Byte
  76. };
  77. HttpResult hResult = http.GetHtml(item);
  78. using (MemoryStream ms = new MemoryStream(hResult.ResultByte))
  79. {
  80. Bitmap bm = new Bitmap(ms);
  81. //Graphics g = Graphics.FromImage(bm);//实例一个画板的对象,就用上面的图像的画板
  82. //g.DrawImage(bm, 0, 0);
  83. bm.Save(webConfig.runUserPicPath + "\\b\\" + entity.ID + ".jpg", ImageFormat.Jpeg);
  84. using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(ms))
  85. {
  86. ImageMaker.ToThumbnailImages(imgThumb, webConfig.runUserPicPath + "\\" + entity.ID + ".jpg", 100, "", 9, 3);
  87. //result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 600, "", 9, 3);
  88. }
  89. //bm.Save(webConfig.userPicPath + "\\" + entity.ID + ".jpg", ImageFormat.Jpeg);
  90. }
  91. });
  92. oThread.Start();
  93. }
  94. //封装成对象
  95. string session_id = con.Session.SessionID;
  96. if (isnew)
  97. {
  98. entity.AddTime = DateTime.Now;
  99. entity.Create();
  100. entity.UserPic = entity.ID + ".jpg";
  101. entity.Update();
  102. }
  103. else
  104. {
  105. entity.UserPic = entity.ID + ".jpg";
  106. object sid = RedisHelper.StringGet(entity.ID.ToString());
  107. if (sid != null && sid.ToString()!="")
  108. {
  109. RedisHelper.StringDelete(sid.ToString());
  110. }
  111. entity.Update();
  112. }
  113. UserObj uObj = new UserObj()
  114. {
  115. session_key = session_key,
  116. openid = openid,
  117. userid = entity.ID
  118. };
  119. //存入内存中
  120. RedisHelper.StringSet(session_id, JsonConvert.SerializeObject(uObj));
  121. RedisHelper.StringSet(entity.ID.ToString(), session_id);
  122. //返回数据给小程序
  123. StringBuilder userStr = new StringBuilder();
  124. userStr.Append("{");
  125. userStr.AppendFormat("\"session3\":\"{0}\"", session_id);
  126. userStr.AppendFormat(",\"userpic\":\"{0}\"", entity.UserPic);
  127. //userStr.AppendFormat(",\"mobile\":\"{0}\"", entity.Mobile);
  128. userStr.AppendFormat(",\"username\":\"{0}\"", entity.NickName);
  129. userStr.Append("}");
  130. conSuccess("登录成功", userStr.ToString());
  131. return;
  132. }
  133. catch (Exception ex)
  134. {
  135. //微信服务器验证失败
  136. //string msg = jo["errcode"].ToString() + "," + jo["errmsg"].ToString();
  137. conError("暂时无法登录");
  138. }
  139. return;
  140. }
  141. conError("错误的参数");
  142. }
  143. public void runuser_relogin()
  144. {
  145. if (UrlPostParmsCheck("code,iv,data"))
  146. {
  147. string code = GetPostString("code");
  148. string iv = GetPostString("iv");
  149. string data = GetPostString("data");
  150. //向微信服务端 使用登录凭证 code 获取 session_key 和 openid
  151. string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mini_Appid_run + "&secret=" + mini_Secret_run + "&js_code=" + code + "&grant_type=" + grant_type;
  152. string type = "utf-8";
  153. string json = GetUrltoHtml(url, type);//获取微信服务器返回字符串
  154. //微信服务器验证成功
  155. JObject jo = (JObject)JsonConvert.DeserializeObject(json);
  156. try
  157. {
  158. string openid = jo["openid"].ToString();
  159. string session_key = jo["session_key"].ToString();
  160. if (openid == "" || session_key == "")
  161. {
  162. conError("无效CODE!");
  163. return;
  164. }
  165. //pwd = SecurityHelper.EncryptMD5(pwd);//加密
  166. SRunUser entity = SRunUser.GetByWeixinMiniOpenId(openid);
  167. if (entity == null)
  168. {
  169. conLoginError("请先登录");
  170. return;
  171. }
  172. //封装成对象
  173. string session_id = con.Session.SessionID;
  174. object sid = RedisHelper.StringGet(entity.ID.ToString());
  175. if (sid != null)
  176. {
  177. RedisHelper.StringDelete(sid.ToString());
  178. }
  179. UserObj uObj = new UserObj()
  180. {
  181. session_key = session_key,
  182. openid = openid,
  183. userid = entity.ID
  184. };
  185. //存入内存中
  186. RedisHelper.StringSet(session_id, JsonConvert.SerializeObject(uObj));
  187. RedisHelper.StringSet(entity.ID.ToString(), session_id);
  188. StepInfoList sil = null;
  189. try
  190. {
  191. sil = DecryptRun(data, iv, session_key);
  192. }
  193. catch(Exception ex)
  194. {
  195. XLog.SaveLog(0, "不能解密" + ex.Message);
  196. conLoginError("请重新授权");
  197. return;
  198. }
  199. if (sil.stepInfoList.Count > 30)
  200. {
  201. int step = sil.stepInfoList[30].step;
  202. DateTime wxTime = GetTime(sil.stepInfoList[30].timestamp);
  203. DateTime dTime = DateTime.Now;
  204. string sTime = dTime.ToString("yyyy-MM-dd");
  205. StringBuilder tsql = new StringBuilder();
  206. StringBuilder sql = new StringBuilder();
  207. string tsTime = "";
  208. tsql.AppendFormat("select top 1 addtime from s_runstep where userid={0} order by addtime desc", entity.ID);
  209. object result = DbHelper.DbConn.ExecuteScalar(tsql.ToString());
  210. if (result == null || result.ToString() == "")
  211. {
  212. tsql = new StringBuilder();
  213. tsql.AppendFormat("select top 1 addtime from s_runuser where id={0}", entity.ID);
  214. result = DbHelper.DbConn.ExecuteScalar(tsql.ToString());
  215. if (result == null || result.ToString() == "") return;
  216. }
  217. DateTime rTime = Convert.ToDateTime(result);
  218. for (int i = 30; i >= 0; i--)
  219. {
  220. wxTime = GetTime(sil.stepInfoList[i].timestamp);
  221. if (rTime.Subtract(wxTime).TotalDays > 0) break;
  222. tsTime = wxTime.ToString("yyyy-MM-dd");
  223. sql.AppendFormat("if (select count(0) from s_runstep where userid={0} and addtime='{1}' and updatetime='{1}')>0 begin ", entity.ID, tsTime);
  224. sql.AppendFormat(" update s_runstep set step={2} where userid={0} and addtime='{1}' ", entity.ID, tsTime, sil.stepInfoList[i].step);
  225. sql.AppendFormat(" end else begin");
  226. sql.AppendFormat(" insert into s_runstep(userid,addtime,step,updatetime) values({0},'{1}',{2},'{1}') ", entity.ID, tsTime, sil.stepInfoList[i].step, tsTime);
  227. sql.AppendFormat(" end ");
  228. }
  229. if (sql.Length > 0) DbHelper.DbConn.ExecuteNonQuery(sql.ToString());
  230. string usql = "select * from s_runstep where datediff(d,updatetime,'" + sTime + "')<=1 and userid=" + entity.ID + " order by updatetime asc";
  231. DataTable dt = DbHelper.DbConn.ExecuteDataset(usql).Tables[0];
  232. int p = 0;
  233. List<string> lst = new List<string>();
  234. foreach (DataRow dr in dt.Rows)
  235. {
  236. p = getComStep(dr["step"], dr["comstep"]);
  237. if (p > 0) lst.Add(dr["updatetime"] + "_" + getComStep(dr["step"], dr["comstep"]));
  238. }
  239. //conSuccess(step.ToString() + "|" + p);//+ "|" + dTime.ToString("HH:mm")
  240. //返回数据给小程序
  241. StringBuilder userStr = new StringBuilder();
  242. userStr.Append("{");
  243. userStr.AppendFormat("\"session3\":\"{0}\"", session_id);
  244. userStr.AppendFormat(",\"step\":\"{0}\"", step);
  245. if (lst.Count > 0) userStr.AppendFormat(",\"p\":\"{0}\"", string.Join(",", lst.ToArray()));
  246. else userStr.AppendFormat(",\"p\":\"{0}\"", "");
  247. userStr.Append("}");
  248. conSuccess("登录成功", userStr.ToString());
  249. return;
  250. }
  251. }
  252. catch (Exception)
  253. {
  254. //微信服务器验证失败
  255. string msg = jo["errcode"].ToString() + "," + jo["errmsg"].ToString();
  256. conError(msg);
  257. }
  258. return;
  259. }
  260. conError("错误的参数");
  261. }
  262. public void get_userallstep()
  263. {
  264. UserObj uo = GetUserEntity();
  265. if (uo == null) return;
  266. string dTime = DateTime.Now.ToString("yyyy-MM-dd");
  267. StringBuilder sql = new StringBuilder();
  268. //sql.AppendFormat("select step from s_userstep where userid={0} and addtime='{1}'; ", uo.userid, dTime);
  269. sql.AppendFormat("select sum(step) as sumstep from s_runstep where userid={0} and addtime<>'{1}' ;", uo.userid, dTime);
  270. object result = DbHelper.DbConn.ExecuteScalar(sql.ToString());
  271. conSuccess(result.ToString());
  272. }
  273. public void get_userstep_report()
  274. {
  275. UserObj uo = GetUserEntity();
  276. if (uo == null) return;
  277. int dtype = GetPostInt("dtype");
  278. string sql = "select step,addtime as atime from s_runstep where userid="+uo.userid+" and datediff(d,addtime,getdate())<=7 order by addtime asc";
  279. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
  280. conGridJson(dt.Rows.Count, Utils.Serialization.JsonString.DataTable2LowerAjaxJson(dt));
  281. }
  282. public void ins_userrunstep()
  283. {
  284. UserObj uo = GetUserEntity();
  285. if (uo == null) return;
  286. if(UrlPostParmsCheck("iv,data"))
  287. {
  288. string iv = GetPostString("iv");
  289. string data = GetPostString("data");
  290. StepInfoList sil = null;
  291. try
  292. {
  293. sil = DecryptRun(data, iv, uo.session_key);
  294. }
  295. catch
  296. {
  297. conLoginError("请重新授权");
  298. return;
  299. }
  300. if (sil.stepInfoList.Count > 30)
  301. {
  302. int step = sil.stepInfoList[30].step;
  303. DateTime wxTime= GetTime(sil.stepInfoList[30].timestamp);
  304. DateTime dTime = DateTime.Now;
  305. string sTime = dTime.ToString("yyyy-MM-dd");
  306. StringBuilder tsql = new StringBuilder();
  307. StringBuilder sql = new StringBuilder();
  308. string tsTime = "";
  309. tsql.AppendFormat("select top 1 addtime from s_runstep where userid={0} order by addtime desc", uo.userid);
  310. object result = DbHelper.DbConn.ExecuteScalar(tsql.ToString());
  311. if (result == null || result.ToString() == "")
  312. {
  313. tsql = new StringBuilder();
  314. tsql.AppendFormat("select top 1 addtime from s_runuser where id={0}", uo.userid);
  315. result = DbHelper.DbConn.ExecuteScalar(tsql.ToString());
  316. if (result == null || result.ToString() == "") return;
  317. }
  318. //for (int i = 0; i <= 30; i++)
  319. //{
  320. // wxTime = GetTime(sil.stepInfoList[i].timestamp);
  321. // //if (dTime.Subtract(wxTime).TotalDays < 0) break;
  322. // tsTime = wxTime.ToString("yyyy-MM-dd");
  323. // sql.AppendFormat("if (select count(0) from s_runstep where userid={0} and addtime='{1}')>0 begin ", uo.userid, tsTime);
  324. // sql.AppendFormat(" update s_runstep set step={2} where userid={0} and addtime='{1}' ", uo.userid, tsTime, sil.stepInfoList[i].step);
  325. // sql.AppendFormat(" end else begin");
  326. // sql.AppendFormat(" insert into s_runstep(userid,addtime,step,updatetime) values({0},'{1}',{2},'{1}') ", uo.userid, tsTime, sil.stepInfoList[i].step, tsTime);
  327. // sql.AppendFormat(" end ");
  328. //}
  329. DateTime rTime = Convert.ToDateTime(result);
  330. for (int i = 30; i >= 0; i--)
  331. {
  332. wxTime = GetTime(sil.stepInfoList[i].timestamp);
  333. if (rTime.Subtract(wxTime).TotalDays > 0) break;
  334. tsTime = wxTime.ToString("yyyy-MM-dd");
  335. sql.AppendFormat("if (select count(0) from s_runstep where userid={0} and addtime='{1}' and updatetime='{1}')>0 begin ", uo.userid, tsTime);
  336. sql.AppendFormat(" update s_runstep set step={2} where userid={0} and addtime='{1}' ", uo.userid, tsTime, sil.stepInfoList[i].step);
  337. sql.AppendFormat(" end else begin");
  338. sql.AppendFormat(" insert into s_runstep(userid,addtime,step,updatetime) values({0},'{1}',{2},'{1}') ", uo.userid, tsTime, sil.stepInfoList[i].step, tsTime);
  339. sql.AppendFormat(" end ");
  340. }
  341. if (sql.Length > 0) DbHelper.DbConn.ExecuteNonQuery(sql.ToString());
  342. string usql = "select * from s_runstep where datediff(d,updatetime,'" + sTime + "')<=1 and userid="+uo.userid+" order by updatetime asc";
  343. DataTable dt = DbHelper.DbConn.ExecuteDataset(usql).Tables[0];
  344. int p = 0;
  345. List<string> lst = new List<string>();
  346. foreach (DataRow dr in dt.Rows)
  347. {
  348. p = getComStep(dr["step"], dr["comstep"]);
  349. if (p > 0) lst.Add(Convert.ToDateTime(dr["updatetime"]).ToString("yyyy-MM-dd") + "_" + getComStep(dr["step"], dr["comstep"]));
  350. }
  351. if(lst.Count<1) conSuccess(step.ToString() + "|");
  352. else conSuccess(step.ToString() + "|" + string.Join(",", lst.ToArray()));//+ "|" + dTime.ToString("HH:mm")
  353. return;
  354. }
  355. }
  356. conError("同步失败");
  357. }
  358. private int getComStep(object step, object comstep)
  359. {
  360. if (Convert.ToInt32(comstep) >= 20000) return 0;
  361. int s = Convert.ToInt32(step);
  362. if (s > 20000) s = 20000;
  363. int p = (s - Convert.ToInt32(comstep)) / perPoint;
  364. return p;
  365. }
  366. public void get_runuser()
  367. {
  368. UserObj uo = GetUserEntity();
  369. if (uo == null) return;
  370. int uid = uo.userid;
  371. //RedisHelper.StringGet()
  372. DataStruct dStruct = GetPostStruct();
  373. dStruct.PageSize = 3;
  374. List<string> lw = new List<string>();
  375. //lw.Add("state=1");
  376. string order = "";
  377. string key = GetString("key");
  378. if (key.Length > 0) lw.Add(string.Format("title like '%{0}%'", key));
  379. dStruct.Order = "addtime desc";
  380. //lw.Add(" newstypeid=1 ");
  381. dStruct.MainWhere = string.Join(" and ", lw.ToArray());
  382. dStruct.Fileds = "id,title,sectitle,coverimage,addtime as pubtime";
  383. DataTable dt = WebCache.GetData("s_runuser", dStruct);
  384. conGridJson(dStruct.TotalCount, Utils.Serialization.JsonString.DataTable2LowerAjaxJson(dt));
  385. }
  386. public void get_runinfo()
  387. {
  388. UserObj uo = GetUserEntity();
  389. if (uo == null) return;
  390. DataStruct dStruct = GetPostStruct();
  391. string dTime = DateTime.Now.ToString("yyyy-MM-dd");
  392. StringBuilder sql = new StringBuilder();
  393. //sql.AppendFormat("select step from s_userstep where userid={0} and addtime='{1}'; ", uo.userid, dTime);
  394. sql.AppendFormat("select typeid as tid,con from s_runinfo where id in (select MAX(id) from S_RunInfo group by typeID) ;");
  395. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql.ToString()).Tables[0];
  396. conGridJson(dStruct.TotalCount, Utils.Serialization.JsonString.DataTable2LowerAjaxJson(dt));
  397. }
  398. public void get_runinfotype()
  399. {
  400. string sql = "select id,name from s_runinfotype order by sort";
  401. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
  402. conGridJson(dt.Rows.Count, Utils.Serialization.JsonString.DataTable2LowerAjaxJson(dt));
  403. }
  404. public void get_run_rank()
  405. {
  406. //string sql = "select nickname,userpic,sex,SUM(step) as allstep from view_RunStep group by nickname,userpic,sex order by allstep desc";
  407. DataTable dt = WebCache.GetRunRank();
  408. conGridJson(dt.Rows.Count, Utils.Serialization.JsonString.DataTable2LowerAjaxJson(dt));
  409. }
  410. public void get_run_currank()
  411. {
  412. UserObj uo = GetUserEntity();
  413. if (uo == null) return;
  414. //string sql = "select nickname,userpic,sex,SUM(step) as allstep from view_RunStep group by nickname,userpic,sex order by allstep desc";
  415. DataTable dt = WebCache.GetCurRunRank(uo.userid);
  416. conGridJson(dt.Rows.Count, Utils.Serialization.JsonString.DataTable2LowerAjaxJson(dt));
  417. }
  418. public void like_runstep()
  419. {
  420. UserObj uo = GetUserEntity();
  421. if (uo == null) return;
  422. if (UrlPostParmsCheck("sid"))
  423. {
  424. int sid = GetPostInt("sid");
  425. string key = uo.userid + "_" + sid.ToString();
  426. if (RedisHelper.HasKey(key))
  427. {
  428. conError("0");
  429. return;
  430. }
  431. RedisHelper.SetKeyValue(key, 1);
  432. StringBuilder sql = new StringBuilder();
  433. sql.AppendFormat("update S_RunStep set likes=likes+1 where id=" + sid + ";");
  434. sql.AppendFormat("delete from s_runsteplikes where stepid={0} and userid={1} ;", sid, uo.userid);
  435. sql.AppendFormat("insert into s_runsteplikes(stepid,userid,addtime)values({0},{1},getdate()) ;", sid, uo.userid);
  436. DbHelper.DbConn.ExecuteNonQuery(sql.ToString());
  437. WebCache.RemoveRunCache("curRunRankFlag");
  438. conSuccess("已点赞");
  439. return;
  440. }
  441. conError("0");
  442. }
  443. public void get_runuserlike()
  444. {
  445. UserObj uo = GetUserEntity();
  446. if (uo == null) return;
  447. if (UrlPostParmsCheck("sid"))
  448. {
  449. int sid = GetPostInt("sid");
  450. string sql = "select nickname,sex,userpic,addtime from view_runsteplikes where userid=" + uo.userid + " and stepid=" + sid;
  451. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
  452. conGridJson(dt.Rows.Count, Utils.Serialization.JsonString.DataTable2LowerAjaxJson(dt));
  453. return;
  454. }
  455. conError("0");
  456. }
  457. public void get_runstate()
  458. {
  459. conSuccess("0");
  460. }
  461. public void get_runpoint()
  462. {
  463. UserObj uo = GetUserEntity();
  464. if (uo == null) return;
  465. string sql = "select userpoint from s_runuser where id=" + uo.userid;
  466. object up = DbHelper.DbConn.ExecuteScalar(sql);
  467. if (up != null) conSuccess(up.ToString());
  468. else conSuccess("0");
  469. }
  470. public void com_runmain()
  471. {
  472. UserObj uo = GetUserEntity();
  473. if (uo == null) return;
  474. int uid = uo.userid;
  475. if (UrlPostParmsCheck("con"))
  476. {
  477. string con = GetPostString("con");
  478. string[] cArr = con.Split(',');
  479. string[] tArr;
  480. int step;
  481. int com_step;
  482. StringBuilder str = new StringBuilder();
  483. for (int i = 0; i < cArr.Length; i++)
  484. {
  485. if (cArr[i] != "")
  486. {
  487. tArr = cArr[i].Split('_');
  488. string sql = string.Format("select * from s_runstep where userid={0} and datediff(d,updatetime,getdate())<=1 ;", uid);
  489. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
  490. str = new StringBuilder();
  491. foreach (DataRow dr in dt.Rows)
  492. {
  493. if (Convert.ToDateTime(dr["updatetime"]).ToString("yyyy-MM-dd") == tArr[0])
  494. {
  495. step = Convert.ToInt32(dr["step"]);
  496. com_step = Convert.ToInt32(dr["comstep"]);
  497. if (com_step >= 20000) continue;
  498. if ((step - com_step) / perPoint >= Convert.ToInt32(tArr[1]))
  499. {
  500. str.AppendFormat("update S_RunStep set ComStep=ComStep+{0} where UserID={1} and datediff(d,updatetime,'{2}')= 0 ;", Convert.ToInt32(tArr[1]) * perPoint, uid, tArr[0]);
  501. str.AppendFormat("update S_RunUser set UserPoint+={0} where ID={1} ;", tArr[1], uid);
  502. str.AppendFormat("insert into s_runuserpoint(userid, point, summary) values({0}, {1}, '步数积分{1}') ;", uid, tArr[1]);
  503. }
  504. }
  505. }
  506. if(str.Length>0)
  507. {
  508. DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  509. }
  510. }
  511. }
  512. conSuccess("1");
  513. return;
  514. }
  515. conError("错误的参数");
  516. }
  517. #region private
  518. /// <summary>
  519. /// 时间戳转为C#格式时间
  520. /// </summary>
  521. /// <param name="timeStamp">Unix时间戳格式</param>
  522. /// <returns>C#格式时间</returns>
  523. private DateTime GetTime(string timeStamp)
  524. {
  525. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  526. long lTime = long.Parse(timeStamp + "0000000");
  527. TimeSpan toNow = new TimeSpan(lTime);
  528. return dtStart.Add(toNow);
  529. }
  530. /// <summary>
  531. /// 根据微信小程序平台提供的解密算法解密数据
  532. /// </summary>
  533. /// <param name="encryptedData">加密数据</param>
  534. /// <param name="iv">初始向量</param>
  535. /// <param name="sessionKey">从服务端获取的SessionKey</param>
  536. /// <returns></returns>
  537. private StepInfoList DecryptRun(string encryptedData, string iv, string sessionKey)
  538. {
  539. if (sessionKey == "" || sessionKey == null) return null;
  540. StepInfoList userInfo;
  541. //创建解密器生成工具实例
  542. AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
  543. //设置解密器参数
  544. aes.Mode = CipherMode.CBC;
  545. aes.BlockSize = 128;
  546. aes.Padding = PaddingMode.PKCS7;
  547. //格式化待处理字符串
  548. byte[] byte_encryptedData = Convert.FromBase64String(encryptedData);
  549. byte[] byte_iv = Convert.FromBase64String(iv);
  550. byte[] byte_sessionKey = Convert.FromBase64String(sessionKey);
  551. aes.IV = byte_iv;
  552. aes.Key = byte_sessionKey;
  553. //根据设置好的数据生成解密器实例
  554. ICryptoTransform transform = aes.CreateDecryptor();
  555. //解密
  556. byte[] final = transform.TransformFinalBlock(byte_encryptedData, 0, byte_encryptedData.Length);
  557. //生成结果
  558. string result = Encoding.UTF8.GetString(final);
  559. //反序列化结果,生成用户信息实例
  560. userInfo = JsonConvert.DeserializeObject<StepInfoList>(result);
  561. return userInfo;
  562. }
  563. #endregion
  564. }
  565. }