erp系统对接⽤友T+系统(PHP版本)
对接步骤:
⼀、T+开发社区中⼼注册成为开发者,且申请开发者,T+管理员审核通过后会有appkey、appsecret,⽤于登录接⼝使⽤⼆、对接接⼝有两个版本:v1和v2。
三、安装好⽤友T+系统,并创建账套和账号,(账号在对接的接⼝登录验证会使⽤)
四、到你所需的对应版本的开发⽂档,进⾏开发对接
五、⽤什么账号登录,决定接⼝提交单据的制单⼈(这点需注意)
⽤友T+版本为T+12.3以上的要⽤v2版本的对接接⼝,以下⽤v1版本的对接接⼝
所做的⽤友T+版本为T+12.2版本,⼏年前使⽤v1版本的对接⽂档,代码如下
<?php
//开启开发⽤友系统接⼝
class TPlusProxy{
const Uri = '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;
php手机版下载}
//⽤户账号密码登录
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{
//
$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就在登录机制需要变化,其他接⼝的参数不需变化

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。