GetTencentAddressInfoService.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. using LitJson;
  9. using MySystem.Models.Main;
  10. namespace MySystem
  11. {
  12. /// <summary>
  13. /// 获取腾讯地图地址
  14. /// </summary>
  15. public class GetTencentAddressInfoService
  16. {
  17. public readonly static GetTencentAddressInfoService Instance = new GetTencentAddressInfoService();
  18. private GetTencentAddressInfoService()
  19. { }
  20. public void Start()
  21. {
  22. Thread th = new Thread(GetTencentAddress);
  23. th.IsBackground = true;
  24. th.Start();
  25. }
  26. public void GetTencentAddress()
  27. {
  28. while (true)
  29. {
  30. string data = RedisDbconn.Instance.RPop<string>("GetTencentAddressInfoQueueHd");
  31. if (!string.IsNullOrEmpty(data))
  32. {
  33. try
  34. {
  35. JsonData jsonObj = JsonMapper.ToObject(data);
  36. int Id = int.Parse(jsonObj["MerChantId"].ToString());
  37. string Address = jsonObj["Address"].ToString();
  38. string Key = Library.ConfigurationManager.AppSettings["TencentKey"].ToString();
  39. var info = function.GetWebRequest("https://apis.map.qq.com/ws/geocoder/v1/?address=" + Address + "&key=" + Key);
  40. JsonData result = JsonMapper.ToObject(info);
  41. //创建成功
  42. if (result["status"].ToString() == "0")
  43. {
  44. WebCMSEntities db = new WebCMSEntities();
  45. var merInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  46. if (merInfo.Id > 0)
  47. {
  48. merInfo.Longitude = decimal.Parse(result["result"]["location"]["lng"].ToString());
  49. merInfo.Latitude = decimal.Parse(result["result"]["location"]["lat"].ToString());
  50. db.SaveChanges();
  51. db.Dispose();
  52. }
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取腾讯地图地址线程异常");
  58. }
  59. }
  60. else
  61. {
  62. Thread.Sleep(60000);
  63. }
  64. }
  65. }
  66. }
  67. }