Soft.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\soft\Codes;
  5. use app\common\model\soft\Devices;
  6. use think\Db;
  7. use think\exception\HttpResponseException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. /**
  11. * 首页接口
  12. */
  13. class Soft extends Api
  14. {
  15. protected $noNeedLogin = ['*'];
  16. protected $noNeedRight = ['*'];
  17. /**
  18. * 首页
  19. *
  20. */
  21. public function index()
  22. {
  23. $this->success('请求成功');
  24. }
  25. public function activate()
  26. {
  27. try {
  28. $params = $this->request->post();
  29. $error = $this->validate(
  30. $params,
  31. [
  32. 'device_id' => 'require|max:255',
  33. 'activate_code' => 'require|max:255',
  34. ],
  35. [
  36. 'device_id.require' => '设备ID缺失',
  37. 'activate_code.require' => '激活码必填',
  38. ]);
  39. if (true !== $error) {
  40. throw new \Exception($error);
  41. }
  42. $device_id = $params['device_id'];
  43. $activate_code = $params['activate_code'];
  44. Db::startTrans();
  45. try {
  46. $codeInfo = Codes::withCount('devices')->where(['activate_code' => $activate_code])->find();
  47. if (!$codeInfo) {
  48. throw new \Exception('激活码无效');
  49. }
  50. if ($codeInfo->status == 2) {
  51. throw new \Exception('该激活码已失效');
  52. }
  53. $deviceInfo = Devices::get(['activate_code' => $activate_code, 'device_id' => $device_id]);
  54. if ($deviceInfo) {
  55. Db::commit();
  56. $this->success('该设备已经激活');
  57. } else {
  58. if ($codeInfo->devices_count >= 2) {
  59. throw new \Exception('该激活码绑定的设备已达上限');
  60. }
  61. }
  62. $params['activated_time'] = time();
  63. $params['expiration_time'] = $codeInfo->valid_days == 0 ? 0 : strtotime("+{$codeInfo->valid_days} days");
  64. $result = (new Devices)->allowField(true)->save($params);
  65. if ($result === false) {
  66. throw new \Exception('激活失败');
  67. }
  68. if ($codeInfo->status == 0) {
  69. $codeInfo->status = 1;
  70. $codeInfo->save();
  71. }
  72. Db::commit();
  73. } catch (ValidateException|PDOException|Exception $e) {
  74. Db::rollback();
  75. throw new \Exception($e->getMessage());
  76. }
  77. $this->success('激活成功');
  78. } catch (HttpResponseException $e) {
  79. throw $e;
  80. } catch (\Exception $e) {
  81. $this->error($e->getMessage());
  82. }
  83. }
  84. public function cancelActivate()
  85. {
  86. try {
  87. $params = $this->request->post();
  88. $error = $this->validate(
  89. $params,
  90. [
  91. 'device_id' => 'require|max:255',
  92. 'activate_code' => 'require|max:255',
  93. ],
  94. [
  95. 'device_id.require' => '设备ID缺失',
  96. 'activate_code.require' => '激活码必填',
  97. ]);
  98. if (true !== $error) {
  99. throw new \Exception($error);
  100. }
  101. $device_id = $params['device_id'];
  102. $activate_code = $params['activate_code'];
  103. Db::startTrans();
  104. try {
  105. $codeInfo = Codes::where(['activate_code' => $activate_code])->find();
  106. if (!$codeInfo) {
  107. throw new \Exception('激活码无效');
  108. }
  109. if ($codeInfo->status == 2) {
  110. throw new \Exception('该激活码已失效');
  111. }
  112. $deviceInfo = Devices::get(['activate_code' => $activate_code, 'device_id' => $device_id]);
  113. if (!$deviceInfo) {
  114. throw new \Exception('该激活码未绑定设备');
  115. }
  116. $deviceInfo->activate_code = '';
  117. $result = $deviceInfo->save();
  118. if ($result === false) {
  119. throw new \Exception('解绑失败');
  120. }
  121. Db::commit();
  122. } catch (ValidateException|PDOException|Exception $e) {
  123. Db::rollback();
  124. throw new \Exception($e->getMessage());
  125. }
  126. $this->success('解绑成功');
  127. } catch (HttpResponseException $e) {
  128. throw $e;
  129. } catch (\Exception $e) {
  130. $this->error($e->getMessage());
  131. }
  132. }
  133. public function checkActivated()
  134. {
  135. try {
  136. $params = $this->request->get();
  137. $device_id = $params['device_id'] ?? '0';
  138. $activate_code = $params['activate_code'] ?? '0';
  139. $codeInfo = Codes::where(['activate_code' => $activate_code])->find();
  140. if (!$codeInfo) {
  141. throw new \Exception('激活码无效');
  142. }
  143. if ($codeInfo->status == 2) {
  144. throw new \Exception('激活码已失效,请重新激活');
  145. }
  146. $deviceInfo = Devices::get(['activate_code' => $activate_code, 'device_id' => $device_id]);
  147. if (!$deviceInfo) {
  148. throw new \Exception('未激活');
  149. }
  150. if ($deviceInfo->expiration_time > 0 && $deviceInfo->expiration_time < time()) {
  151. throw new \Exception('激活已过期,请重新激活');
  152. }
  153. $this->success('已激活');
  154. } catch (HttpResponseException $e) {
  155. throw $e;
  156. } catch (\Exception $e) {
  157. $this->error($e->getMessage());
  158. }
  159. }
  160. }