GetTencentAddressInfoService.cs 2.6 KB

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