123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?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());
- }
- }
- }
|