Пример #1
0
        /// <summary>
        /// 调用微信接口获取所有门店信息
        /// </summary>
        /// <param name="context"></param>
        private void getPoiList(System.Web.HttpContext context)
        {
            context.Response.ContentType = "application/json";
            try
            {
                Thread.Sleep(1000);
                DataTable          dtPoiInfo   = WxPoiHelper.GetPoiListInfo();
                List <PoiInfoList> poiInfoList = new List <PoiInfoList>();
                if (dtPoiInfo.Rows.Count == 0)//如果没有同步,则调用微信接口重新获取
                {
                    //获取access_token
                    string token = Access_token.GetAccess_token(Access_token.Access_Type.weixin, true);
                    //门店列表接口提交url
                    string url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" + token;
                    //提交json串,门店列表索引开始:begin,门店列表返回数量限制:limit
                    string json = @"{""begin"":0,""limit"":10}";
                    //调用post提交方法
                    string strPOIList = new Hishop.Weixin.MP.Util.WebUtils().DoPost(url, json);
                    //将传回的json字符串转换为json对象
                    JObject obj3 = JsonConvert.DeserializeObject(strPOIList) as JObject;
                    //将json对象转换为实体类对象
                    poiInfoList = JsonHelper.JsonToList <PoiInfoList>(obj3["business_list"].ToString());

                    //同步微信门店信息
                    if (WxPoiHelper.SyncPoiListInfo(poiInfoList))
                    {
                        dtPoiInfo = WxPoiHelper.GetPoiListInfo();
                    }
                }


                //获取所有门店的坐标
                string offset = string.Empty;
                foreach (DataRow row in dtPoiInfo.Rows)
                {
                    offset += row["longitude"] + "," + row["latitude"] + ";";//增加精度纬度
                }
                offset = offset.TrimEnd(';');
                //将门店坐标放入数组
                string[] offsetList = offset.Split(';');
                /****************根据配送范围将门店的坐标循环匹配用户当前的坐标,误差范围:1公里*******************/
                //允许的误差值(配送范围)
                double range = Convert.ToDouble(context.Request["range"]);
                //获取用户的坐标
                double userLongtitude = Convert.ToDouble(context.Request["userLontitude"]);
                double userLatitude   = Convert.ToDouble(context.Request["userLatitude"]);
                //循环判断获取距离,得到配送范围内的门店poi_id
                List <string> poi_id            = new List <string>();
                List <double> poi_user_distance = new List <double>();
                for (int i = 0; i < offsetList.Length; i++)
                {
                    string[] oa       = offsetList[i].Split(',');//获取门店经度,纬度
                    double   distance = GetDistance(userLatitude, userLongtitude, Convert.ToDouble(oa[1]), Convert.ToDouble(oa[0]));
                    if (distance <= range)
                    {
                        poi_id.Add(dtPoiInfo.Rows[i]["poi_id"].ToString());
                        poi_user_distance.Add(distance);
                    }
                }
                bool   isUserInRange = false;
                string matchIds      = "";
                string matchDistance = "";
                if (poi_id.Count > 0)//如果有配送范围内的用户,则返回第一个匹配到的门店后台id
                {
                    for (int i = 0; i < poi_id.Count; i++)
                    {
                        DataTable dtSender = WxPoiHelper.GetSenderByPoiId(poi_id[i]);
                        foreach (DataRow row in dtSender.Rows)
                        {
                            if (row["clientUserId"].ToString() != "")
                            {
                                matchIds      += row["clientUserId"] + ",";
                                matchDistance += poi_user_distance[i] + ",";
                            }
                        }
                    }
                    isUserInRange = true;
                    //如果匹配到的微信门店还没有绑定至后台账号,给出提示
                    if (matchIds.Length == 0)
                    {
                        context.Response.Write("{\"success\":false,\"errMsg\":\"匹配到了未绑定的门店,或者门店还未通过审核!\"}");
                        return;
                    }
                    //根据门店id匹配到对应的子账号id:sender
                    matchDistance = matchDistance.TrimEnd(',');
                    matchIds      = matchIds.TrimEnd(',');
                    //string[] sender = matchId.Split(',');
                    //string[] clientUserId = matchId.Split(',');

                    /*
                     * //将匹配到的所有门店以门店名字进行展示 (目前更换为街道名)
                     * DataTable dtStoreName = WxPoiHelper.GetStoreName(matchIds);
                     * string storeNameBtns = "";
                     * foreach (DataRow row in dtStoreName.Rows)
                     * {
                     *  storeNameBtns += "<span role='btnStreet' distributorId='" + row["userid"].ToString() + "'>" + row["storeName"].ToString() + "</span>";
                     * }
                     */
                    //将匹配到的所有街道以街道名字进行展示
                    DataTable dtStreetName   = WxPoiHelper.GetStoreStreets(matchIds);
                    string    streetNameBtns = "";
                    foreach (DataRow row in dtStreetName.Rows)
                    {
                        streetNameBtns += "<span role='btnStreet' la='" + row["latitude"] + "' lo='" + row["longitude"] + "'  distributorId='" + row["distributorid"].ToString() + "'>" + row["regionName"].ToString() + "</span>";
                    }


                    context.Response.Write("{\"success\":true,\"isUserInRange\":\"" + isUserInRange + "\",\"distributorId\":\"" + streetNameBtns + "\"}");
                }

                else
                {
                    context.Response.Write("{\"success\":true,\"isUserInRange\":\"" + isUserInRange + "\"}");
                }

                /*
                 * //调试
                 * string[] la0 = offsetList[0].Split(',');
                 * double distance = GetDistance(userLatitude,userLongtitude,Convert.ToDouble(la0[1]),Convert.ToDouble(la0[0]));
                 * context.Response.Write("{\"success\":true,\"userLo\":" + userLongtitude + ",\"userLa\":" + userLatitude + ",\"poiLA\":\"" + offsetList[0] + "\",\"distance\":"+distance+"}");
                 */
            }
            catch (Exception ex)
            {
                context.Response.Write("{\"success\":false,\"errMsg\":\"" + ex.Message + "\"}");
            }
        }