UNPKG

gisq-dd-mobile

Version:

gisq dingding mobile lib

337 lines (330 loc) 9.36 kB
import dd from 'gdt-jsapi' import axios from 'axios' export var gd = { authorCode: "", accessToken: "", ticket: "", userInfo: "", agentId:"", signature:"", nonceStr:"", timeStamp:"", scenseId:"", locate: function locate() { console.log("locate"); }, alert: function alert(msg) { console.log("locate"); if (!dd) return; dd.alert({ message: msg }); }, 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(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(ticketStr, successCallback) { if (!dd) return; var _this = this; dd.authConfig({ ticket: ticketStr, jsApiList: ["alert", "createChatGroup", "openChat", "copyToClipboard", "getGeolocation", "chooseDepartments", "chooseContactWithComplexPicker", "chooseContact", "takePhoto","startGeolocation","stopGeolocation","downloadFile" ] }).then(res => { console.log(res) successCallback(); //_this.alert("js授权错误"+JSON.stringify(res)); }).catch(err => { console.log(err) _this.alert("js授权错误" + JSON.stringify(err)); }) }, getAuthorCode: function(corpId) { if (!dd) return; return new Promise((resolve, reject) => { dd.getAuthCode({ corpId: corpId }).then(res => { console.log(res) resolve(res) }).catch(err => { //_this.alert("获取授权码错误"+JSON.stringify(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; successCallback(res); if (isJsAuth) { _this.jsAuthor(_this.ticket, function() {}); } } else { errorCallback("failed to login"); } }); }).catch(err => { _this.alert("获取授权码错误" + JSON.stringify(err)); }); }, freeLoginNew: function(tdtype,indexurl,corpId, baseUrl, isJsAuth, successCallback, errorCallback) { var _this = this; this.getAuthorCode(corpId).then(function(authCodeJson) { var url = "/tdLogin"; var params = { "tdtype": tdtype, "authorCode": authCodeJson.code, "indexurl": indexurl } _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(_this.ticket, function() {}); } } else { errorCallback("failed to login"); } }); }).catch(err => { _this.alert("获取授权码错误" + JSON.stringify(err)); }); }, getGeolocation: function(useCache, successCallback, errorCallback) { dd.getGeolocation({ targetAccuracy: 200, coordinate: 1, withReGeocode: true, useCache: useCache, //默认是true,如果需要频繁获取地理位置,请设置false }).then(res => { successCallback(res); }).catch(err => { errorCallback(err); }) }, getGeolocationCustom: function(option,useCache, successCallback, errorCallback) { dd.getGeolocation(option).then(res => { successCallback(res); }).catch(err => { errorCallback(err); }) }, takePhoto: function(successCallback, errorCallback) { dd.takePhoto().then(res => { successCallback(res); }).catch(err => { errorCallback(err); }); }, recordVideo: function(successCallback, errorCallback) { dd.shootVideo().then(res => { successCallback(res) }).catch(err => { errorCallback(err); }) }, getPhoneInfo: function(successCallback, errorCallback) { dd.getPhoneInfo().then(res => { successCallback(res) }).catch(err => { errorCallback(err) }) }, getUUID: function(successCallback, errorCallback) { dd.getUUID().then(res => { successCallback(res) }).catch(err => { errorCallback(err) }) }, scan: function(type, successCallback, errorCallback) { dd.scan({ type: type ? type : "qr" }).then(res => { successCallback(res) }).catch(err => { errorCallback(err) }) }, version: function(successCallback, errorCallback) { dd.version().then(res => { successCallback(res) }).catch(err => { errorCallback(err) }) }, ready: function(callback) { dd.ready(function() { //调用jsapi方法 callback(); }) }, getLoginUser: function(successCallback, errorCallback) { dd.getLoginUser().then(res => { successCallback(res) }).catch(err => { errorCallback(err) }) }, fetchLocal: function(url) { return new Promise(function(resolve, reject) { var xhr = new XMLHttpRequest(); xhr.responseType = 'arraybuffer'; xhr.onload = function() { if (xhr.status == 0) { xhr.status = 200; } resolve(new Response(xhr.response, { status: xhr.status })); }; xhr.onerror = function() { reject(new TypeError('Local request failed')); }; xhr.open('GET', url); xhr.send(null); }); }, getMobileLocalFile: function(path,fileName, successCallback,errorCallback) { this.fetchLocal(path).then(function(response) { return response.arrayBuffer(); }).then(function(arrayBuffer) { //console.log(arrayBuffer) var fd = new File([new Blob([arrayBuffer])], fileName, { "type": "text/plain", "lastModified": "" + new Date() + "" }); successCallback(fd); }).catch(function(err) { errorCallback(err); }); }, gpsFollowStart:function(callBackInterval,onSuccess,onFail){ var scenseIdStr=new Date().getTime()+""; this.scenseId=scenseIdStr; dd.startGeolocation({ 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.stopGeolocation({ sceneId: _this.scenseId, // 需要停止定位场景id onSuccess : function(result) { onSuccess(result); }, onFail : function(err) { onFail(err); } }); }, downloadFile:function(config){ /** * dd.downloadFile({ url: "https://download.dg-work.cn/Dinggov.exe?spm=a2q2b.13441934.downloadAppTip.downloadWindows.6e7d17decEo0xB&file=Dinggov.exe", name: "Dinggov.exe" }) */ var _this=this; dd.downloadFile(config); } };