×

挺齐全的PHP 金碟WEBAPI接口 基于TP6开发

天外来信 天外来信 发表于2021-10-06 18:25:53 浏览16963 评论0

抢沙发发表评论

最近做金碟二开,写了一个金碟接口,代码比较简练。


有需要的可以拿去用。


配置文件:

/config/kingdee.php

<?php
// +----------------------------------------------------------------------
// | 金碟接口
// +----------------------------------------------------------------------

return [
	//自己金蝶的域名或者是IP地址/K3Cloud/
	'cloudUrl'=>'https://xxx.com/k3cloud/',
	//array('套账id','账号','密码',2052);
	'login'=>array('222','Administrator','xxxx',2052),
];


文件名:

/extend/frey/service/KingdeeService.php

<?php

//金碟WebApi接口

namespace frey\service;
use think\facade\Cache;

class KingdeeService
{
	 private static $APIURL=[
		 'login'=>'Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc',//登录
		 'view'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc',//查看
		 'save'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc',//保存
		 'batchsave'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.BatchSave.common.kdsvc',//批量保存
		 'submit'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc',//提交表单
		 'audit'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc',//审核
		 'unaudit'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc',//反审核
		 'delete'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc',//删除
		 'executebillquery'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc',//表单查询
		 'draft'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Draft.common.kdsvc',//暂存表单数据
		 'allocate'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Allocate.common.kdsvc',//分配表单
		 'push'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Push.common.kdsvc',//下推
		 'groupsave'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Push.common.kdsvc',//分组保存
		 'querybusinessinfo'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.QueryBusinessInfo.common.kdsvc',//查询元数据
		 'loginkill'=>'Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser2.common.kdsvc',//登录验证接口带踢人功能
		 'flexsave'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.FlexSave.common.kdsvc',//弹性域保存接口
		 'sendmsg'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.SendMsg.common.kdsvc',//发送消息接口
		 'logout'=>'Kingdee.BOS.WebApi.ServicesStub.AuthService.Logout.common.kdsvc',//空操作接口
		 'executeoperation'=>'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteOperation.common.kdsvc',//登出接口
	 ];
	//登陆
	public static function login()
	{
		$post_content=config('kingdee.login');
		$loginurl = self::$APIURL['login'];
		return self::invoke_post($loginurl,$post_content,TRUE);
	}
	//API接口
	public static function api($act,$data)
	{
		$invokeurl = self::$APIURL[$act];
		return self::invoke_post($invokeurl,$data,FALSE);
	}

	//提交数据
	public static function invoke_post($url,$data,$isLogin)
	{
		$kingdeeurl=config('kingdee.cloudUrl').$url;
		$ch = curl_init($kingdeeurl);
		$post_content = self::create_postdata($data);
		$this_header = array(
			'Content-Type: application/json',
			'Content-Length: '.strlen($post_content)
		);

		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		if($isLogin){
			curl_setopt($ch, CURLOPT_COOKIEJAR, root_path().'runtime/session/kingdee.cookie');
		}
		else{
			curl_setopt($ch, CURLOPT_COOKIEFILE, root_path().'runtime/session/kingdee.cookie');
		}
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		
		$result = curl_exec($ch); 
		curl_close($ch);  

		return json_decode($result,1);
	}
	//构造Web API请求格式
	public static function create_postdata($args) {
		$postdata = array(
		'format'=>1,
		'useragent'=>'ApiClient',
		'rid'=>self::create_guid(),
		'parameters'=>$args,
		'timestamp'=>date('Y-m-d'),
		'v'=>'1.0'
		);
		return json_encode($postdata);
	}

	//生成guid
	public static function create_guid() {
		$charid = strtoupper(md5(uniqid(mt_rand(), true)));
		$hyphen = chr(45);// "-"
		$uuid = chr(123)// "{"
		.substr($charid, 0, 8).$hyphen
		.substr($charid, 8, 4).$hyphen
		.substr($charid,12, 4).$hyphen
		.substr($charid,16, 4).$hyphen
		.substr($charid,20,12)
		.chr(125);// "}"
		return $uuid;
	}

}


//测试用例:

<?php
/*
 module:		测试功能
 create_time:	2021-07-10 12:37:02
 author:		
 contact:		
*/

namespace app\admin\controller\Test;

class Test extends Admin
{


    /*首页数据列表*/
    function index()
    {
            //登录金碟
    		$res=\frey\service\KingdeeService::login();
    		$map=array('{"FormId": "BD_MATERIAL"}');
    		//获取表单元数据
    		$title=\frey\service\KingdeeService::api('querybusinessinfo',$map);
    		var_dump($title);
    		//获取100个物料
    		$json='{"FormId": "BD_MATERIAL","FieldKeys": "FName,FNumber","FilterString": "","OrderString": "","TopRowCount": 0,"StartRow": 0,"Limit": 100}';
    		$map=array('BD_MATERIAL',$json);
    		$title=\frey\service\KingdeeService::api('executebillquery',$map);
    		var_dump($title);
      }
}


评论列表

访客