當前位置:首頁 » 編程語言 » php自動注冊

php自動注冊

發布時間: 2024-01-01 17:15:39

php怎麼獲取釘釘員工授權信息

做過一個E應用,使用lumen框架,和你的思路是一樣的,新用戶點進去就自動授權注冊應用,數據存到我們自己的資料庫中,不依賴釘釘,我們還同步了部門信息,如果粘貼復制和下面的那個同學一樣,看上去你也會覺得懵,方法都是封裝好了的。

建議你這樣試試看:

  1. 獲取AccessToken:

    後端通過corpid,corpsecret請求介面gettoken?corpid=id&corpsecret=secrect獲取AccessToken

  2. 獲取釘釘用戶userid:

    前端需要相應的處理,攜帶authCode請求,加上AccessToken這兩個參數請求介面/user/getuserinfo?access_token=access_token&code=authCode這個

  3. 獲取釘釘用戶詳情:

    使用access_token和上一步的釘釘userid 請求介面 /user/get?access_token=ACCESS_TOKEN&userid=

  4. 插入釘釘用戶的數據到你的 資料庫中

我們這樣做的:

/**

* 釘釘免登陸獲獲取用信息

* @param $authCode

* @param $url

* @return array

*/

static function outhLogin($authCode, $url)

{

if (empty($authCode) || empty($url)) {

return self::returnError('1101', self::$errorArray['1101']);

}

$accessToken = ComponentDingtalk::getPcAccessToken();

if ($accessToken['code']) {

self::logError(__CLASS__ . '->' . __FUNCTION__, '獲取access_token失敗');

return self::returnError('1102', self::$errorArray['1102']);

}

$dingUserId = ComponentDingtalk::getDingUserid($accessToken['data'], $authCode);

if ($dingUserId['code']) {

self::logError(__CLASS__ . '->' . __FUNCTION__, '用戶userid獲取失敗(調用釘釘API)');

return self::returnError('1103', self::$errorArray['1103']);

}

$dinguserInfo = ComponentDingtalk::getDingUserInfo($accessToken['data'], $dingUserId['data']);

if ($dinguserInfo['code']) {

self::logError(__CLASS__ . '->' . __FUNCTION__, '用戶信息獲取失敗(調用釘釘API)');

return self::returnError('1104', self::$errorArray['1004']);

}

$userInfo = $dinguserInfo['data'];

return self::transaction(function () use ($accessToken, $userInfo, $url) {

if (count($userInfo['department']) > 1) {

$departIdArr = [];

$departNameArr = [];

for ($i = 0, $iMax = count($userInfo['department']); $i < $iMax; $i++) {

$departInfo[$i] = ServerDepartment::getByDdDepartid($userInfo['department'][$i]);

$departIdArr[] = $departInfo[$i]['id'];

$departNameArr[] = $departInfo[$i]['name'];

}

$depart['id'] = implode(',', $departIdArr);

$depart['name'] = implode(',', $departNameArr);

} else {

$ddDepartmentId = implode(',', $userInfo['department']);

$depart = ServerDepartment::getByDdDepartid($ddDepartmentId);

}

//插入用戶

$user = ServerEmployee::getByDdUserid($userInfo['userid']);

if ($user && $user['status'] == 2) {

return self::returnError('1105', self::$errorArray['1105']);

}

if (empty($user)) {

$roleId = 0;

$departId = $depart['id'];

$name = $userInfo['name'];

$mobile = $userInfo['mobile'];

$departName = $depart['name'];

$position = $userInfo['position'];

$ddUserid = $userInfo['userid'];

$ddStatus = $userInfo['active'] ? 1 : 2;

$ddInfo = json_encode($userInfo, JSON_UNESCAPED_UNICODE);

$tokenOverAt = (int)(time() + $_ENV['PROJECT_apiAppTokenOverTime']);

$token = self::_createToken($userInfo['userid'], $tokenOverAt);

$status = 1;

$userId = ServerEmployee::insert($roleId, $departId, $name, $mobile, $departName, $position, $ddUserid, $ddStatus, $ddInfo, $token, $tokenOverAt, $status);

if (!$userId) {

self::logError(__CLASS__ . '->' . __FUNCTION__, '用戶初始化創建失敗');

return self::returnError('1106', self::$errorArray['1106']);

}

}

$userId = $userId ?? $user['id'];

// 更新Token

$id = $userId;

$roleId = $user['roleId'];

$departId = $depart['id'];

$name = $userInfo['name'];

$mobile = $userInfo['mobile'];

$departName = $depart['name'];

$position = $userInfo['position'];

$ddUserid = $userInfo['userid'];

$ddStatus = $userInfo['active'] ? 1 : 2;

$ddInfo = json_encode($userInfo, JSON_UNESCAPED_UNICODE);

$tokenOverAt = (int)(time() + $_ENV['PROJECT_apiAppTokenOverTime']);

$token = self::_createToken($userInfo['userid'], $tokenOverAt);

$status = 1;

$updateParams = ServerEmployee::update($id, $roleId, $departId, $name, $mobile, $departName, $position, $ddUserid, $ddStatus, $ddInfo, $token, $tokenOverAt, $status);

if (!$updateParams) {

self::logError(__CLASS__ . '->' . __FUNCTION__, '用戶信息更新失敗' . json_encode($updateParams, JSON_UNESCAPED_UNICODE) . '/' . json_encode([$id, $roleId, $departId, $name, $mobile, $depart, $position, $ddUserid, $ddStatus, $ddInfo, $token, $tokenOverAt, $status]));

return self::returnError('1107', self::$errorArray['1107']);

}

// 前端的配置信息

// 獲取jsTicket

$jsTicket = ComponentDingtalk::getPcJsTicket($accessToken['data']);

if ($jsTicket['code']) {

self::logError(__CLASS__ . '->' . __FUNCTION__, '獲取jsTicket失敗(調用釘釘API)');

return self::returnError('1111', self::$errorArray['1111']);

}

// 組裝簽名數據

$curUrl = $url;;

$nonceStr = uniqid('', true);

$agentId = $_ENV['PROJECT_ddInterfaceAgentID'];

$timeStamp = time();

$corpId = $_ENV['PROJECT_ddInterfaceCorpId'];

$signature = ComponentDingtalk::getSign($jsTicket['data'], $nonceStr, $timeStamp, $curUrl);

$config = array(

'url' => urldecode($curUrl),

'nonceStr' => $nonceStr,

'agentId' => $agentId,

'timeStamp' => $timeStamp,

'corpId' => $corpId,

'signature' => $signature

);

// 獲取當前角色的許可權

$roleInfo = ServerRole::getById($roleId);


// 當前用戶的頂級部門(不含根部門)

$departInfo = ServerDepartment::getById($departId);

if ($departInfo['parentid'] == 1) { // 二級部門(總經辦)

$departRootId = $departId;

$departRootName = $departName;

} else {

$sonDepart = ServerDepartment::getById($departInfo['parentid']);//分組

if ($sonDepart['parentid'] == 1) {

$departRootId = $sonDepart['id'];

$departRootName = $sonDepart['name'];

} else {

$grandsonDepart = ServerDepartment::getById($sonDepart['parentid']);//部門

if ($grandsonDepart['parentid'] == 1) {

$departRootId = $grandsonDepart['id'];

$departRootName = $grandsonDepart['name'];

} else {

$grandchildDepart = ServerDepartment::getById($grandsonDepart['parentid']);//分公司

$departRootId = $grandchildDepart['id'];

$departRootName = $grandchildDepart['name'];

}

}

}

$company = ServerDepartment::get(['parentid' => 0, 'dd_departid' => 1]);


return self::returnSuccess(array(

'id' => $userId,

'name' => $name,

'token' => $token,

'tokenOverAt' => $tokenOverAt,

'config' => $config,

'power' => $roleInfo['power'] ?? '',

'departId' => $departId,

'departName' => $departName,

'departRootId' => $departRootId,

'departRootName' => $departRootName,

'company' => $company['name'],


));

}, function (Exception $e) {

echo $e->getMessage();

self::logError(__CLASS__ . '->' . __FUNCTION__, $e->getMessage());

return self::returnError('1108', self::$errorArray['1108']);

});

}

熱點內容
如何知道老婆微信和密碼 發布:2024-11-30 06:46:16 瀏覽:848
java計劃 發布:2024-11-30 06:44:04 瀏覽:942
linux查看ftp日誌 發布:2024-11-30 06:33:19 瀏覽:475
設置截屏存儲 發布:2024-11-30 06:29:00 瀏覽:394
jpg演算法 發布:2024-11-30 06:28:55 瀏覽:195
怎麼刪除u盤中的文件夾 發布:2024-11-30 06:28:20 瀏覽:216
iphone文件夾打開 發布:2024-11-30 06:13:43 瀏覽:298
如何配置Javaweb環境 發布:2024-11-30 06:09:24 瀏覽:121
怎麼使用Androidapi 發布:2024-11-30 06:08:43 瀏覽:61
包鋼伺服器地址 發布:2024-11-30 06:06:27 瀏覽:563