Files
xk-api-yii/common/services/PlatformService.php
2024-09-19 11:32:39 +08:00

116 lines
3.8 KiB
PHP

<?php
namespace common\services;
use GuzzleHttp\Client;
use yii\base\Exception;
use yii\helpers\Json;
class PlatformService
{
public $url = '';
public $token = '';
protected static $guzzleOptions = ['http_errors' => false];
public function __construct($config = [])
{
$this->url = \Yii::$app->params['platform']['url'];
$this->token = \Yii::$app->params['platform']['token'];
if(!$this->url || !$this->token){
throw new Exception('平台配置错误');
}
}
/**
* Get a fresh instance of the Guzzle HTTP client.
*
* @return \GuzzleHttp\Client
*/
protected function getHttpClient()
{
return new Client(self::$guzzleOptions);
}
// 用户登录
public function userLogin($data){
$response = (new Client(['http_errors' => false, 'verify' => false]))->post($this->url."/platform/v1/user/login", [
'headers' => ['Authorization' =>"Bearer ".$this->token],
'form_params' => [
'user_id' => $data['user_id'],
'openid' => $data['openid'],
'session_key' => $data['session_key'],
'mobile' => $data['mobile'] ?? '',
'nickname' => $data['nickname'] ?? '微信用户',
'avatarurl' => $data['avatarurl'] ?? '',
'platform_store_id' => $data['store_id']
],
]);
$result = json_decode($response->getBody(),true);
if ($result['errcode'] == -1) {
throw new Exception($result['msg']);
}
return ['token' => $result['data']['token']];
}
// 订单支付、取消或退款
public function updateOrder($data){
$response = (new Client(['http_errors' => false, 'verify' => false]))->post($this->url."/platform/v1/sync/product-order", [
'headers' => ['Authorization' =>"Bearer ".$this->token],
'form_params' => [
'platform_store_id' => $data['store_id'],
'order_no' => $data['order_no'],
'status' => $data['status'],
],
]);
$result = json_decode($response->getBody(),true);
if ($result['errcode'] == -1) {
throw new Exception($result['msg']);
}
return true;
}
public function forwardNotify($data, $type = 'pay'){
$response = (new Client(['http_errors' => false, 'verify' => false]))->post($this->url."/platform/v1/order/notify", [
'headers' => ['Authorization' =>"Bearer ".$this->token],
'json' => Json::encode([
'type' => $type,
'notify' => $data
]),
]);
$result = json_decode($response->getBody(),true);
if ($result['errcode'] == -1) {
throw new Exception($result['msg']);
}
return true;
}
public function syncDrug($data){
$response = (new Client(['http_errors' => false, 'verify' => false]))->post($this->url."/platform/v1/sync/drug", [
'headers' => ['Authorization' =>"Bearer ".$this->token],
'form_params' =>$data,
]);
$result = json_decode($response->getBody(),true);
if ($result['errcode'] == -1) {
return false;
}
return true;
}
//问诊信息
// public function consultInfo($orderNo){
// $response = $this->getHttpClient()->post($this->url."/platform/v1/order/info", [
// 'headers' => ['Authorization' =>"Bearer ".$this->token],
// 'form_params' => [
// 'order_no' => $orderNo
// ],
// ]);
// $result = json_decode($response->getBody(),true);
// if ($result['errcode'] == -1) {
// throw new Exception($result['msg']);
// }
// return $result;
// }
}