Ophost.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 Ophost extends Model
  8. {
  9. /**
  10. * 三方平台默认业务域名 + 平台ID
  11. */
  12. const CACHE_KEY_DEFAULT_PLATFORM = 'OPH:D:';
  13. /**
  14. * 微信业务域名host列表
  15. */
  16. const CACHE_KEY_HOST_LIST = 'OPHOSTLIST';
  17. /**
  18. * 业务域名缓存 + 业务域名ID
  19. */
  20. const CACHE_KEY_ID = 'OPH:';
  21. /**
  22. * 业务域名缓存 + 业务域名host
  23. * @var
  24. */
  25. const CACHE_KEY_HOST = 'OPH:H:';
  26. // 表名
  27. protected $table = 'ophost';
  28. // 自动写入时间戳字段
  29. protected $autoWriteTimestamp = false;
  30. // 定义时间戳字段名
  31. protected $createTime = false;
  32. protected $updateTime = false;
  33. // 追加属性
  34. protected $append = [
  35. 'isdefault_text',
  36. 'status_text'
  37. ];
  38. /**
  39. * 删除全部缓存
  40. * @param $id
  41. * @param null $host
  42. * @param null $platform_id
  43. */
  44. public function delCacheAll($id,$host = null,$platform_id = null){
  45. $redis = Redis::instance();
  46. //删除ID
  47. $redis->del(self::CACHE_KEY_ID.$id);
  48. //Host没有时取ID的
  49. if(empty($host)){
  50. $host = $this->where('id',$id)->value('host');
  51. }
  52. //平台Id没有事取ID的
  53. if(empty($platform_id)){
  54. $platform_id = $this->where('id',$id)->value('platform_id');
  55. }
  56. //删除Host
  57. $redis->del(self::CACHE_KEY_HOST.$host);
  58. $redis->del(self::CACHE_KEY_HOST_LIST);
  59. //删除平台ID
  60. $redis->del(self::CACHE_KEY_DEFAULT_PLATFORM.$platform_id);
  61. }
  62. public function delCacheByHosts(){
  63. $redis = Redis::instance();
  64. $redis->del(self::CACHE_KEY_HOST_LIST);
  65. }
  66. /**
  67. * 删除Ophost平台缓存
  68. * @param null $platform_id
  69. */
  70. public function delCacheByPlatformId($platform_id){
  71. $redis = Redis::instance();
  72. //删除平台ID
  73. $redis->del(self::CACHE_KEY_DEFAULT_PLATFORM.$platform_id);
  74. }
  75. //获取所有可用的业务域名 全部业务域名
  76. public function getHosts(){
  77. $redis = Redis::instance();
  78. if($redis->exists(self::CACHE_KEY_HOST_LIST)){
  79. $res = \GuzzleHttp\json_decode($redis->get(self::CACHE_KEY_HOST_LIST),true);
  80. }else{
  81. $res = collection($this->column('host'))->toArray();
  82. $redis->setex(self::CACHE_KEY_HOST_LIST,86400,json_encode($res));
  83. }
  84. return $res;
  85. }
  86. public function getInfo($platform_id,$id=null){
  87. if(empty($platform_id)) {
  88. return null;
  89. }
  90. $redis = Redis::instance();
  91. if(empty($id)){ //获取当前平台下默认的业务域名信息
  92. $modulename = request()->module();
  93. if($modulename == 'admin' && PHP_SAPI != 'cli') { //如果是后台域名
  94. $arr = $this->where(['platform_id'=>$platform_id,'isdefault'=>'1','status'=>1])->find();
  95. if(!$arr){
  96. $arr = $this->where(['platform_id'=>$platform_id,'status'=>1])->find();
  97. }
  98. if($arr){
  99. $arr = $arr->toArray();
  100. }
  101. }else{
  102. $key = self::CACHE_KEY_DEFAULT_PLATFORM.$platform_id;
  103. if($redis->exists($key)){
  104. $arr = $redis->hgetall($key);
  105. }else{
  106. $arr = $this->where(['platform_id'=>$platform_id,'isdefault'=>'1','status'=>1])->find();
  107. if(!$arr){
  108. $arr = $this->where(['platform_id'=>$platform_id,'status'=>1])->find();
  109. }
  110. if($arr){
  111. $arr = $arr->toArray();
  112. $redis->hmset($key,$arr);
  113. $redis->expire($key,86400);
  114. }
  115. }
  116. }
  117. }else{ //获取指定的域名信息
  118. $modulename = request()->module();
  119. if($modulename == 'admin' && PHP_SAPI != 'cli') { //如果是后台域名
  120. $arr = $this->where('id',$id)->find();
  121. if($arr){
  122. $arr = $arr->toArray();
  123. }
  124. }else{
  125. $arr = $this->getInfoById($id);
  126. }
  127. }
  128. return $arr;
  129. }
  130. /**
  131. * 根据host获取业务域名信息
  132. * @param $host
  133. * @return mixed
  134. */
  135. public function getInfoByHost($host){
  136. $redis = Redis::instance();
  137. $key = self::CACHE_KEY_HOST.$host;
  138. $ophost = null;
  139. if($ophost = Cache::get($key)){
  140. if(!is_array($ophost)){
  141. $ophost = json_decode($ophost,true);
  142. }
  143. $time = date('Y-m-d H:i:s',time());
  144. Log::write("Time: {$time} Read: ".json_encode($ophost,JSON_UNESCAPED_UNICODE),'FileCache');
  145. return $ophost;
  146. }else{
  147. if($redis->exists($key)){
  148. $ophost = $redis->hgetall($key);
  149. Cache::set($key,$ophost,5);
  150. $time = date('Y-m-d H:i:s',time());
  151. Log::write("Time: {$time} Set: ".json_encode($ophost,JSON_UNESCAPED_UNICODE),'FileCache');
  152. }else{
  153. if($ophost = $this->where('host',$host)->find()){
  154. $ophost = $ophost->toArray();
  155. $redis->hmset($key,$ophost);
  156. $redis->expire($key,3600);
  157. }
  158. }
  159. }
  160. return $ophost;
  161. }
  162. public function getInfoById($id){
  163. $redis = Redis::instance();
  164. $key = self::CACHE_KEY_ID.$id;
  165. $ophost = null;
  166. if (Cache::has($key)) {
  167. Log::info("获取缓存:$key filecache命中");
  168. $ophost = Cache::get($key);
  169. }
  170. if (!$ophost) {
  171. if ($redis->exists($key)) {
  172. $ophost = $redis->hgetall($key);
  173. Log::info("获取缓存:$key redis命中");
  174. Cache::set($key, $ophost, 10);
  175. }
  176. }
  177. if (!$ophost) {
  178. $ophost = $this->where('id', $id)->find();
  179. if ($ophost) {
  180. Log::info("获取缓存:$key mysql命中");
  181. $ophost = $ophost->toArray();
  182. $redis->hmset($key, $ophost);
  183. $redis->expire($key, 3600);
  184. }
  185. }
  186. return $ophost;
  187. }
  188. public function getListByPlatformId($platform_id=null,$isReturnPlatformID = false){
  189. if(!$platform_id){
  190. return false;
  191. }
  192. $map['status'] = 1;
  193. $list = $this->whereIn('platform_id',$platform_id)->where($map)->select();
  194. $ophostList = [];
  195. if($list){
  196. foreach ($list as $v){
  197. $platform_name = model('Platform')->where('id',$v['platform_id'])->value('name');
  198. if($isReturnPlatformID){
  199. array_push($ophostList,['id'=>$v['id'],'platform_id'=>$v['platform_id'],'text'=>$platform_name.'---'.$v['host']."--{$v['p_desc']}"]);
  200. }else{
  201. $ophostList[$v['id']] = $platform_name.'---'.$v['host']."--{$v['p_desc']}";
  202. }
  203. }
  204. }
  205. return $ophostList;
  206. }
  207. public function getHostListByPlatformId($platform_id=null){
  208. if(!$platform_id){
  209. return false;
  210. }
  211. $map['status'] = 1;
  212. $map['allow_changed'] = 1;
  213. $list = $this->whereIn('platform_id', explode(',', $platform_id))->where($map)->select();
  214. Log::sql('[ ChangeDomain ] [ getHostListByPlatformId] ' . $this->getLastSql());
  215. $ophostList = [];
  216. if($list){
  217. foreach ($list as $v) {
  218. array_push($ophostList, ['ophost_id'=>$v['id'], 'ophost_host'=>$v['host']]);
  219. }
  220. }
  221. return $ophostList;
  222. }
  223. /**
  224. * 获取平台支付列表
  225. * @param $platform_id
  226. * @return array
  227. * @throws \think\db\exception\DataNotFoundException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. * @throws \think\exception\DbException
  230. */
  231. public function getListFormatByPlatformId($platform_id = null){
  232. $result = [];
  233. $map['status'] = 1;
  234. if($platform_id){
  235. $map['platform_id'] = ['in',$platform_id];
  236. }
  237. if($list = $this->where($map)->select()){
  238. foreach($list as $val){
  239. $platform_name = model('Platform')->where('id',$val['platform_id'])->value('name');
  240. array_push($result,['id'=>$val['id'],'platform_id'=>$val['platform_id'],'text'=>$platform_name.'--'.$val['host'].'--'.$val['p_desc']]);
  241. }
  242. }
  243. return $result;
  244. }
  245. public function getIsdefaultList()
  246. {
  247. return ['0' => __('Isdefault 0'),'1' => __('Isdefault 1')];
  248. }
  249. public function getStatusList()
  250. {
  251. return ['0' => __('Status 0'),'1' => __('Status 1')];
  252. }
  253. public function getAllowChangedList()
  254. {
  255. return ['0' => __('AllowChanged0'),'1' => __('AllowChanged1')];
  256. }
  257. public function getIsdefaultTextAttr($value, $data)
  258. {
  259. $value = $value ? $value : $data['isdefault'];
  260. $list = $this->getIsdefaultList();
  261. return isset($list[$value]) ? $list[$value] : '';
  262. }
  263. public function getStatusTextAttr($value, $data)
  264. {
  265. $value = $value ? $value : $data['status'];
  266. $list = $this->getStatusList();
  267. return isset($list[$value]) ? $list[$value] : '';
  268. }
  269. public function platform()
  270. {
  271. return $this->belongsTo('Platform', 'platform_id')->setEagerlyType(0);
  272. }
  273. }