Platform.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Cache;
  5. use think\Log;
  6. use think\Model;
  7. class Platform extends Model
  8. {
  9. /**
  10. * 默认平台
  11. */
  12. const CACHE_KEY_DEFAULT_LIST = 'PF:D';
  13. /**
  14. * 平台信息缓存 + id
  15. */
  16. const CACHE_KEY_ID = 'PF:';
  17. // 表名
  18. protected $table = 'platform';
  19. // 自动写入时间戳字段
  20. protected $autoWriteTimestamp = 'int';
  21. // 定义时间戳字段名
  22. protected $createTime = 'createtime';
  23. protected $updateTime = 'updatetime';
  24. // 追加属性
  25. protected $append = [
  26. 'status_text',
  27. 'isdefault_text'
  28. ];
  29. /**
  30. * 删除平台全部缓存
  31. * @param $id
  32. */
  33. public function delCacheAll($id){
  34. $redis = Redis::instance();
  35. //删除ID
  36. $redis->del(self::CACHE_KEY_ID.$id);
  37. //删除默认平台缓存
  38. $redis->del(self::CACHE_KEY_DEFAULT_LIST);
  39. }
  40. public function getInfo($id=null){
  41. $redis = Redis::instance();
  42. if(empty($id)){ //获取默认平台信息
  43. $modulename = request()->module();
  44. if($modulename == 'admin' && PHP_SAPI != 'cli'){
  45. $arr = $this->where(['isdefault'=>'1','status'=>1])->find();
  46. if(!$arr){
  47. $arr = $this->where(['status'=>1])->find();
  48. }
  49. if($arr){
  50. $arr = $arr->toArray();
  51. }
  52. }else{
  53. if($redis->exists(self::CACHE_KEY_DEFAULT_LIST)){
  54. $arr = $redis->hgetall(self::CACHE_KEY_DEFAULT_LIST);
  55. }else{
  56. $arr = $this->where(['isdefault'=>'1','status'=>1])->find();
  57. if(!$arr){
  58. $arr = $this->where(['status'=>1])->find();
  59. }
  60. if($arr){
  61. $arr = $arr->toArray();
  62. $redis->hmset(self::CACHE_KEY_DEFAULT_LIST,$arr);
  63. $redis->expire(self::CACHE_KEY_DEFAULT_LIST,86400);
  64. }
  65. }
  66. }
  67. }else{ //获取指定平台信息
  68. $modulename = request()->module();
  69. if($modulename == 'admin' && PHP_SAPI != 'cli'){ //如果是后台域名
  70. $arr = $this->where('id',$id)->find();
  71. if($arr){
  72. $arr = $arr->toArray();
  73. }
  74. }else{
  75. $key = self::CACHE_KEY_ID.$id;
  76. $arr = null;
  77. if (Cache::has($key)) {
  78. Log::info("获取缓存:$key filecache命中");
  79. $arr = Cache::get($key);
  80. }
  81. if (!$arr) {
  82. if ($redis->exists($key)) {
  83. $arr = $redis->hgetall($key);
  84. Log::info("获取缓存:$key redis命中");
  85. Cache::set($key, $arr, 10);
  86. }
  87. }
  88. if (!$arr) {
  89. $arr = $this->where('id', $id)->find();
  90. if ($arr) {
  91. Log::info("获取缓存:$key mysql命中");
  92. $arr = $arr->toArray();
  93. $redis->hmset($key, $arr);
  94. $redis->expire($key, 3600);
  95. }
  96. }
  97. }
  98. }
  99. return $arr;
  100. }
  101. /**
  102. * 获取当前渠道已授权的平台ID
  103. * @param $channel_id
  104. * @param null $platform_list 过滤指定的平台列表
  105. * @return string
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. * @throws \think\exception\DbException
  109. */
  110. public function getChannelHaveAuthorizedPlatformId($channel_id,$platform_list = null){
  111. $result = [];
  112. if($platform_list){
  113. if(!is_array($platform_list)){
  114. $platform_list = explode(',',$platform_list);
  115. }
  116. }
  117. $list = $this->join('ptoken p','platform.id=p.platform_id and p.admin_id = '.$channel_id)->where('p.refresh_token','not null')->field('platform.*')->order('platform.id','desc')->select();
  118. if($list){
  119. if($platform_list){
  120. foreach($platform_list as $val){
  121. foreach($list as $platform){
  122. if($val == $platform['id']){
  123. array_push($result,$platform['id']);
  124. }
  125. }
  126. }
  127. }else{
  128. foreach($list as $platform){
  129. array_push($result,$platform['id']);
  130. }
  131. }
  132. }else{
  133. if($platform_list){
  134. $result = $platform_list;
  135. }
  136. }
  137. return trim(implode(',',$result),',');
  138. }
  139. public function getPlatformList($admin_id=0)
  140. {
  141. if(empty($admin_id)){
  142. $list = $this->where("status = '1'")->order('id','desc')->select();
  143. }else{
  144. $list = $this->join('ptoken p','platform.id=p.platform_id and p.admin_id='.$admin_id)->where('p.refresh_token','not null')->field('platform.*')->order('platform.id','desc')->select();
  145. }
  146. return $list;
  147. }
  148. public function getStatusList()
  149. {
  150. return ['0' => __('Status 0'),'1' => __('Status 1')];
  151. }
  152. public function getIsdefaultList()
  153. {
  154. return ['0' => __('Isdefault 0'),'1' => __('Isdefault 1')];
  155. }
  156. public function getStatusTextAttr($value, $data)
  157. {
  158. $value = $value ? $value : $data['status'];
  159. $list = $this->getStatusList();
  160. return isset($list[$value]) ? $list[$value] : '';
  161. }
  162. public function getIsdefaultTextAttr($value, $data)
  163. {
  164. $value = $value ? $value : $data['isdefault'];
  165. $list = $this->getIsdefaultList();
  166. return isset($list[$value]) ? $list[$value] : '';
  167. }
  168. }