npm install dingtalk-jsapi --save import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载,也可按需进行加载
jsapi鉴权
获取token、ticket、签名的操作都在后端,前2步可后端单独进行,获取签名通过前端提供一个时间戳、项目url(不包含#后面路径,用window.location.href.split('#')[0],ios需要使用decodeURL编码一下)
前端可在main.js中获取授权
dd.config({ agentId: '', // 必填,微应用ID,后端返回 corpId: '',//必填,企业ID,后端返回 timeStamp: '', // 必填,生成签名的时间戳 nonceStr: '', // 必填,自定义固定字符串。前后端统一 signature: '', // 必填,签名,后端返回 type:0/1, //选填。0表示微应用的jsapi,1表示服务窗的jsapi;不填默认为0。该参数从dingtalk.js的0.8.3版本开始支持 jsApiList : [ 'biz.contact.choose', 'device.geolocation.get' ] // 必填,需要使用的jsapi列表,注意:不要带dd。 }); dd.error(function (err) { alert('dd error: ' + JSON.stringify(err)); })//该方法必须带上,用来捕获鉴权出现的异常信息,否则不方便排查出现的问题
获取当前位置信息(单次)
参数说明
参数 | 类型 | 说明 |
---|---|---|
targetAccuracy | Number | 期望定位精度半径(单位米),定位结果尽量满足该参数要求,但是不一定能保证小于该误差,开发者需要读取返回结果的 accuracy 字段校验坐标精度。 |
建议按照业务需求设置定位精度,推荐采用200m,可获得较好的精度和较短的响应时长。 | ||
coordinate | Number | 1:获取高德坐标0:获取标准坐标 |
推荐使用高德坐标,标准坐标没有 address字段。 | ||
withReGeocode | Boolean | 是否需要带有逆地理编码信息。 |
该功能需要网络请求,请根据自己的业务场景使用。 | ||
useCache | Boolean | 是否缓存地理位置信息。默认是true。如果为true,客户端会对定位的地理位置信息缓存,在缓存期内 (2分钟) 再次定位会返回旧的定位。 |
dd.ready(()=>{ dd.device.geolocation.get({ targetAccuracy: 200, coordinate: 1, withReGeocode: false, useCache: true, onSuccess : function(res) { // 调用成功时回调 /* 高德坐标 result 结构 { longitude : Number, latitude : Number, accuracy : Number, address : String, province : String, city : String, district : String, road : String, netType : String, operatorType : String, locationType:1, errorMessage : String, errorCode : Number, isWifiEnabled : Boolean, isGpsEnabled : Boolean, isFromMock : Boolean, provider : wifi|lbs|gps, isMobileEnabled : Boolean } */ console.log(res) }, onFail : function(err) { // 调用失败时回调 console.log(err) } }); })