Files
jk8_admin/app/common/service/JK8Services.php

115 lines
3.2 KiB
PHP

<?php
namespace app\common\service;
use app\admin\model\Config as ConfigModel;
use think\Exception;
class Jk8Services
{
private $domain = '';
private $accessId = '';
private $token = '';
public function __construct()
{
$config = ConfigModel::where('group', 'jdk_api')->column('value', 'name');
$this->domain = $config['api_url'];
$this->accessId = $config['access_id'];
$this->token = $config['token'];
}
/**
* 构建请求参数
* @param string $method
* @param array $params
* @return array
*/
public function createParam(string $method, array $params): array
{
return [
'module' => $method,
'accessId' => $this->accessId,
'accessToken' => $this->token,
...$params,
];
}
/**
* 用户注册
* @param string $username
* @param string $nickname
* @param string $referrerCode
* @return string
* @throws Exception
*/
public function register(string $username, string $nickname, string $referrerCode): string
{
$params = $this->createParam('/member/register', [
'username' => $username,
// 'name' => $nickname,
'referrerCode' => $referrerCode,
]);
$result = doCurl($this->domain, $params);
if (($result['status'] ?? '') === 'ERROR') {
throw new Exception($result['data']['message'] ?? 'Remote API Error');
}
return $result['data']['id'];
}
/**
* 用户注册
* @param int|null $userId
* @return string
* @throws Exception
*/
public function getAllUsers(int $userId = null): string
{
$params = $this->createParam('/users/getAllUsers', [
'id' => $userId,
]);
$result = doCurl($this->domain, $params);
if (($result['status'] ?? '') === 'ERROR') {
throw new Exception($result['data']['message'] ?? 'Remote API Error');
}
return $result['data']['users'][0]['username'] ?? '';
}
/**
* 获取促销列表
* @param string $userId
* @return array|mixed|null
* @throws Exception
*/
public function getPromotionList(string $userId): mixed
{
$params = $this->createParam('/users/getPromotionList', [
'userId' => $userId,
]);
$result = doCurl($this->domain, $params);
if (($result['status'] ?? '') === 'ERROR') {
throw new Exception($result['data']['message'] ?? 'Remote API Error');
}
return $result;
}
/**
* 转账
* @param string $username
* @param float $amount
* @return array|mixed|null
* @throws Exception
*/
public function setScore(string $username, float $amount): mixed
{
$params = $this->createParam('/member/setScore', [
'username' => $username,
'amount' => $amount
]);
$result = doCurl($this->domain, $params);
if (($result['status'] ?? '') === 'ERROR') {
throw new Exception($result['data']['message'] ?? 'Remote API Error');
}
return $result['data']['transactionId'];
}
}