PHP对接管家erp系统获取相应令牌
最编程
2024-04-30 19:14:54
...
首先是设置对应的的管家婆erp所需要的相关信息
'online'=>[
'appkey' =>'appkey',
'app_secret'=>'app_secret',
'sign_key'=>'sign_key',
'grt_auth_code'=>'登录链接',
'get_token'=>'获取token链接',
'get_refresh_token'=>'获取刷新token链接',
'api_url'=>'api链接',
'shopkey'=>'商城秘钥'
],
登录管家婆erp获取对应的code并将其转换成我们需要的token
public function authCode($code)
{
// $auth_code=$param['auth_code'];
$config = config('erp.online');
// $iv = mb_substr(trim($config['app_secret']),5,16);//获取偏移量
// $eas->iv = $iv;
// $eas->encryptKey = trim($config['app_secret']);//转换密码
$params = array(
'TimeStamp' => date('Y-m-d H:i:s', time()),
'GrantType' => 'auth_token',
'AuthParam' => trim($code)
);
$p = $this->encrypt(trim(json_encode($params, JSON_UNESCAPED_SLASHES)));
$params = array('appkey' => trim($config['appkey']), 'p' => $p, 'signkey' => trim($config['sign_key']));
$sign = trim($this->sha256(json_encode($params, JSON_UNESCAPED_SLASHES)));
$sign = trim(urlencode($sign));
$post_data['p'] = trim(urlencode($p));//签名
$post_data['sign'] = $sign;
$post_data['appkey'] = trim($config['appkey']);//key
$ret_json = $this->post($config['get_token'], $post_data);
$authStr = $ret_json['response']['response'];
$tokenStr = $this->decrypt($authStr);
$result = stripslashes($tokenStr);
$arr = [];
$preg = '/(?<={).*?(?=})+/';
preg_match_all($preg, $result, $arr);
$arr_result = '{' . $arr['0']['0'] . '}';
$result = json_decode($arr_result, true);
// trace('获得token:' . print_r($tokenStr, true), 'zyx');
return $result;
}
//sha256算法
public function sha256($data, $rawOutput = false)
{
if (!is_scalar($data)) {
return false;
}
$data = (string)$data;
$rawOutput = !!$rawOutput;
return hash('sha256', $data, $rawOutput);
}
//加密
public function encrypt($encryptStr)
{
$config = config('erp.online');
$localIV = $this->iv;
$encryptKey = trim($config['app_secret']);
$encryptStr = trim($encryptStr);
var_dump($encryptStr);
//Open module
$module = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV);
//print "module = $module <br/>" ;
@mcrypt_generic_init($module, $encryptKey, $localIV);
//Padding
$block = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad
$encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples
//encrypt
$encrypted = @mcrypt_generic($module, $encryptStr);
//Close
@mcrypt_generic_deinit($module);
@mcrypt_module_close($module);
return base64_encode($encrypted);
}
//解密
public function decrypt($encryptStr)
{
$config = config('erp.online');
$localIV = $this->iv;
$encryptKey = trim($config['app_secret']);
//Open module
$module = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV);
//print "module = $module <br/>" ;
@mcrypt_generic_init($module, $encryptKey, $localIV);
$encryptedData = base64_decode($encryptStr);
$encryptedData = @mdecrypt_generic($module, $encryptedData);
return $encryptedData;
$decode = @openssl_decrypt(base64_decode($encryptStr), "AES-128-CBC", $encryptKey, true, $localIV);
return $decode;
}
public function post($url, $data)
{
$str = '';
foreach ($data as $k => $v) {
$str .= $k . '=' . $v . '&';
}
$str = trim($str, '&');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, FALSE);//不抓取头部信息。只返回数据
curl_setopt($curl, CURLOPT_TIMEOUT, 3000);//超时设置
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);//1表示不返回bool值
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//重点
curl_setopt($curl, CURLOPT_POSTFIELDS, $str);
$response = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
$result = json_decode($response, true, 512, JSON_BIGINT_AS_STRING);
return $result;
}
public function code()
{
$params = $this->request->param();
$auth = $this->authCode($params['auth_code']);
$data = [
'code' => $params['auth_code'],
'createtime' => time(),
'updatetime' => time(),
'auth_token' => $auth['auth_token'],
'profileid' => $auth['profileid'],
'employee_id' => $auth['employee_id'],
'expires_in' => $auth['expires_in'],
'refresh_token' => $auth['refresh_token'],
're_expires_in' => $auth['re_expires_in'],
'timestamp' => $auth['timestamp']
];
db('code')->insert($data);
}
获取到对应的token之后我们就可以根据管家婆提供的文档使用token来唤醒不同的接口了
上一篇: 快速分析:NAT Mobile 与巴特勒使用操作相结合
下一篇: 巴特勒的二次开发分析
推荐阅读