码迷,mamicode.com
首页 > Web开发 > 详细

erp系统对接用友T+系统(PHP版本)

时间:2020-06-09 16:14:35      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:some   决定   orm   php   res   文件   span   好用   auth   

对接步骤:

一、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就在登录机制需要变化,其他接口的参数不需变化

erp系统对接用友T+系统(PHP版本)

标签:some   决定   orm   php   res   文件   span   好用   auth   

原文地址:https://www.cnblogs.com/TreeSky/p/13073241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!