1.建立文件封装通过corpid获取code的方法
import { env as dd_env, util, ready as dd_ready, config } from 'dingtalk-jsapi';
import dd_requestAuthCode from 'dingtalk-jsapi/api/runtime/permission/requestAuthCode';
// import { openAuthMiniApp } from 'dingtalk-design-libs/biz/openAuthMiniApp';
export const dd_reqAuthCode = (corpId) => {
console.log('企业的corpId', corpId);
return new Promise((resolve, reject) => {
if (dd_env.platform === 'notInDingTalk') {
reject({
code:20020,
message: '请在钉钉下使用'
});
} else {
dd_ready(() => {
dd_requestAuthCode({
corpId: corpId,
onSuccess: res => {
resolve(res.code);
},
onFail: err => {
reject(err);
}
}).then(res => {
console.log('ddJsApi.dd_requestAuthCode', res);
}).catch(err => {
console.log('ddJsApi.dd_requestAuthCode', err);
});
})
}
});
};
2.引入获取token
import { dd_reqAuthCode } from '@/utils/dd-func';
// 登录
LoginDing({ state, commit }, userInfo) {
return new Promise((resolve, reject) => {
dd_reqAuthCode(state.corpId).then(authCode => {
dinglogin({ code: authCode }).then((res) => {
setToken(res.token)
commit('SET_TOKEN', res.token)
resolve()
}).catch(err => reject(err));
});
});