gisq-dd-mobile
Version:
gisq dingding mobile lib
293 lines (285 loc) • 8.65 kB
JavaScript
import dd from 'dingtalk-jsapi'
import axios from 'axios'
/* Date.prototype.Format = function(fmt)
{ //author: meizz
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
} */
export var gd = {
authorCode: "",
accessToken: "",
ticket: "",
userInfo: "",
agentId:"",
signature:"",
nonceStr:"",
timeStamp:"",
scenseId:"",
locate: function locate() {
console.log("locate");
},
alert: function alert(msg,title,buttonName,successCallback,errorCallback) {
if (!dd) return;
dd.device.notification.alert({
message: msg,
title: title,//可传空
buttonName: buttonName,
onSuccess : function(){
successCallback();
},
onFail:function(err){
errorCallback(err);
}
});
},
axiosGet: function(url, params) {
return new Promise((resolve, reject) => {
axios.get(url, {
params: params
}).then(res => {
resolve(res.data);
}).catch(err => {
reject(err.data)
})
});
},
axiosPost: function(url, params) {
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
return new Promise((resolve, reject) => {
axios.post(url, params)
.then(res => {
resolve(res.data);
})
.catch(err => {
reject(err.data)
});
});
},
axiosPostFormData: function axiosPost(url, params) {
axios.defaults.headers.post['Content-Type'] = 'application/json';
return new Promise(function(resolve, reject) {
var fd = new FormData();
for (let it in params) {
fd.append(it, params[it]);
}
axios.post(url, fd).then(res => {
resolve(res.data);
})
.catch(err => {
reject(err.data)
});
});
},
getAccessToken: function(baseUrl, params) {
var _this = this;
var url = "/user/getAccessToken";
this.axiosGet(baseUrl + url, params).then(accessTokenStr => {
localStorage.setItem("gisqDdAccessToken", accessTokenStr);
_this.accessToken = accessTokenStr;
});
},
getTicket: function(baseUrl, params, isJsAuth) {
//TODO ajax getTicket
var _this = this;
var url = "/user/getJsAuthorTicket";
this.axiosGet(baseUrl + url, params).then(ticketStr => {
localStorage.setItem("gisqDdTicket", ticketStr);
if (isJsAuth) _this.jsAuthor(ticketStr);
_this.ticket = ticketStr;
});
},
jsAuthor: function(corpId,ticketStr,agentId,timeStamp,nonceStr,signature) {
if (!dd) return;
alert("window.location.href===="+window.location.href);
dd.config({
agentId: agentId, // 必填,微应用ID
corpId: corpId,//必填,企业ID
timeStamp: timeStamp, // 必填,生成签名的时间戳
nonceStr: nonceStr, // 必填,生成签名的随机串
signature: signature, // 必填,签名
type:0,
jsApiList: ["device.base.getUUID", "biz.util.scanCard", "biz.util.open",
"device.geolocation.get", "device.geolocation.start", "device.geolocation.stop",
"biz.map.locate"
]
})
},
getAuthorCode: function(corpId) {
if (!dd) return;
return new Promise((resolve, reject) => {
dd.runtime.permission.requestAuthCode({
corpId: corpId,
onSuccess: function (info) {
resolve(info)
},
onFail : function(err) {
reject(err);
},
});
});
},
getUserDetail: function(baseUrl, accessToken, authorCode) {
var _this = this;
var url = "/user/getUserDetail";
var params = {
"accessToken": accessToken,
"authorCode": authorCode
}
this.axiosGet(baseUrl + url, params).then(userInfo => {
localStorage.setItem("gisqDdUserInfo", userInfo);
_this.userInfo = userInfo;
});
},
freeLogin: function(corpId, baseUrl, isJsAuth, successCallback, errorCallback) {
var _this = this;
this.getAuthorCode(corpId).then(function(authCodeJson) {
var url = "/user/freeLogin";
var params = {
"authorCode": authCodeJson.code
}
_this.axiosPostFormData(baseUrl + url, params).then(res => {
//_this.alert("freeLogin" + JSON.stringify(res));
if (res && res.data.userInfo) {
_this.accessToken = res.data.accessToken;
_this.ticket = res.data.jsTicket;
_this.userInfo = res.data.userInfo;
_this.agentId = res.data.agentId;
_this.nonceStr = res.data.nonceStr;
_this.signature = res.data.signature;
_this.timeStamp = res.data.timeStamp;
successCallback(res);
if (isJsAuth) {
alert("sssss===="+_this.timeStamp+","+_this.nonceStr+","+_this.signature);
_this.jsAuthor(corpId,_this.ticket,_this.agentId,_this.timeStamp,_this.nonceStr,_this.signature);
}
} else {
errorCallback("failed to login");
}
});
}).catch(err => {
_this.alert("获取授权码错误" + JSON.stringify(err));
});
},
getGeolocation: function(useCache, successCallback, errorCallback) {
dd.device.geolocation.get({
targetAccuracy : 200,
coordinate : 0,
withReGeocode : false,
useCache:useCache, //默认是true,如果需要频繁获取地理位置,请设置false
onSuccess : function(result){
successCallback(result);
},
onFail : function(err) {
errorCallback(err);
}
});
},
gpsFollowStart:function(callBackInterval,onSuccess,onFail){
var scenseIdStr=new Date().getTime()+"";
this.scenseId=scenseIdStr;
dd.device.geolocation.start({
targetAccuracy : 200, // 期望精确度
iOSDistanceFilter: 2, // 变更感知精度(iOS端参数)
useCache: false, // 是否使用缓存(Android端参数)
withReGeocode : false, // 是否返回逆地理信息,默认否
callBackInterval : callBackInterval, //回传时间间隔,ms
sceneId: scenseIdStr, // 定位场景id,
onSuccess : function(result) {
/* 结果同dd.device.geolocation.get */
onSuccess(result);
},
onFail : function(err) {
onFail(err);
}
});
},
gpsFollowStop:function(onSuccess,onFail){
var _this=this;
dd.device.geolocation.stop({
sceneId: _this.scenseId, // 需要停止定位场景id
onSuccess : function(result) {
onSuccess(result);
},
onFail : function(err) {
onFail(err);
}
});
},
takePhoto: function(successCallback, errorCallback) {
},
recordVideo: function(successCallback, errorCallback) {
},
getPhoneInfo: function(successCallback, errorCallback) {
dd.device.base.getPhoneInfo({
onSuccess : function(data) {
/*
{
screenWidth: 1080, // 手机屏幕宽度
screenHeight: 1920, // 手机屏幕高度
brand:'Mi', // 手机品牌
model:'Note4', // 手机型号
version:'7.0', // 版本
netInfo:'wifi', // 网络类型 wifi/4g/3g
operatorType:'xx' // 运营商信息
}
*/
successCallback(data)
},
onFail : function(err) {
errorCallback(err)
}
});
},
getUUID: function(successCallback, errorCallback) {
dd.device.base.getUUID({
onSuccess : function(data) {
/*
{
uuid: '3udbhg98ddlljokkkl' //
}
*/
successCallback(data)
},
onFail : function(err) {
errorCallback(err)
}
});
},
scan: function(type, successCallback, errorCallback) {
dd.biz.util.scan({
type: type ? type : "qr" , // type 为 all、qrCode、barCode,默认是all。
onSuccess: function(data) {
//onSuccess将在扫码成功之后回调
/* data结构
{ 'text': String}
*/
successCallback(data)
},
onFail : function(err) {
errorCallback(err)
}
})
},
version: function(successCallback, errorCallback) {
successCallback(dd.version);
},
ready: function(callback) {
dd.ready(function() {
//调用jsapi方法
callback();
})
}
};