悟空crm系统oa对接钉钉扫码登陆 vue+tp构建的5kcrm通过钉钉扫码登陆

  • A+
所属分类:个人体验

悟空CRM也叫5kcrm是一个基于TP5.0+vue+ElementUI的前后端分离CRM系统,开源版本没有钉钉扫码登陆接口,想要钉钉扫码登陆接口就要另外付费,那就自己整,各种跳坑,vue架构又不太了解,钉钉接口获取到authKey和sessionID都不能跳转过去,最后弄了head传参才完事。

先获取凭证

https://open-dev.dingtalk.com/apiExplorer#/?devType=org&api=dingtalk.oapi.gettoken

include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai');

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_GET , DingTalkConstant::$FORMAT_JSON);
$req = new OapiGettokenRequest;
$req->setAppkey("dingo88888888dozusw");
$req->setAppsecret("8bLqB888888888888888DXocTPrhm-BwqRjN8HkBvggDXdJ");
$resp = $c->execute($req, $access_token, "https://oapi.dingtalk.com/gettoken");

 

获取后的$resp 是一个对象,可以直接读取

$unionid=$resp->user_info->unionid;

也可以转换成熟组再读取

$resp = json_encode($resp);
$accessToken_array=json_decode($resp,true);
$accessToken=$accessToken_array['access_token'];

 

 

搞了半天,手机号没出来,还以为是接口的问题。

最后看到一行字。

注意 :如果在调用中发现返回数据中没有手机号、邮箱等信息,登录开发者后台查看是否添加了获取通讯录中手机号和邮箱的权限。

date_default_timezone_set('Asia/Shanghai');
include "TopSdk.php";
$appId = 'ding88888ozusw';
$appSecret = '8bLqBS888888888888888888uFIKDXocTPrhm-BwqRjN8HkBvggDXdJ';

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
$req = new OapiSnsGetuserinfoBycodeRequest;
$code = $_REQUEST['code'];
$req->setTmpAuthCode($code);
$resp=$c->executeWithAccessKey($req, "https://oapi.dingtalk.com/sns/getuserinfo_bycode",$appId ,$appSecret);

//直接读取$resp对象的unionid元素
$unionid=$resp->user_info->unionid;

//print_r($unionid);
//echo '<br>';
$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_GET , DingTalkConstant::$FORMAT_JSON);
$req = new OapiGettokenRequest;
$req->setAppkey("dingooyggsqtdvdozusw");
$req->setAppsecret("8bLqBS_95cfOlWEnMwuT2mMGONK_U2r1UuFIKDXocTPrhm-BwqRjN8HkBvggDXdJ");
$resp = $c->execute($req, $access_token, "https://oapi.dingtalk.com/gettoken");
//对象转换为数组,再进行读取
$resp = json_encode($resp);
$accessToken_array=json_decode($resp,true);

$accessToken=$accessToken_array['access_token'];

//print_r($accessToken);

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_GET , DingTalkConstant::$FORMAT_JSON);
$req = new OapiUserGetUseridByUnionidRequest;
$req->setUnionid($unionid);
$resp=$c->execute($req,$accessToken, "https://oapi.dingtalk.com/user/getUseridByUnionid");
//echo '<br><br>userid<br>';
//var_dump($resp);
$userid=$resp->userid;
$userid->errcode==0?'':die('无该接口的调用权限');

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_GET , DingTalkConstant::$FORMAT_JSON);
$req = new OapiUserGetRequest();
$req->setUserid($userid);// 用户 id
$resp=$c->execute($req, $accessToken,"https://oapi.dingtalk.com/topapi/v2/user/get");
$user_info=$resp;
//echo '<br><br>用户返回信息:';
var_dump($resp);
//echo '<br>========================<br>';
echo '<br><img src="'.$user_info->result->avatar.'" style="width:100px;"/>';
echo '<br> 姓 名: '.$user_info->result->name;
echo '<br>用户id: '.$user_info->result->userid;
echo '<br>手机号: '.$user_info->result->mobile;
echo '<br> 职 位: '.$user_info->result->position;
$user_info->errcode==0?'':die('您不是本企业用户,无法登录系统');

$dingCode=$user_info->result->mobile;

//post登陆
$url = 'http://gugu.vaiwan.com/index.php/admin/base/login'; //POST地址
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'username='.$dingCode.'&dingCode='.$dingCode);
/**
* 如果$url是https则需要取消下面两行注释
* curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
* curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
*/
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$info = curl_exec($curl);
curl_close($curl);
$data = json_decode($info);

输入密码查看加密内容:

虎头财商

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: