ClientService.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace services\oauth2;
  3. use common\components\Service;
  4. use common\enums\StatusEnum;
  5. use common\helpers\StringHelper;
  6. use common\models\oauth2\Client;
  7. /**
  8. * Class ClientService
  9. * @package services\oauth2
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class ClientService extends Service
  13. {
  14. /**
  15. * @param $client_id
  16. * @return array|\yii\db\ActiveRecord|null
  17. */
  18. public function findByClientId($client_id)
  19. {
  20. return Client::find()
  21. ->where(['client_id' => $client_id, 'status' => StatusEnum::ENABLED])
  22. ->one();
  23. }
  24. /**
  25. * @param $merchant_id
  26. * @param $title
  27. */
  28. public function createByMerchantId($merchant_id, $title)
  29. {
  30. $model = new Client();
  31. $model = $model->loadDefaultValues();
  32. $model->merchant_id = $merchant_id;
  33. $model->title = $title;
  34. $model->client_id = StringHelper::random(15);
  35. $model->client_secret = StringHelper::random(30);
  36. $model->scope = [];
  37. if ($this->findByClientId($model->client_id)){
  38. return $this->createByMerchantId($merchant_id, $title);
  39. }
  40. $model->save();
  41. return $model;
  42. }
  43. /**
  44. * @param $merchant_id
  45. * @param $title
  46. */
  47. public function resetByMerchantId($merchant_id, $title)
  48. {
  49. $model = $this->findByMerchantId($merchant_id);
  50. $model->title = $title;
  51. $model->client_id = StringHelper::random(15);
  52. $model->client_secret = StringHelper::random(30);
  53. if ($this->findByClientId($model->client_id)){
  54. return $this->resetByMerchantId($merchant_id, $title);
  55. }
  56. $model->save();
  57. return $model;
  58. }
  59. /**
  60. * @return array|\yii\db\ActiveRecord|null|Client
  61. */
  62. public function findByMerchantId($merchant_id)
  63. {
  64. return Client::find()
  65. ->where(['merchant_id' => $merchant_id])
  66. ->andWhere(['status' => StatusEnum::ENABLED])
  67. ->one();
  68. }
  69. }
粤ICP备19079148号