npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@codoonfxd/crn-lib

v0.9.3

Published

react native通用方法封装

Downloads

3

Readme

react native 通用方法封装

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

安装

# npm
npm install @codoonfxd/crn-lib -S
# yarn
yarn add @codoonfxd/crn-lib

发布新版本

发布新版本需要更改package.json中的version

在修复了 bug 或者添加了新功能之后,如果想进行发布,请按照以下步骤操作:

  1. 命令行进入crn-lib项目目录,即packages/@codoonfxd/crn-lib目录下。
  2. 执行npm run build打包。
  3. 执行npm publish即可发布成功(如果失败,请联系管理员)。

使用

  • 导入模块
// 部分引入
import { native, api, config } from '@codoonfxd/crn-lib';
// 引入所有至lib变量
import * as lib from '@codoonfxd/crn-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();

16、showPay(goods_id, orderExternalInfo, list)

  • 功能:根据商品 id,调用咕咚币支付弹窗
  • 参数:
    • goods_id: string, // 商品 id
    • orderExternalInfo: string // 透传参数 json 格式
    • list: Array<{ title: string, content: string, }>, // 需要在列表里展示的内容

17、showPayState(state, classType)

  • 功能:展示支付状态的弹窗,包括支付中,支付成功,支付失败三种状态。
  • 参数:
    • state: 支付状态(0 -> 支付中, 1 -> 支付成功, 2 -> 支付失败)
    • classType: 课程类型(0 -> 训练课程, 1 -> 直播课程)

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','#')