Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

API 定义 #3

Open
imfycc opened this issue Oct 12, 2020 · 0 comments
Open

API 定义 #3

imfycc opened this issue Oct 12, 2020 · 0 comments

Comments

@imfycc
Copy link
Member

imfycc commented Oct 12, 2020

API 定义

登录注册

login 包含注册,无需关心新用户注册场景。

使用

import { login, checkLogin } from 'hlj-open-sdk';
import 'hlj-open-sdk/dist/lib.css'; // 引入样式

const loginSuccess = () => {
  // 登录成功后的回调
}

const onClick => async() => {
  const logined = await checkLogin();
  if (!logined) {
     login(loginSuccess);  // h5 和 app 内会唤起登录弹窗, 小程序内会静默登录
  }
}

埋点

使用

import { trackAction, trackPage } from 'hlj-open-sdk';

// 埋点字符串由产品同学提供,以下字符串仅供演示使用

trackPage('b-face-test'); // 页面埋点 进入页面时调用 比如,didMount 时调用

const onClick = () => {
  trackAction('b-face-test.click') // 动作埋点  长按或者点击等动作埋点
}

图片上传

使用

import { uploadImage, imgurl } from 'hlj-open-sdk';

const onUpload = async(event) => { // input upload event
  const file = event.target.files[0];
  const res = await uploadImage(file);
  if (res.path) {
      console.log(path); // /upload/20201012/3/2/326e310bdee14a9ca231ae32188d7d0e.jpeg
      console.log(imgurl(path)); // 补全图片地址  https://img-ucdn-static.helijia.com/zmw/upload/20201012/3/2/326e310bdee14a9ca231ae32188d7d0e.jpeg
     ... 
     return;
  } 
  if (res.errorMessage) {
     console.log(res.errorMessage);
  }
}

获取 app 相册图片

使用

import { getAppImage } from 'hlj-open-sdk';
import compareVersions from 'compare-versions';
import qs  from 'query-string';

// type 1 拍照 2 选相册 3 拍照+选相册
// 该方法仅安卓 4.66.0 及以上版本可用 需要判断设备和版本号
const { appVersion } = qs.parse(window.location.search);
if (getPlatform() === 'android' && compareVersions.compare(appVersion, '4.66.0', '>=')) {
    getAppImage({type: 3}).then(image => {
       console.log(image); // "data:image/png;base64,xxxxxxxx"
    })
}

链接识别和跳转

使用 go 代替 window.location.href 跳转

url 需要经过 hljurl 处理,转换成各个端(App、H5、小程序)能识别的链接

使用

import { go, hljurl } from 'hlj-open-sdk';

const onClick = () => {
  const url = 'hljclient://page?jsonData={"type":4,"pageName":"HTML","data":{"url1":"https://m.helijia.com"}}';
  go(hljurl(url));
}

分享

使用

import { setShare, doShare } from 'hlj-open-sdk';

setShare({
  icon: 'https://static.helijia.cn/zmw/upload/active/bigfish/fishicon.jpg',
  title: '分享标题',
  desc: '分享副标题,一般会有很多字',
  link: 'https://m.helijia.com',  // 分享出去的 h5 链接
  mediaType: 0,  // 代表分享的媒体类型 0 是链接 1 是单图 2是小程序
  miniPath: '/pages/index',   // 分享的小程序地址
  miniUserName: 'gh_899999999', 
  miniTitle: '分享小程序标题',
  weappImg: 'https://static.helijia.cn/zmw/upload/active/bigfish/fishicon.jpg'
});

doShare();  直接唤起分享浮窗

平台判断

使用

import { getPlatform } from 'hlj-open-sdk';

getPlatform();

// ios 河狸家 iOS 客户端
// android 河狸家安卓客户端
// aliapp 支付宝小程序
// weapp 微信小程序
// wap H5

const isApp = getPlatform() === 'ios' || getPlatform() === 'android';

网络请求

1、自动获取 token 处理登录信息

自动从 url 参数中获取 token=... 并附在 ajax 请求中

2、返回结果 success: true 会当做成功,否则当做失败 reject

接口返回结果

请求成功

{
   "data": "返回的数据类型不同的接口定义可能不同",
   "success": true
}

请求失败

{
   "success": false,
   "apiCode": "12020",
   "apiMessage": "请求接口出错"
}

使用

import { ajax, isAuthError} from 'hlj-open-sdk';

const loadData = () => {
  ajax({
    url: 'https://app.helijia.com/customer/upload',
    method: 'POST' // 可省略 默认 GET
    data: { // 无论是 POST  还是 GET,参数都通过 data 传递
       a: 1,
       b: 'test' 
    }
  }).then(res => {
     // 请求成功,说明 res.success 为 true
     // 处理返回结果 res.data
  }, res => {
     const { apiCode, apiMessage} = res || {};
     // apiCode 错误码
     // apiMessage 错误信息
     // isAuthError(apiCode) 如果需要判断是否登录失效的场景,可以使用 isAuthError   true 登录失效 
  });
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant