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

2021-02-08 16:15

阅读:391

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

对接步骤:

一、T+开发社区中心注册成为开发者,且申请开发者,T+管理员审核通过后会有appkey、appsecret,用于登录接口使用

二、对接接口有两个版本:v1和v2。

三、安装好用友T+系统,并创建账套和账号,(账号在对接的接口登录验证会使用)

四 、找到你所需的对应版本的开发文档,进行开发对接

五、用什么账号登录,决定接口提交单据的制单人(这点需注意)

用友T+版本为T+12.3以上的要用v2版本的对接接口,以下用v1版本的对接接口
所做的用友T+版本为T+12.2版本,几年前使用v1版本的对接文档,代码如下

 $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),代码如下:

 ‘xxxxx‘, // ISV账号的AppKey 
        ‘appsecret‘ => ‘xxxxx‘, // ISV账号的AppSecret 
        ‘cert‘ => ‘D:\xxxxx\xxx\cjet_pri.pem‘, // 申请ISV账号审核通过后下发的pem版证书,使用cjet_pri.pem文件 
        ‘orgid‘ => ‘‘, // 企业云账号 
        ‘authmode‘ => ‘account‘, // 认证模式 account-账套 ecloud-企业云账号模式
        ‘account‘ => [ // 账套账号配置 
            ‘id‘ => ‘‘, // 账套账号ID   账号名
            ‘password‘ => ‘‘, // 账套账号密码   账号密码
            ‘number‘ => ‘001‘, // 账套编号 
        ],
    ];
    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


评论


亲,登录后才可以留言!