Subscription.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class Subscription extends Model
  6. {
  7. // 表名
  8. protected $table = 'subscription';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. ];
  17. public function getSubscriptionByAppId($appid){
  18. $redis = Redis::instance();
  19. $key = 'S-A:' . $appid;
  20. $subscription_id = null;
  21. if ($redis->exists($key)) {
  22. $subscription_id = $redis->get($key);
  23. }
  24. if (!$subscription_id) {
  25. $subscription_id = $this->where('appid', $appid)->value('id');
  26. if ($subscription_id) {
  27. $redis->setex($key, 3600, $subscription_id);
  28. }
  29. }
  30. if ($subscription_id) {
  31. return $this->getSubscriptionById($subscription_id);
  32. }else{
  33. return null;
  34. }
  35. }
  36. public function getSubscriptionById($id){
  37. $redis = Redis::instance();
  38. $key = 'S-N:'.$id;
  39. if($redis->exists($key)){
  40. return json_decode($redis->get($key),true);
  41. }else{
  42. if($subscription = $this->where('id',$id)->find()){
  43. $refresh_token = model('SubscriptionToken')->where('subscription_id',$id)->value('refresh_token');
  44. $subscription['refresh_token'] = $refresh_token;
  45. $redis->setex($key, 3600, json_encode($subscription, JSON_UNESCAPED_UNICODE));
  46. return $subscription;
  47. }
  48. return null;
  49. }
  50. }
  51. public function clearCacheByAppId($appid){
  52. $redis = Redis::instance();
  53. $key = 'S-A:' . $appid;
  54. $subscription_id = null;
  55. if ($redis->exists($key)) {
  56. $subscription_id = $redis->get($key);
  57. $redis->del('S-N:'.$subscription_id);
  58. }else{
  59. $subscription_id = $this->where('appid', $appid)->value('id');
  60. if ($subscription_id) {
  61. $redis->del('S-N:'.$subscription_id);
  62. }
  63. }
  64. }
  65. /**
  66. * 通过订阅号名称查找订阅号id
  67. * @param $accName
  68. * @return array|bool
  69. */
  70. public function getIdByOfficialName($accName)
  71. {
  72. if (empty($accName)) {
  73. return false;
  74. }
  75. $where = [
  76. 'name' => $accName,
  77. 'status' => 'normal'
  78. ];
  79. $result = $this->where($where)->column('id');
  80. return empty($result) ? 0 : current($result);
  81. }
  82. }