MVC4.0接口学习

/// <summary>

        /// 正则验证身份证号是否合法

        /// </summary>

        /// <param name="sIdCard"></param>

        /// <returns></returns>

        public bool CheckIDCard(string sIdCard)

        {

            return Regex.IsMatch(sIdCard, @"(^\d{17}(?:\d|x)$)|(^\d{15}$)");

        }

 

 

Response.ContentEncoding = Encoding.GetEncoding("GB2312");

 

项目中有两个网中,其中WebService是接口:

 

 

题目是:

 

接口名称

心跳(附近)

url

http://202.102.90.169:8081/sys.asmx/HeartBeat?im=A000022DAC8C@&ln=xxxx&la=xxxxx&p=江苏省&c=南京市&d=建邺区

参数

参数1: im,设备的imei

参数2: ln,用户当前经度

参数3: la,用户当前纬度

参数4: p, 省(province)

参数5: c,市(city)

参数6: d 区(district)

返回值

{“r”:”0”,”msg”:”成功,无需下发信息”}

{“r”:”1”,”msg”:”成功,有新任务待接收”}

{“r”:”2”,”msg”:”请求参数错误”}

()字符串的json格式

备注

这个题目的目的就是,将这几个参数获取过来,对应的1.更新B_VS_Curpos服务车当前位置表;2.向B_VEH_TRACK服务车轨迹表中添加记录3.从T_TASK_INFO表中获取此imei的车是否有新任务此处同时满足TI_STATUS  N      NUMBER(2)         Y                          "任务状态(1:待下发;2:下发到服务站;3:下发到服务人员;4:拒绝任务;

5:任务开始;6:到达目的地;7:维修开始;8:配件申请;9:配件申请已审核;

10:配件申请已驳回;11:维修完成;12:任务完成;13:强制结束)"及TI_STATUS=3

 

 

 

解题方法:

在WebServiceController中添加Json方法

地址的写法是

http://localhost:3361/WebService/HeartBeat?im=VS_IMEI1&ln=10&la=10&p=jiangsu&c=nanjing&d=jiangning

其中WebService表示的是WebServiceController

HeartBeat表示的是WebServiceController内的JsonResult HeartBeat方法

地址上的im ln la等参数,直接在方法的参数上写HeartBeat(string im, string ln, string la, string p, string c, string d)获取过来

 

 

 

 

如果要求返回的是  这个样子  {"r":"","msg":"","info":[{"n":"li"}]}

说明:info的那部分后面其实是一个数组

          return Json(new

            {

                r = r,

                msg = msg,

                info = new object[] {

                    new { n = "li" }

                }

            }, JsonRequestBehavior.AllowGet);

 

返回{“r”:”0”,”msg”:”成功,无需下发信息”}

去掉数组即可

return Json(new

            {

                r = r,

                msg = msg

            }, JsonRequestBehavior.AllowGet);

 

 

 

/// <summary>

        /// 心跳

        /// </summary>

        /// <param name="im">设备的imei</param>

        /// <param name="ln">用户的当前经度</param>

        /// <param name="la">用户的当前纬度</param>

        /// <param name="p">省(province)</param>

        /// <param name="c">市(city)</param>

        /// <param name="d">区(district)</param>

        /// <returns></returns>

        public JsonResult HeartBeat(string im, string ln, string la, string p, string c, string d)

        {

            int r = 0; string msg = "";

            if (im == "" || ln == "" || la == "" || p == "" || c == "" || d == "" || im == null || ln == null || la == null || p == null || c == null || d == null)

            { r = 2; msg = "请求参数错误"; }

            else

            {

                DbAccessOracle DbAccessor = new DbAccessOracle();

                //如果b_vs_curpos 中服务车当前位置信息 没有此imei值的数据 返回请求参数错误

                string sql =string.Format(@"select count (vs_imei) from b_vs_curpos where vs_imei='{0}'",im);

               int imeicount=Convert.ToInt32(DbAccessor.GetScalar(sql));

               if (imeicount <= 0)

               {

                   r = 2; msg = "请求参数错误";

               }

               else

               {

                   try

                   {

                       //事务处理 更新和插入的功能

                       ArrayList al = new ArrayList();

//更新B_VS_CurPos中服务车当前位置信息

                       string mySql01 = string.Format(@"update b_vs_curpos set vc_long={0},vc_lat={1},vc_long_encrypt={0},vc_lat_encrypt={1},province='{2}',city='{3}',area='{4}' where vs_imei='{5}'", ln, la, p, c, d, im);

//插入B_VEH_TRACK 服务车轨迹表   服务车当前位置信息

 

                       string mySql02 = string.Format(@"insert into b_veh_track(vt_id,vs_imei,vt_long,vt_lat,vt_long_encrypt,vt_lat_encrypt,vt_province,vt_city,vt_area,vt_sysdate) values(to_number(to_char(sysdate,'yyyymmddhhmiss')),'{0}',{1},{2},{1},{2},'{3}','{4}','{5}',sysdate)", im, ln, la, p, c, d);

                       //string mySql03 = string.Format("select count(TI_TASK_ID) from T_Task_Info where VS_IMEI='{1}' and TI_STATUS=3", im);

                       al.Add(mySql01);

                       al.Add(mySql02);

                       //al.Add(mySql03);

                      bool istrue= DbAccessor.ExecuteSql(al);//此处是项目中自己封装的执行多条sql语句的事务方法

                      if (istrue)

                      {

                          // 查询有无新任务

                          string sqlTask = string.Format("select count(TI_TASK_ID) from T_Task_Info where VS_IMEI='{0}' and TI_STATUS=3", im);

                          int taskcount = Convert.ToInt32(DbAccessor.GetScalar(sqlTask));

                          if (taskcount > 0) { r = 1; msg = "成功,有新任务待接收"; }

                          else { r = 0; msg = "成功,无需下发信息"; }

                      }

                      else { r = 2; msg = "请求参数错误"; }

                   }

                   catch { r = 2; msg = "请求参数错误"; }

               }

                ////更新B_VS_CurPos中服务车当前位置信息              

                //BVsCurpos bc = new BVsCurpos();

                //int result = bc.updateVSCurposLocation(im, Convert.ToDouble(ln), Convert.ToDouble(la), p, c, d);

                //if (result <= 0) { r = 2; msg = "请求参数错误"; }

                //else

                //{

                //    //插入B_VEH_TRACK 服务车轨迹表   服务车当前位置信息

                //    string mySql = string.Format(@"insert into b_veh_track(vs_imei,vt_long,vt_lat,vt_long_encrypt,vt_lat_encrypt,vt_province,vt_city,vt_area,vt_sysdate) values('{0}',{1},{2},{1},{2},'{3}','{4}','{5}',{6});", im, ln, la, p, c, d, DateTime.Now);

                //    DbAccessOracle DbAccessor = new DbAccessOracle();

                //    DbAccessor.ExecuteSql(mySql);

 

                //    //查询有无新任务

                //    string sql = string.Format("select count(TI_TASK_ID) from T_Task_Info where VS_IMEI='{1}' and TI_STATUS=3", im);

                //    int taskcount = Convert.ToInt32(DbAccessor.GetScalar(sql));

                //    if (taskcount > 0) { r = 1; msg = "成功,有新任务待接收"; }

                //    else { r = 0; msg = "成功,无需下发信息"; }

                //}

            }

            return Json(new { r = r, msg = msg }, JsonRequestBehavior.AllowGet);

        }

 

 

JsonRequestBehavior.AllowGet

表示允许使用get方式获取地址参数等信息

 

 

 

/// <summary>

        /// 执行多条SQL语句,返回执行是否成功

        /// </summary>

        /// <param name="sqls"></param>

        /// <returns></returns>

        public bool ExecuteSql(ArrayList sqls)

        {

            bool success = true;

            Open();

            OracleCommand cmd = new OracleCommand();

            OracleTransaction trans = oracleConnection.BeginTransaction();

            cmd.Connection = oracleConnection;

            cmd.Transaction = trans;

            try

            {

                foreach (string str in sqls)

                {

                    cmd.CommandText = str;

                    cmd.ExecuteNonQuery();

                }

                trans.Commit();

            }

            catch (Exception ex)

            {

                success = false;

                trans.Rollback();

            }

            finally

            {

                Close();

                Dispose();

            }

            return success;

        }

 

 

程序编好后:

在地址栏输入地址及参数,回车,出现此弹出窗口

 

此内容保存起来:

其返回的内容就是题目所要求的格式内容

 

 

/// <summary>

        /// 用户登录

        /// </summary>

        /// <param name="im"></param>

        /// <param name="un">用户名</param>

        /// <param name="pwd">用户密码</param>

        /// <returns></returns>

        public JsonResult UserLogin(string im, string un, string pwd)

        {

            string r = "", msg = "", realname = "";

            bool istrue = false;

            //判断imei号在手持终端(服务车)表(B_VEH_SERVICE)中是否存在  不存在 设备未注册

            if (ExistIMEI(im))

            {

                //判断用户名和密码是否匹配

                string sql = string.Format("select SS_REALNAME from b_service_staff where ss_name='{0}' and SS_PWD='{1}'", un, pwd);

                //count(ss_name)

                try

                {

                    realname = Convert.ToString(DAO.GetScalar(sql));

                    r = "0"; msg = "登录成功"; istrue = true;

                }

                catch { r = "2"; msg = "登录失败,用户名或密码错误"; }

            }

            else { r = "1"; msg = "该设备未注册"; }

 

            object result = null;

 

            if (istrue)

            {

                result = new

            {

                r = r,

                msg = msg,

                info = new object[] {

                    new { n = realname }

                }

            };

            }

            else

            {

                result = new

            {

                r = r,

                msg = msg

            };

            }

 

            return Json(result, JsonRequestBehavior.AllowGet);

        }

 

以上,通过黄色的部分与以下普通的

return Json(new { r = r, msg = msg }, JsonRequestBehavior.AllowGet);

相比

可以看出返回的Json 都是object  其中{}内部的内容可以直接是普通的字符串,也可以是object数组等等

posted @ 2013-11-18 19:04  JIN__JIN  阅读(361)  评论(0编辑  收藏  举报