美文网首页
iOS网络层业务层-dianping api 业务层封装

iOS网络层业务层-dianping api 业务层封装

作者: solozyx | 来源:发表于2016-08-26 14:50 被阅读370次

    1.面向通用网络工具类开发

    1-面向ZYXAPITool开发.png

    参数是字典对象
    目前的请求参数是面向字典在开发
    字典的key容易写错

    2016-08-26 14:50:39.780 大众点评API封装[78066:2524177] success jsonObj = {
        count = 1;
        deals =     (
                    {
                businesses =             (
                                    {
                        city = "\U5317\U4eac";
                        "h5_url" = "http://m.dianping.com/shop/22371617?utm_source=open";
                        id = 22371617;
                        latitude = "39.92104";
                        longitude = "116.43245";
                        name = "\U4e1c\U701b\U7f8e\U7a7a\U95f4(\U671d\U9633\U95e8\U94f6\U6cb3SOHO\U5e97)";
                        url = "http://www.dianping.com/shop/22371617?utm_source=open";
                    },
                                    {
                        city = "\U5317\U4eac";
                        "h5_url" = "http://m.dianping.com/shop/22372576?utm_source=open";
                        id = 22372576;
                        latitude = "39.907345";
                        longitude = "116.47703";
                        name = "\U4e1c\U701b\U7f8e\U7a7a\U95f4(\U73b0\U4ee3\U57ceSOHO\U5e97)";
                        url = "http://www.dianping.com/shop/22372576?utm_source=open";
                    },
                                    {
                        city = "\U5317\U4eac";
                        "h5_url" = "http://m.dianping.com/shop/22372588?utm_source=open";
                        id = 22372588;
                        latitude = "39.93286";
                        longitude = "116.45271";
                        name = "\U4e1c\U701b\U7f8e\U7a7a\U95f4(\U4e09\U91cc\U5c6fSOHO\U5e97)";
                        url = "http://www.dianping.com/shop/22372588?utm_source=open";
                    },
                                    {
                        city = "\U5317\U4eac";
                        "h5_url" = "http://m.dianping.com/shop/22393040?utm_source=open";
                        id = 22393040;
                        latitude = "39.90619";
                        longitude = "116.45975";
                        name = "\U4e1c\U701b\U7f8e\U7a7a\U95f4(\U56fd\U8d38\U5efa\U5916SOHO\U5e97)";
                        url = "http://www.dianping.com/shop/22393040?utm_source=open";
                    },
                                    {
                        city = "\U5317\U4eac";
                        "h5_url" = "http://m.dianping.com/shop/22404783?utm_source=open";
                        id = 22404783;
                        latitude = "39.91882";
                        longitude = "116.45226";
                        name = "\U4e1c\U701b\U7f8e\U7a7a\U95f4(\U671d\U5916\U5c1a\U90fdSOHO\U5e97)";
                        url = "http://www.dianping.com/shop/22404783?utm_source=open";
                    },
                                    {
                        city = "\U5317\U4eac";
                        "h5_url" = "http://m.dianping.com/shop/32704822?utm_source=open";
                        id = 32704822;
                        latitude = "39.89302";
                        longitude = "116.45894";
                        name = "\U4e1c\U701b\U7f8e\U7a7a\U95f4(\U5bcc\U529b\U57ce\U5e97)";
                        url = "http://www.dianping.com/shop/32704822?utm_source=open";
                    },
                                    {
                        city = "\U5317\U4eac";
                        "h5_url" = "http://m.dianping.com/shop/47195699?utm_source=open";
                        id = 47195699;
                        latitude = "39.95375";
                        longitude = "116.3231";
                        name = "\U4e1c\U701b\U7f8e\U7a7a\U95f4(\U9b4f\U516c\U6751\U5e97)";
                        url = "http://www.dianping.com/shop/47195699?utm_source=open";
                    }
                );
                categories =             (
                    "\U7f8e\U53d1"
                );
                city = "\U5317\U4eac";
                "commission_ratio" = 0;
                "current_price" = 68;
                "deal_h5_url" = "http://m.dianping.com/tuan/deal/11892406?utm_source=open";
                "deal_id" = "2-11892406";
                "deal_url" = "http://t.dianping.com/deal/11892406?utm_source=open";
                description = "\U4e1c\U701b\U7f8e\U7a7a\U95f4 \U5355\U4eba\U8d44\U6df1\U8bbe\U8ba1\U5e08\U6d17\U526a\U5439 \U4ec5\U552e68\U5143\Uff0c\U4ef7\U503c98\U5143\U5355\U4eba\U8d44\U6df1\U8bbe\U8ba1\U5e08\U6d17\U526a\U5439\Uff0c\U957f\U77ed\U53d1\U4e0d\U9650\Uff01\U4e0a\U6d77\U77e5\U540d\U7f8e\U53d1\U8fde\U9501\U54c1\U724c\Uff0c\U7cbe\U6e5b\U6280\U827a \U8d85\U503c\U670d\U52a1\U7b49\U4f60\U6765\U4f53\U9a8c\Uff01";
                distance = "-1";
                "image_url" = "http://p1.meituan.net/dpdeal/0acdf9e1dc8d4aafe9d3a7e8e164ecf0330987.jpg%40640w_400h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D5%26y%3D5%26relative%3D1";
                "list_price" = 98;
                "publish_date" = "2015-05-12";
                "purchase_count" = 42135;
                "purchase_deadline" = "2016-12-31";
                regions =             (
                    "\U671d\U9633\U533a",
                    "\U4e1c\U57ce\U533a",
                    "\U6d77\U6dc0\U533a"
                );
                "s_image_url" = "http://p1.meituan.net/dpdeal/0acdf9e1dc8d4aafe9d3a7e8e164ecf0330987.jpg%40160w_100h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D5%26y%3D5%26relative%3D1";
                title = "\U4e1c\U701b\U7f8e\U7a7a\U95f4";
            }
        );
        status = OK;
        "total_count" = 2663;
    }
    

    返回值是JSON对象

    2.封装搜索团购业务类 获取单个团购信息业务类

    搜索团购API业务类封装
    ZYXFindDealsParam.h

    #import <Foundation/Foundation.h>
    
    @interface ZYXFindDealsParam : NSObject
    /** 
     string 包含团购信息的城市名称 
     */
    @property (copy, nonatomic) NSString *city;
    /** 
     指定目的地城市名称,适用于“酒店”、“旅游”等分类 
     */
    @property (copy, nonatomic) NSString *destination_city;
    /** 
     纬度坐标,须与经度坐标同时传入 
     */
    @property (strong, nonatomic) NSNumber *latitude;
    /** 
     经度坐标,须与纬度坐标同时传入 
     */
    @property (strong, nonatomic) NSNumber *longitude;
    /** 
     搜索半径,单位为米,最小值1,最大值5000,如不传入默认为1000 
     */
    @property (strong, nonatomic) NSNumber *radius;
    /** 
     包含团购信息的城市区域名(不含返回结果中包括的城市名称信息) 
     */
    @property (copy, nonatomic) NSString *region;
    /** 
     包含团购信息的分类名,支持多个category合并查询,多个category用逗号分割 
     */
    @property (copy, nonatomic) NSString *category;
    /** 
     关键词,搜索范围包括商户名、商品名、地址等 
     */
    @property (copy, nonatomic) NSString *keyword;
    /** 
     结果排序,1:默认,2:价格低优先,3:价格高优先,4:购买人数多优先,
     5:最新发布优先,6:即将结束优先,7:离经纬度坐标距离近优先 
     */
    @property (strong, nonatomic) NSNumber *sort;
    /** 
     每页返回的团单结果条目数上限,最小值1,最大值40,如不传入默认为20 
     */
    @property (strong, nonatomic) NSNumber *limit;
    /** 
     页码,如不传入默认为1,即第一页 
     */
    @property (strong, nonatomic) NSNumber *page;
    @end
    

    ZYXFindDealsParam.m

    #import "ZYXFindDealsParam.h"
    @implementation ZYXFindDealsParam
    @end
    

    ZYXBusiness.h

    #import <Foundation/Foundation.h>
    
    @interface ZYXBusiness : NSObject //商家
    /** 
     商户名 
     */
    @property (copy, nonatomic) NSString *name;
    /** 
     商户ID
     */
    @property (copy, nonatomic) NSString *ID;
    /** 
     商户城市 
     */
    @property (copy, nonatomic) NSString *city;
    
    /** 
     纬度 
     */
    @property (assign, nonatomic) float latitude;
    /** 
     经度 
     */
    @property (assign, nonatomic) float longitude;
    
    /** 
     商户页面链接,适用于网页应用 
     */
    @property (copy, nonatomic) NSString *url;
    /** 
     商户HTML5页面链接,适用于移动应用和联网车载应用 
     */
    @property (copy, nonatomic) NSString *h5_url;
    /** 
     商户地址 
     */
    @property (copy, nonatomic) NSString *address;
    
    @end
    

    ZYXBusiness.m

    #import "ZYXBusiness.h"
    
    @implementation ZYXBusiness
    
    - (NSDictionary *)replacedKeyFromPropertyName
    {
        // 模型的ID属性对应着jsonObj字典对象的id键名
        // (服务端返回的字段是id)
        /*
         businesses =             (
         {
         city = "\U5317\U4eac";
         "h5_url" = "http://m.dianping.com/shop/22371617?utm_source=open";
         id = 22371617;
         latitude = "39.92104";
         longitude = "116.43245";
         name = "\U4e1c\U701b\U7f8e\U7a7a\U95f4(\U671d\U9633\U95e8\U94f6\U6cb3SOHO\U5e97)";
         url = "http://www.dianping.com/shop/22371617?utm_source=open";
         },
        */
        
        return @{@"ID" : @"id"};
    }
    
    @end
    

    ZYXDeal.h

    #import <Foundation/Foundation.h>
    
    @interface ZYXDeal : NSObject // 代表一个团购信息
    /** 
     团购单ID 
     */
    @property (copy, nonatomic) NSString *deal_id;
    /** 
     团购标题 
     */
    @property (copy, nonatomic) NSString *title;
    /** 
     团购描述 
     */
    @property (copy, nonatomic) NSString *desc;
    
    /** 
     城市名称,city为"全国"表示全国单,其他为本地单 
     */
    @property (copy, nonatomic) NSString *city;
    
    /** 
     团购包含商品原价值 
     */
    @property (assign, nonatomic) float list_price;
    /** 
     团购价格 
     */
    @property (assign, nonatomic) float current_price;
    
    /**  
     团购适用商户所在行政区
     */
    @property (strong, nonatomic) NSArray *regions;
    /**  
     团购所属分类
     */
    @property (strong, nonatomic) NSArray *categories;
    
    /** 
     团购当前已购买数 
     */
    @property (assign, nonatomic) float purchase_count;
    
    /** 
     团购发布上线日期 
     */
    @property (copy, nonatomic) NSString *publish_date;
    /** 
     团购单的截止购买日期 
     */
    @property (copy, nonatomic) NSString *purchase_deadline;
    
    /** 
     团购图片链接,最大图片尺寸450×280 
     */
    @property (copy, nonatomic) NSString *image_url;
    @property (copy, nonatomic) NSString *more_image_urls;
    
    /** 
     小尺寸团购图片链接,最大图片尺寸160×100 
     */
    @property (copy, nonatomic) NSString *s_image_url;
    @property (copy, nonatomic) NSString *more_s_image_urls;
    
    /** 
     团购Web页面链接,适用于网页应用 
     */
    @property (copy, nonatomic) NSString *deal_url;
    /** 
     团购HTML5页面链接,适用于移动应用和联网车载应用 
     */
    @property (copy, nonatomic) NSString *deal_h5_url;
    
    /** 
     团购所适用的商户列表 
     把 服务器返回的 jsonObj 团购字典中的 businesses 商家里面的小字典 转为商家模型
     */
    @property (strong, nonatomic) NSArray *businesses;
    
    /** 
     团购详情 
     */
    @property (copy, nonatomic) NSString *details;
    /** 
     重要通知(一般为团购信息的临时变更) 
     */
    @property (copy, nonatomic) NSString *notice;
    
    @end
    

    ZYXDeal.m

    #import "ZYXDeal.h"
    
    #import "ZYXBusiness.h"
    
    @implementation ZYXDeal
    - (NSDictionary *)objectClassInArray
    {
        // 指明 businesses 数组 装 ZYXBusiness 模型
        return @{@"businesses" : [ZYXBusiness class]};
        
        // ZYXDeal 模型中如果有2个 businesses 数组 里面都是 ZYXBusiness 模型
        // 就分别指明一下
        return @{@"businesses1" : [ZYXBusiness class],
                 @"businesses1" : [ZYXBusiness class]};
    }
    
    - (NSDictionary *)replacedKeyFromPropertyName
    {
        // (服务端返回的字段是description)
        // description = "\U4e1c\U701b\U7f8e\U7a7a\U95f4 \U5355\U4eba\U8d44";
        
        // 服务器返回的二进制数据解析的JSON字典对象中有个 description 键名
        // ZYXDeal 模型中也要有这个属性
        // 这里不要用 description 作为成员变量指针
        // NSObject 对象有 description 方法
        // <NSObject> 协议中也有 description 方法
        // 任何遵守 <NSObject> 协议的对象都有个 description 方法
        // description 做属性会和对象的 description 方法会冲突
        // 所以 ZYXDeal 使用 desc 做属性名 但是和字典的 description 键名对不上
        
        // 模型 ZYXDeal 的 desc 属性 对应着 jsonObj字典中对象的 description 键名
        
        // desc 这个属性 将来去服务器返回的字典中找description键的值
        // 所以模型的属性你写什么都可以,你在这个方法中 把 模型的属性名 和 字典的键名对应好即可 
        return @{@"desc" : @"description"};
    }
    
    @end
    

    ZYXGetSingleDealResult.h

    #import <Foundation/Foundation.h>
    
    @interface ZYXGetSingleDealResult : NSObject
    
    /** 
     本次API访问所获取的单页团购数量 
     */
    @property (assign, nonatomic) int count;
    /** 
     所有的团购 
     */
    @property (strong, nonatomic) NSArray *deals;
    
    @end
    

    ZYXGetSingleDealResult.m

    #import "ZYXGetSingleDealResult.h"
    
    #import "ZYXDeal.h"
    
    @implementation ZYXGetSingleDealResult
    
    - (NSDictionary *)objectClassInArray
    {
        // deals 数组中装着团购 ZYXDeal 字典 
        return @{@"deals" : [ZYXDeal class]};
    }
    
    @end
    

    ZYXFindDealsResult.h

    #import <Foundation/Foundation.h>
    
    #import "ZYXGetSingleDealResult.h"
    
    @interface ZYXFindDealsResult : ZYXGetSingleDealResult
    /** 
     所有页面团购总数 
     */
    @property (assign, nonatomic) int total_count;
    @end
    

    ZYXFindDealsResult.m

    #import "ZYXFindDealsResult.h"
    @implementation ZYXFindDealsResult
    @end
    

    ZYXGetSingleDealParam.h

    #import <Foundation/Foundation.h>
    @interface ZYXGetSingleDealParam : NSObject
    /** 
     团购ID 
     */
    @property (nonatomic, copy) NSString *deal_id;
    @end
    

    ZYXGetSingleDealParam.m

    #import "ZYXGetSingleDealParam.h"
    @implementation ZYXGetSingleDealParam
    @end
    

    两个具体的业务类
    ZYXDealTool.h

    // 业务类(负责团购的所有业务)
    
    #import <Foundation/Foundation.h>
    #import "ZYXFindDealsParam.h"
    #import "ZYXFindDealsResult.h"
    #import "ZYXGetSingleDealParam.h"
    #import "ZYXGetSingleDealResult.h"
    
    @interface ZYXDealTool : NSObject
    
    /*
    业务类接口把请求地址隐藏
    获取到搜索团购的数据返回 ZYXFindDealsResult 结果
     
    ZYXDealTool的搜索团购的failure的block和ZYXAPITool请求接口的failure的block是完全一样的类型
    直接把ZYXDealTool的failure的block 传给 ZYXAPITool请求接口的failure的block 处理
    因为两个failure类型是一样的
    */
    
    /**
     *  搜索团购
     *
     *  @param param   请求参数
     *  @param success 请求成功后的回调
     *  @param failure 请求失败后的回调
     */
    + (void)findDeals:(ZYXFindDealsParam *)findDealsParamModel
              success:(void (^)(ZYXFindDealsResult *findDealsResultModel))success
              failure:(void (^)(NSError *error))failure;
    
    /**
     *  获得指定团购(获得单个团购信息)
     */
    + (void)getSingleDeal:(ZYXGetSingleDealParam *)getSingleDealParamModel
                  success:(void (^)(ZYXGetSingleDealResult *getSingleDealResultModel))success
                  failure:(void (^)(NSError *error))failure;
    @end
    

    ZYXDealTool.m

    #import "ZYXDealTool.h"
    #import "ZYXAPITool.h"
    
    @implementation ZYXDealTool
    
    + (void)findDeals:(ZYXFindDealsParam *)findDealsParamModel
              success:(void (^)(ZYXFindDealsResult *))success
              failure:(void (^)(NSError *))failure{
        [[ZYXAPITool sharedAPITool] request:@"v1/deal/find_deals"
                                     params:findDealsParamModel.keyValues // MJExtension 模型对象-->字典对象
                                    success:^(id jsonObj) {
                                        if (success) {
                                            ZYXFindDealsResult *findDealsResultModel =
                                            [ZYXFindDealsResult objectWithKeyValues:jsonObj]; // JSON字典对象-->模型对象
                                            success(findDealsResultModel);
                                        }
                                    }
                                    failure:^(NSError *error) {
                                        if (failure) {
                                            failure(error);
                                        }
                                    }];
    }
    
    + (void)getSingleDeal:(ZYXGetSingleDealParam *)getSingleDealParamModel
                  success:(void (^)(ZYXGetSingleDealResult *getSingleDealResultModel))success
                  failure:(void (^)(NSError *error))failure{
        [[ZYXAPITool sharedAPITool] request:@"v1/deal/get_single_deal"
                                     params:getSingleDealParamModel.keyValues
                                    success:^(id jsonObj) {
                                        if (success) {
                                            ZYXGetSingleDealResult *getSingleDealResultModel =
                                            [ZYXGetSingleDealResult objectWithKeyValues:jsonObj];
                                            success(getSingleDealResultModel);
                                        }
                                    }
                                    failure:failure];
    }
    
    @end
    

    接口测试

    - (void)testFindDeals{
        NSLog(@"%s",__func__);
        
        ZYXFindDealsParam *findDealsParamModel = [[ZYXFindDealsParam alloc] init];
        findDealsParamModel.city = @"北京";
        findDealsParamModel.region = @"朝阳区";
        findDealsParamModel.category = @"美发";
        findDealsParamModel.limit = @1;
        
        [ZYXDealTool findDeals:findDealsParamModel
                       success:^(ZYXFindDealsResult *findDealsResultModel) {
                            NSLog(@"搜索团购成功-------%@", findDealsResultModel.deals);
                       }
                       failure:^(NSError *error) {
                            NSLog(@"搜索团购失败-------%@", error);
                       }];
    }
    
    /*
    2016-08-26 16:20:42.562 大众点评API[82666:2602846] -[TESTViewController testFindDeals]
    2016-08-26 16:20:42.931 大众点评API[82666:2602846] 搜索团购成功-------(
                                                              "<ZYXDeal: 0x7fa9a34260d0>"
                                                            )
    */
    
    - (void)testGetSingleDeal{
        // "deal_id" = "2-11892406";
        
        NSLog(@"%s",__func__);
        
        ZYXGetSingleDealParam *getSingleDealParamModel = [[ZYXGetSingleDealParam alloc] init];
        getSingleDealParamModel.deal_id = @"2-11892406";
        
        [ZYXDealTool getSingleDeal:getSingleDealParamModel
                           success:^(ZYXGetSingleDealResult *getSingleDealResultModel) {
                                NSLog(@"获得指定团购成功---%@", getSingleDealResultModel.deals);
                           }
                           failure:^(NSError *error) {
                                NSLog(@"获得指定团购失败---%@", error);
                           }];
    
    }
    
    /*
    2016-08-26 16:21:42.049 大众点评API[82727:2603947] -[TESTViewController testGetSingleDeal]
    2016-08-26 16:21:42.471 大众点评API[82727:2603947] 获得指定团购成功---(
                                                            "<ZYXDeal: 0x7fcac1448c90>"
                                                            )
    */
    
    - (void)testAPITool{
        NSLog(@"%s",__func__);
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"city"] = @"北京";
        dict[@"region"] = @"朝阳区";
        dict[@"category"] = @"美发";
        dict[@"limit"] = @1;
        
        ZYXAPITool *apiTool = [[ZYXAPITool alloc] init];
        [apiTool request:@"v1/deal/find_deals"
                  params:dict
                 success:^(id jsonObj) {
                     NSLog(@"success jsonObj = %@",jsonObj);
                 }
                 failure:^(NSError *error) {
                     NSLog(@"failure error   = %@",error);
                 }];
    }
    

    相关文章

      网友评论

          本文标题:iOS网络层业务层-dianping api 业务层封装

          本文链接:https://www.haomeiwen.com/subject/efkzsttx.html