1.3.1 • Published 5 years ago

react-native-lib v1.3.1

Weekly downloads
4
License
ISC
Repository
-
Last release
5 years ago

react native通用方法封装

react native通用方法封装,包括:网络请求、数据缓存、NativeModules方法封装和其他一些通用方法。

####npm login

####npm publish

npm version patch | minor | major
npm publish

安装

npm install react-native-lib

使用

  • 导入模块

import 'react-native-lib';
或
import lib from 'react-native-lib';
  • 方法调用
//网络请求
lib.api.fetch()
  • 参数说明: url: 请求url,string,required。 method: 请求方法,string,optional (默认post)。 signature: 是否验签 boolean, optional (默认false)。 storagekey: 缓存key string, optional (默认为空)。
  • 示例:
lib.api.fetch({  
  url: 'http://www.baidu.com',
  method: 'post',
  signature: false,
  storagekey: 'fds'
}).then((response) => {
  if (response.status == 'OK') {
    //...
  } else {
    console.log(response.description);
  }
})
//nativeModules方法
lib.native.funcName();

//缓存方法
lib.storage.funcName();

nativeModules属性说明

lib.native.appEventEmitter

  • 功能:获取NativeModules.CDBridgeAppEventEmitter类,8.1.0版本开始支持。
  • 语法:
lib.native.appEventEmitter

nativeModules方法使用说明

0、global.getUserViewState

  • 功能:统计用户浏览情况
  • 用法:componentDidMount中把gloabl.getUserViewState指向一个同步函数,该函数返回统计离开页面的所需数据
componentDidMount () {
  const extraBUInfo = {
    match_id: 1,
    match_name: 2,
    match_type: 3,
    match_sport_type: 4,
    match_group_type: 5,
    match_level: 6
  }
  global.getUserViewState = () => {
    return {
      content_height: contentOffset + 200,   //内容高度,必须字段
      show_height: contentOffset + 200 - 50, //展示高度,必须字段
      obsolute_height: contentOffset,        //已经滑动的最大值,必须字段
      ...extraBUInfo                         //扩展数据
    }
  }
}

1、logEvent

  • 功能:统计事件
  • 语法:
lib.native.logEvent(options);
  • 参数说明:
    options:object,包括选项如下:
    eventID:统计事件ID,string,必需。
  • 示例:
lib.native.logEvent({  
    eventID: 'test_1213'   
});  

2、logEventWithParams

  • 功能:统计事件带参数
  • 语法:
lib.native.logEventWithParams(options);
  • 参数说明: options:object,包括选项如下:
    eventID:统计事件ID,string,必需。
    params:统计事件参数,object,选填。
  • 示例:
lib.native.logTimedEventWithParams({  
   eventID: '12322',  
   params: {  
       user_id:'test'  
   }  
})

3、logTimedEventWithParams

  • 功能:统计事件持续时间——事件开始调用方法
  • 语法:
lib.native.logTimedEventWithParams(options);
  • 参数说明: options:object,包括选项如下:
    eventID:统计事件ID,string,必需。
    params:统计事件参数,object,选填。
  • 示例:
lib.native.logTimedEventWithParams({  
   eventID: '12322',  
   params: {  
       user_id:'test'  
   }  
})

4、endTimedEventWithParams

  • 功能:统计事件持续时间——事件结束调用方法
  • 语法:
lib.native.endTimedEventWithParams(options);
  • 参数说明: options:object,包括选项如下:
    eventID:统计事件ID,string,必需。
    params:统计事件参数,object,选填。
  • 示例:
lib.native.endTimedEventWithParams({
   eventID: '12322',
   ...params
})

5、logTimedEventWithParamsAndTag

  • 功能:统计事件持续时间,携带事件唯一标识 —— 事件开始调用
  • 语法:
lib.native.logTimedEventWithParamsAndTag(options);
  • 参数说明: options:object,包括选项如下:
    eventID:统计事件ID,string,必需。
    params:统计事件参数,object,选填。 tag:事件唯一标识,string,必需`。
  • 示例:
lib.native.logTimedEventWithParamsAndTag ({
   eventID: '12322',
   params: {
       user_id:'test',
       user_name:'xxx'
   },
   tag:'11'
})

6、endTimedEventWithParamsAndTag

  • 功能:统计事件持续时间,携带事件唯一标识 —— 事件结束调用
  • 语法:
lib.native.endTimedEventWithParamsAndTag(options);
  • 参数说明: options:object,包括选项如下:
    eventID:统计事件ID,string,必需。
    params:统计事件参数,object,选填。 tag:事件唯一标识,string,必需`。
  • 示例:
lib.native.endTimedEventWithParamsAndTag ({
   eventID: '12322',
   params: {
       user_id:'test',
       user_name:'xxx'
   },
   tag:'11'
})

7、miaoZhenAdAnalytics

  • 功能:秒针广告监控
  • 语法:
lib.native.miaoZhenAdAnalytics(options);
  • 参数说明: options:object,包括选项如下:
    url:'请求链接,string,必需'。
  • 示例:
lib.native.miaoZhenAdAnalytics({
   url: 'http://www.cocoon.com'
})

8、clearTrackTimer

  • 功能:清除所有事件计时器的时间计数
  • 语法:
lib.native.clearTrackTimer();

9、setLogUserProfile

  • 功能:设置当前统计客户端的用户属性
  • 语法:
lib.native.setLogUserProfile({
  name: '李蛋',
  age: '21',
  sex: '男'
});
  • 参数说明:options:object, 包含自定义的用户属性字段

10、unsetLogUserProfile

  • 功能:设置当前统计客户端的用户属性
  • 语法:
lib.native.unsetLogUserProfile('sex');
  • 参数说明:options:string, 自定义的用户属性字段名

11、deleteLogUserData

  • 功能:删除当前统计客户端的所有记录
  • 语法:
lib.native.deleteLogUserData();

12、appendUserProfile

  • 功能:删除当前统计客户端的所有记录
  • 语法:
lib.native.appendUserProfile(key, value);
  • 参数说明: key: string value: string|object|array|boolean

8、handleSchemeURL

  • 功能:页面跳转,支持咕咚自定义的跳转协议,包括跳转原生页面和浏览器页面
  • 语法:
lib.native.handleSchemeURL(options);
  • 参数说明: options:object,包括选项如下:
    url:跳转协议,string,必需。
    success:成功回调函数,function,选填。
    error:失败回调函数,function,选填。
  • 示例:
lib.native.handleSchemeURL({
   url: 'http://www.cocoon.com',
   success: function (data) {
     console.log(data);
   },
   error: function (error) {
    console.log(error);
   }
})

9、shareWithTypes

  • 功能:调用原生分享组件
  • 语法:
lib.native.shareWithTypes(options);
  • 参数说明: options:object,包括选项如下:
    types:'分享类型,array,必需',
    defaultShareInfo:'默认分享信息,object,必需',
    customShareInfo:'自定义分享信息,object,必填',
    success:'成功回调函数,function,选填',
    error:'失败回调函数,function,选填'。

"分享类型"参数说明:

0             不支持的类型  
1             运动圈  
2             运动团  
3             微信朋友圈  
4             微信好友    
5             微博  
6             qq  
7             QQ空间  
8             更多  
9             复制链接 

"默认分享信息"参数说明:
title:分享标题,string,必需
content:分享内容,string,必需
img_url:图片链接,string,选填
url:跳转链接,string,选填

"自定义分享信息"参数说明:

{
   "1": {
          title: '1的分享标题,string,必需',
          content: '1的分享内容,string,必需',
          img_url: '1的图片链接,string,选填',
          url: '1的跳转链接,string,选填'
    },
    "2": {
          title: '2的分享标题,string,必需',
          content: '2的分享内容,string,必需',
          img_url: '2的图片链接,string,选填',
          url: '2的跳转链接,string,选填'
    }
  ....            
}
  • 示例:
lib.native.shareWithTypes({
      types: [1, 2, 3, 4],
      defaultShareInfo: {
        title: 'share title',
        content: 'share content',
      },
      customShareInfo: {
        "1": {
          title: '1 share title',
          content: '1 share content',
        }
      },
      success: function (data) {
        console.log(data);
      },
      error: function (error) {
        console.log(error)
      }
    });

10、fetchAccount

  • 功能:获取当前用户信息
  • 语法:
lib.native.fetchAccount(options);
  • 参数说明: options:object,包括选项如下:
    success:'成功回调函数,function,选填',
    error:'失败回调函数,function,选填'。
  • 示例:
lib.native.fetchAccount({
      success: function (data) {
        console.log(data);
      },
      error: function (error) {
        console.log(error)
      }
    });

11、fetchLocation

  • 功能:获取当前用户定位信息
  • 语法:
lib.native.fetchLocation(options);
  • 参数说明: options:object,包括选项如下:
    success:'成功回调函数,function,选填',
    error:'失败回调函数,function,选填'。
  • 示例:
lib.native.fetchLocation({
      success: function (data) {
        console.log(data);
      },
      error: function (error) {
        console.log(error)
      }
    });

12、relationWithUserID

  • 功能:获取用户关系
  • 语法:
lib.native.relationWithUserID(options);
  • 参数说明: options:object,包括选项如下:
    userID:被查询的用户ID,string,必需,
    success:'成功回调函数,function,选填',
    error:'失败回调函数,function,选填'。
  • 示例:
lib.native.relationWithUserID({
      userID: 'dsdsdsf',
      success: function (data) {
        console.log(data);
      },
      error: function (error) {
        console.log(error)
      }
    });

13、synchronizeRelations

  • 功能:更新用户关系
  • 语法:
lib.native.synchronizeRelations(options);
  • 参数说明: options:object,包括选项如下:
    success:'成功回调函数,function,选填',
    error:'失败回调函数,function,选填'。
  • 示例:
lib.native.synchronizeRelations({
      success: function (data) {
        console.log(data);
      },
      error: function (error) {
        console.log(error)
      }
    });

14、initPageType

  • 功能:标记当前页面是哪一个页面,android专用
  • 语法:
lib.native.initPageType(options);
  • 参数说明: options:object,包括选项如下:
    type:页面对应索引,number,必需。
  • 示例:
lib.native.initPageType({
    type:2
});

15、popModule

  • 功能:关闭rn模块
  • 语法:
lib.native.popModule();
  • 示例:
lib.native.popModule();

Storage方法使用说明

1、save

  • 功能:保存数据
  • 语法:
lib.storage.save(options);
  • 参数说明:
    options:object,包括选项如下:
    key:键值,string,必需。(注:不要在key中使用_下划线符号) , data:保存数据 object 必需。
  • 示例:
lib.storage.save({  
    key: 'storagekey',
    data: data
});  

2、load

  • 功能:读取数据
  • 语法:
lib.storage.load(options);
  • 参数说明:
    options:object,包括选项如下:
    key:键值,string,必需。
  • 示例:
lib.storage.load({  
    key: 'storagekey'
});  

3、getBatchData

  • 功能:读取批量数据
  • 语法:
lib.storage.getBatchData(options);
  • 参数说明:
    options:Array,包括选项如下:
    object: 各个缓存数据键值 必需。
  • 示例:
lib.storage.getBatchData([
  {key: "storagekey1"},
  {key: "storagekey2"},
]);  

4、remove

  • 功能:删除单个数据
  • 语法:
lib.storage.remove(options);
  • 参数说明:
    options:Object
    key: 键值,string,必需。
  • 示例:
lib.storage.remove({
  key: "storagekey"
});  

其他通用方法使用说明

1、px2dp

  • 功能:px转换为dp,parseInt for android bug
  • 语法:
lib.utils.px2dp(px);
  • 参数说明:
    px:要转换的px长度值,number,必需。
  • 示例:
lib.utils.px2dp(10)  

2、stringToJson

  • 功能:非object对象转换为json对象
  • 语法:
lib.utils.stringToJson(item);
  • 参数说明:
    item:要转换非object数据,非object类型,必需。
  • 示例:
lib.utils.stringToJson('[1,2,3]') 

3、dynamicMargin

  • 功能:动态调整margin:给列表中第一项设置marginLeft,最后一项设置marginRight,其他项不做处理
  • 语法:
lib.utils.dynamicMargin(total, index, value);
  • 参数说明:
    total:列表总项数,number,必需。
    index:列表当前项索引,number,必需。
    value:需要调整的margin值,number,选填,默认值为16。
  • 示例:
lib.utils.dynamicMargin(4,0)

4、compareWithVersion

  • 功能:判断客户端当前版本是否大于等于指定版本,果客户端当前版本>=miniVersion,返回true;否则返回false。
  • 语法:
lib.utils.compareWithVersion(miniVersion);
  • 参数说明:
    miniVersion:指定的版本号,string,必需。
  • 示例:
lib.utils.compareWithVersion('8.8.0')

5、getQueryStr

  • 功能:获取url参数
  • 语法:
lib.utils.getQueryStr(url, separator)
  • 参数说明:
    url:需要获取的URL,string,必填
    separator:URL中参数和链接之间的分隔符,string,选填,默认为"?"
  • 示例:
lib.utils.getQueryStr('https://www.npmjs.com/package/url-parse');//{}

//返回值为:{name: "xiaoma", sex: "female"}
lib.utils.getQueryStr('https://www.npmjs.com/package/url-parse?name=xiaoma&sex=female');

//返回值为:{name: "xiaoma", sex: "female"}
lib.utils.getQueryStr('https://www.npmjs.com/package/url-parse#name=xiaoma&sex=female','#')
1.3.1

5 years ago

1.3.0

5 years ago

1.2.16

6 years ago

1.2.15

6 years ago

1.2.14

6 years ago

1.2.13

6 years ago

1.2.12

6 years ago

1.2.11

6 years ago

1.2.10

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.6

6 years ago

1.2.5

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.9

6 years ago

1.1.71

6 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago