ThirdloginRepository.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Thirdlogin;
  4. use Prettus\Repository\Eloquent\BaseRepository;
  5. /**
  6. * Class ThirdloginRepository.
  7. *
  8. * @package namespace App\Repositories;
  9. */
  10. class ThirdloginRepository extends BaseRepository
  11. {
  12. public function model()
  13. {
  14. return Thirdlogin::class;
  15. }
  16. public function checkWechat($openid, $unionid, $type)
  17. {
  18. $thirdlogin=$this->model->where('openid', $openid)->first();
  19. if (!$thirdlogin && $unionid) {
  20. $thirdlogin=$this->model->where('unionid', $unionid)->first();
  21. if ($thirdlogin) {
  22. $newThirdlogin=$thirdlogin->toArray();
  23. unset($newThirdlogin['id']);
  24. unset($newThirdlogin['created_at']);
  25. unset($newThirdlogin['updated_at']);
  26. $newThirdlogin['type']=$type;
  27. $newThirdlogin['openid']=$openid;
  28. $newThirdlogin['unionid']=$unionid;
  29. $this->model->create($newThirdlogin);
  30. }
  31. }
  32. if (!$thirdlogin) {
  33. return false;
  34. }
  35. return $this->getUser($thirdlogin);
  36. }
  37. public function checkQQ($openid)
  38. {
  39. $thirdlogin=$this->model->where('openid', $openid)->first();
  40. if (!$thirdlogin) {
  41. return false;
  42. }
  43. return $this->getUser($thirdlogin);
  44. }
  45. /**
  46. * @param Thirdlogin $thirdlogin
  47. * @return
  48. */
  49. protected function getUser($thirdlogin)
  50. {
  51. if ($thirdlogin->utype == 1) {
  52. return $thirdlogin->company()->withTrashed()->first();
  53. } else {
  54. return $thirdlogin->member()->withTrashed()->first();
  55. }
  56. }
  57. public function checkBind($user, $type)
  58. {
  59. return $this->model->where(['uid'=>$user->id,'utype'=>$user->utype])->whereIn('type', $type)->first();
  60. }
  61. public function unBind($type, $user)
  62. {
  63. return $this->model->where(['uid'=>$user->id,'utype'=>$user->utype])->whereIn('type', $type)->delete();
  64. }
  65. }