123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use think\Cache;
- use think\Log;
- use think\Model;
- class Platform extends Model
- {
- /**
- * 默认平台
- */
- const CACHE_KEY_DEFAULT_LIST = 'PF:D';
- /**
- * 平台信息缓存 + id
- */
- const CACHE_KEY_ID = 'PF:';
- // 表名
- protected $table = 'platform';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
-
- // 追加属性
- protected $append = [
- 'status_text',
- 'isdefault_text'
- ];
- /**
- * 删除平台全部缓存
- * @param $id
- */
- public function delCacheAll($id){
- $redis = Redis::instance();
- //删除ID
- $redis->del(self::CACHE_KEY_ID.$id);
- //删除默认平台缓存
- $redis->del(self::CACHE_KEY_DEFAULT_LIST);
- }
- public function getInfo($id=null){
- $redis = Redis::instance();
- if(empty($id)){ //获取默认平台信息
- $modulename = request()->module();
- if($modulename == 'admin' && PHP_SAPI != 'cli'){
- $arr = $this->where(['isdefault'=>'1','status'=>1])->find();
- if(!$arr){
- $arr = $this->where(['status'=>1])->find();
- }
- if($arr){
- $arr = $arr->toArray();
- }
- }else{
- if($redis->exists(self::CACHE_KEY_DEFAULT_LIST)){
- $arr = $redis->hgetall(self::CACHE_KEY_DEFAULT_LIST);
- }else{
- $arr = $this->where(['isdefault'=>'1','status'=>1])->find();
- if(!$arr){
- $arr = $this->where(['status'=>1])->find();
- }
- if($arr){
- $arr = $arr->toArray();
- $redis->hmset(self::CACHE_KEY_DEFAULT_LIST,$arr);
- $redis->expire(self::CACHE_KEY_DEFAULT_LIST,86400);
- }
- }
- }
- }else{ //获取指定平台信息
- $modulename = request()->module();
- if($modulename == 'admin' && PHP_SAPI != 'cli'){ //如果是后台域名
- $arr = $this->where('id',$id)->find();
- if($arr){
- $arr = $arr->toArray();
- }
- }else{
- $key = self::CACHE_KEY_ID.$id;
- $arr = null;
- if (Cache::has($key)) {
- Log::info("获取缓存:$key filecache命中");
- $arr = Cache::get($key);
- }
- if (!$arr) {
- if ($redis->exists($key)) {
- $arr = $redis->hgetall($key);
- Log::info("获取缓存:$key redis命中");
- Cache::set($key, $arr, 10);
- }
- }
- if (!$arr) {
- $arr = $this->where('id', $id)->find();
- if ($arr) {
- Log::info("获取缓存:$key mysql命中");
- $arr = $arr->toArray();
- $redis->hmset($key, $arr);
- $redis->expire($key, 3600);
- }
- }
- }
- }
- return $arr;
- }
- /**
- * 获取当前渠道已授权的平台ID
- * @param $channel_id
- * @param null $platform_list 过滤指定的平台列表
- * @return string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getChannelHaveAuthorizedPlatformId($channel_id,$platform_list = null){
- $result = [];
- if($platform_list){
- if(!is_array($platform_list)){
- $platform_list = explode(',',$platform_list);
- }
- }
- $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();
- if($list){
- if($platform_list){
- foreach($platform_list as $val){
- foreach($list as $platform){
- if($val == $platform['id']){
- array_push($result,$platform['id']);
- }
- }
- }
- }else{
- foreach($list as $platform){
- array_push($result,$platform['id']);
- }
- }
- }else{
- if($platform_list){
- $result = $platform_list;
- }
- }
- return trim(implode(',',$result),',');
- }
- public function getPlatformList($admin_id=0)
- {
- if(empty($admin_id)){
- $list = $this->where("status = '1'")->order('id','desc')->select();
- }else{
- $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();
- }
- return $list;
- }
-
- public function getStatusList()
- {
- return ['0' => __('Status 0'),'1' => __('Status 1')];
- }
- public function getIsdefaultList()
- {
- return ['0' => __('Isdefault 0'),'1' => __('Isdefault 1')];
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : $data['status'];
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getIsdefaultTextAttr($value, $data)
- {
- $value = $value ? $value : $data['isdefault'];
- $list = $this->getIsdefaultList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- }
|