168 lines
5.8 KiB
PHP
168 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\controller\Api;
|
|
use app\common\model\soft\Codes;
|
|
use app\common\model\soft\Devices;
|
|
use think\Db;
|
|
use think\exception\HttpResponseException;
|
|
use think\exception\PDOException;
|
|
use think\exception\ValidateException;
|
|
|
|
/**
|
|
* 首页接口
|
|
*/
|
|
class Soft extends Api
|
|
{
|
|
protected $noNeedLogin = ['*'];
|
|
protected $noNeedRight = ['*'];
|
|
|
|
/**
|
|
* 首页
|
|
*
|
|
*/
|
|
public function index()
|
|
{
|
|
$this->success('请求成功');
|
|
}
|
|
|
|
public function activate()
|
|
{
|
|
try {
|
|
$params = $this->request->post();
|
|
$error = $this->validate(
|
|
$params,
|
|
[
|
|
'device_id' => 'require|max:255',
|
|
'activate_code' => 'require|max:255',
|
|
],
|
|
[
|
|
'device_id.require' => '设备ID缺失',
|
|
'activate_code.require' => '激活码必填',
|
|
]);
|
|
if (true !== $error) {
|
|
throw new \Exception($error);
|
|
}
|
|
$device_id = $params['device_id'];
|
|
$activate_code = $params['activate_code'];
|
|
Db::startTrans();
|
|
try {
|
|
$codeInfo = Codes::withCount('devices')->where(['activate_code' => $activate_code])->find();
|
|
if (!$codeInfo) {
|
|
throw new \Exception('激活码无效');
|
|
}
|
|
if ($codeInfo->status == 2) {
|
|
throw new \Exception('该激活码已失效');
|
|
}
|
|
$deviceInfo = Devices::get(['activate_code' => $activate_code, 'device_id' => $device_id]);
|
|
if ($deviceInfo) {
|
|
Db::commit();
|
|
$this->success('该设备已经激活');
|
|
} else {
|
|
if ($codeInfo->devices_count >= 2) {
|
|
throw new \Exception('该激活码绑定的设备已达上限');
|
|
}
|
|
}
|
|
$params['activated_time'] = time();
|
|
$params['expiration_time'] = $codeInfo->valid_days == 0 ? 0 : strtotime("+{$codeInfo->valid_days} days");
|
|
$result = (new Devices)->allowField(true)->save($params);
|
|
if ($result === false) {
|
|
throw new \Exception('激活失败');
|
|
}
|
|
if ($codeInfo->status == 0) {
|
|
$codeInfo->status = 1;
|
|
$codeInfo->save();
|
|
}
|
|
Db::commit();
|
|
} catch (ValidateException|PDOException|Exception $e) {
|
|
Db::rollback();
|
|
throw new \Exception($e->getMessage());
|
|
}
|
|
$this->success('激活成功');
|
|
} catch (HttpResponseException $e) {
|
|
throw $e;
|
|
} catch (\Exception $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function cancelActivate()
|
|
{
|
|
try {
|
|
$params = $this->request->post();
|
|
$error = $this->validate(
|
|
$params,
|
|
[
|
|
'device_id' => 'require|max:255',
|
|
'activate_code' => 'require|max:255',
|
|
],
|
|
[
|
|
'device_id.require' => '设备ID缺失',
|
|
'activate_code.require' => '激活码必填',
|
|
]);
|
|
if (true !== $error) {
|
|
throw new \Exception($error);
|
|
}
|
|
$device_id = $params['device_id'];
|
|
$activate_code = $params['activate_code'];
|
|
Db::startTrans();
|
|
try {
|
|
$codeInfo = Codes::where(['activate_code' => $activate_code])->find();
|
|
if (!$codeInfo) {
|
|
throw new \Exception('激活码无效');
|
|
}
|
|
if ($codeInfo->status == 2) {
|
|
throw new \Exception('该激活码已失效');
|
|
}
|
|
$deviceInfo = Devices::get(['activate_code' => $activate_code, 'device_id' => $device_id]);
|
|
if (!$deviceInfo) {
|
|
throw new \Exception('该激活码未绑定设备');
|
|
}
|
|
$deviceInfo->activate_code = '';
|
|
$result = $deviceInfo->save();
|
|
if ($result === false) {
|
|
throw new \Exception('解绑失败');
|
|
}
|
|
Db::commit();
|
|
} catch (ValidateException|PDOException|Exception $e) {
|
|
Db::rollback();
|
|
throw new \Exception($e->getMessage());
|
|
}
|
|
$this->success('解绑成功');
|
|
} catch (HttpResponseException $e) {
|
|
throw $e;
|
|
} catch (\Exception $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function checkActivated()
|
|
{
|
|
try {
|
|
$params = $this->request->get();
|
|
$device_id = $params['device_id'] ?? '0';
|
|
$activate_code = $params['activate_code'] ?? '0';
|
|
$codeInfo = Codes::where(['activate_code' => $activate_code])->find();
|
|
if (!$codeInfo) {
|
|
throw new \Exception('激活码无效');
|
|
}
|
|
if ($codeInfo->status == 2) {
|
|
throw new \Exception('激活码已失效,请重新激活');
|
|
}
|
|
$deviceInfo = Devices::get(['activate_code' => $activate_code, 'device_id' => $device_id]);
|
|
if (!$deviceInfo) {
|
|
throw new \Exception('未激活');
|
|
}
|
|
if ($deviceInfo->expiration_time > 0 && $deviceInfo->expiration_time < time()) {
|
|
throw new \Exception('激活已过期,请重新激活');
|
|
}
|
|
$this->success('已激活');
|
|
} catch (HttpResponseException $e) {
|
|
throw $e;
|
|
} catch (\Exception $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|