123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use think\Model;
- class Subscription extends Model
- {
- // 表名
- protected $table = 'subscription';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- ];
- public function getSubscriptionByAppId($appid){
- $redis = Redis::instance();
- $key = 'S-A:' . $appid;
- $subscription_id = null;
- if ($redis->exists($key)) {
- $subscription_id = $redis->get($key);
- }
- if (!$subscription_id) {
- $subscription_id = $this->where('appid', $appid)->value('id');
- if ($subscription_id) {
- $redis->setex($key, 3600, $subscription_id);
- }
- }
- if ($subscription_id) {
- return $this->getSubscriptionById($subscription_id);
- }else{
- return null;
- }
- }
- public function getSubscriptionById($id){
- $redis = Redis::instance();
- $key = 'S-N:'.$id;
- if($redis->exists($key)){
- return json_decode($redis->get($key),true);
- }else{
- if($subscription = $this->where('id',$id)->find()){
- $refresh_token = model('SubscriptionToken')->where('subscription_id',$id)->value('refresh_token');
- $subscription['refresh_token'] = $refresh_token;
- $redis->setex($key, 3600, json_encode($subscription, JSON_UNESCAPED_UNICODE));
- return $subscription;
- }
- return null;
- }
- }
- public function clearCacheByAppId($appid){
- $redis = Redis::instance();
- $key = 'S-A:' . $appid;
- $subscription_id = null;
- if ($redis->exists($key)) {
- $subscription_id = $redis->get($key);
- $redis->del('S-N:'.$subscription_id);
- }else{
- $subscription_id = $this->where('appid', $appid)->value('id');
- if ($subscription_id) {
- $redis->del('S-N:'.$subscription_id);
- }
- }
- }
- /**
- * 通过订阅号名称查找订阅号id
- * @param $accName
- * @return array|bool
- */
- public function getIdByOfficialName($accName)
- {
- if (empty($accName)) {
- return false;
- }
- $where = [
- 'name' => $accName,
- 'status' => 'normal'
- ];
- $result = $this->where($where)->column('id');
- return empty($result) ? 0 : current($result);
- }
- }
|