Ptoken.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class Ptoken extends Model
  5. {
  6. // 表名
  7. protected $table = 'ptoken';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. /**
  14. * 检查渠道是否授权指定平台,可同时检查多个平台
  15. * @param $platform_id
  16. * @param $channel_id
  17. * @return bool
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function checkChannelIsAuth($platform_id,$channel_id){
  23. if($token = $this->whereIn('platform_id',$platform_id)->where('admin_id',$channel_id)->select()){
  24. return true;
  25. }else{
  26. return false;
  27. }
  28. }
  29. /**
  30. * 检查指定平台是否授权
  31. * @param $platform_id
  32. * @param $admin_id
  33. * @return bool
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. * @throws \think\exception\DbException
  37. */
  38. public function checkPlatformIsAuth($platform_id,$admin_id){
  39. $is_auth = true;
  40. $auth_platform = [];
  41. if(!$platform = model('platform')->whereIn('id',$platform_id)->where(['status'=>'1'])->select()){
  42. return false;
  43. }
  44. if(!$ptoken = $this->where(['admin_id'=>$admin_id])->select()){
  45. return false;
  46. }
  47. foreach($ptoken as $token){
  48. if(!empty($token['refresh_token'])){
  49. array_push($auth_platform,$token['platform_id']);
  50. }
  51. }
  52. foreach($platform as $info){
  53. if(!in_array($info['id'],$auth_platform)){
  54. $is_auth = false;
  55. }
  56. }
  57. return $is_auth;
  58. }
  59. //根据平台获取refresh_token
  60. public function getToken($platform_id,$admin_id){
  61. $info = $this->where(['platform_id'=>$platform_id,'admin_id'=>$admin_id])->find();
  62. if($info){
  63. $info = $info->toArray();
  64. return $info['refresh_token'];
  65. }else{
  66. return false;
  67. }
  68. }
  69. }