對接步驟:
一、T+開發社區中心注冊成為開發者,且申請開發者,T+管理員審核通過后會有appkey、appsecret,用於登錄接口使用
二、對接接口有兩個版本:v1和v2。
三、安裝好用友T+系統,並創建賬套和賬號,(賬號在對接的接口登錄驗證會使用)
四 、找到你所需的對應版本的開發文檔,進行開發對接
五、用什么賬號登錄,決定接口提交單據的制單人(這點需注意)
用友T+版本為T+12.3以上的要用v2版本的對接接口,以下用v1版本的對接接口
所做的用友T+版本為T+12.2版本,幾年前使用v1版本的對接文檔,代碼如下
<?php
//開啟開發用友系統接口
class TPlusProxy{
const Uri = 'http://xxxxx:8800/'; //需配置項 請求地址(用友T+的地址) 不能刪除最后的反斜杠
const appKey = 'xxxx'; //需配置項 appKey
const appSecret = 'xxxxx'; //需配置項 appSecret
public $userName = ''; //需配置項 登錄賬號
public $passWord = ''; //需配置項 登錄密碼
const AccountNumber = '003'; //賬套號(T+系統里創建的)
//構造登錄通行證
private static function AuthSign($uri,$token=''){
$param = array('uri'=> $uri,'access_token'=>$token,'date'=>gmdate('l, d M Y H:i:s').' GMT');
$authinfo = base64_encode(hash_hmac("sha1", stripslashes(json_encode($param)), self::appSecret, true));
$auth = array(
'appKey'=>self::appKey,
'authInfo'=>'hmac-sha1 '.$authinfo,
'paramInfo'=>$param
);
$Authorization = base64_encode(stripslashes(json_encode($auth)));
return $Authorization;
}
//token登錄
private function tokenPost($uri,$token,$args=array()){
$Authorization = self::AuthSign($uri,$token);
$header = array(
"Content-type:application/x-www-form-urlencoded;charset=utf-8",
"Authorization:$Authorization",
);
$ch = curl_init(); //開啟會話信息
curl_setopt($ch, CURLOPT_URL, $uri); //設置會話傳輸信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($args));
$response = curl_exec($ch); //執行會話
curl_close($ch);
return $response;
}
//用戶賬號密碼登錄
private function post($uri,$args=array()){
$Authorization = self::AuthSign($uri);
$header = array(
"Content-type:application/x-www-form-urlencoded;charset=utf-8",
"Authorization:$Authorization",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($args));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
//開放外面開發接口
/*
* $uri : 請求地址
* $args : 傳遞數據
*/
public function Open($uri,$args){
//生成token
// date_default_timezone_set('Asia/Shanghai');
$data= [];
$password = base64_encode(md5($this->passWord,true));
$tokenArgs = array('UserName'=>$this->userName,'Password'=>$password,'AccountNumber'=>self::AccountNumber,'LoginDate'=>date('Y-m-d', time()));
$res = json_decode($this->post(self::Uri . 'TPlus/api/v1/Authorization',array("_args"=>json_encode($tokenArgs))));
if(!empty($res->result)){
$token = $res->access_token;
}else{
if($res->code='EXSM0004'){
//用戶已登錄,需要調用重新登錄接口
$token = $res->data;
$res = json_decode($this->tokenPost(self::Uri . 'TPlus/api/v1/Authorization/ReLogin',$token));
if(!isset($res->access_token)){
$data['status'] = false;
$data['message'] = '連接失敗';
return $data;
}
$token = $res->access_token;
}else{
//something wrong......
$data['status'] = false;
$data['message'] = '連接失敗';
return $data;
}
}
$arr = json_decode($this->tokenPost(self::Uri . $uri,$token,array('_args'=>json_encode($args))));
if(isset($arr->message)){ //出錯
$data['status'] = false;
$data['message'] = $arr->message;
return $data;
} else {
$data['status'] = true;
$data['message'] = $arr;
return $data;
}
}
}
近期升級用友T+系統到13.0以上版本,故以前對接的v1版本已不適用,現采用v2版本對接文檔(需從開發者中心下載v2的sdk),代碼如下:
<?php
// +----------------------------------------------------------------------
// | TPlusOAuth SDK V2[演示代碼]
// +----------------------------------------------------------------------
// | Licensed
// +----------------------------------------------------------------------
// | Author:
// +----------------------------------------------------------------------
use vendor\sdk\tplus\api;
include_once './vendor/sdk/tplus/api.php'; //引用用友的sdk文件
class TPlusOAuth{
public $userName = ''; //需配置項 登錄賬號
public $passWord = ''; //需配置項 登錄密碼
# 配置參數
private $options = [
'appkey' => 'xxxxx', // ISV賬號的AppKey <必須>
'appsecret' => 'xxxxx', // ISV賬號的AppSecret <必須>
'cert' => 'D:\xxxxx\xxx\cjet_pri.pem', // 申請ISV賬號審核通過后下發的pem版證書,使用cjet_pri.pem文件 <必須>
'orgid' => '', // 企業雲賬號 <非賬套模式必須,即authmode=ecloud>
'authmode' => 'account', // 認證模式 account-賬套 ecloud-企業雲賬號模式
'account' => [ // 賬套賬號配置 <account模式下必須>
'id' => '', // 賬套賬號ID <account模式下必須> 賬號名
'password' => '', // 賬套賬號密碼 <account模式下必須> 賬號密碼
'number' => '001', // 賬套編號 <account模式下必須>
],
];
public function Open($url,$args,$setFields=''){
$authorizationHeader = '';
# 實例化
$this->options['account']['id'] = $this->userName;
$this->options['account']['password'] = $this->passWord;
$tplusAPI = new api($this->options);
# 創建授權報頭(鑒權)
$tplusAPI::createAuthorizationHeader($authorizationHeader);
# 創建訪問令牌
$tplusAPI::createAccessToken($authorizationHeader);
# 創建授權報頭(業務)
$tplusAPI::createAuthorizationHeader($authorizationHeader);
# 業務演示
$tplusAPI::setAPIUrl('/'.$url);
if(!empty($setFields)){
$tplusAPI::setFields($setFields);
}
$apiParam = [
'_args' => json_encode($args, JSON_UNESCAPED_UNICODE)
];
$tplusAPI::post($authorizationHeader, $arr, $apiParam);
// var_dump($arr); exit;
if(isset($arr->message)){ //出錯
$data['status'] = false;
$data['message'] = $arr->message;
return $data;
} else {
$data['status'] = true;
$data['message'] = $arr;
return $data;
}
}
}
?>
總結:從v1接口升級到v2就在登錄機制需要變化,其他接口的參數不需變化
