123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use app\common\model\object\ReferralObject;
- use think\Model;
- class Referral extends Model
- {
- // 表名
- protected $table = 'referral';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'wx_type_text',
- 'type_text'
- ];
- /**
- * 获取单条数据
- *
- * @param $referral_id
- * @param bool $idx 默认true 获取idx , false 获取url原文
- * @return mixed|string
- */
- public function getone($referral_id, $idx = true)
- {
- if (!$referral_id){
- return null;
- }
- $redis = Redis::instance();
- $key = 'RI:N:'. $referral_id;
- $json = $redis->get($key);
- if ($json == -1){
- return null;
- }
- if ($json) {
- $data = json_decode($json, true);
- if (is_array($data) && !array_key_exists('state', $data)) {
- $data['state'] = 1;
- }
- $data = $idx ? $data['guide_chapter_idx'] : $data;
- } else {
- $db_data = $this->where('id=' . $referral_id)->find();
- if(!empty($db_data)){
- $db_data = $db_data->toArray();
- $json = json_encode($db_data,JSON_UNESCAPED_UNICODE);
- $redis->setex($key, 86400, $json);
- $data = $idx ? $db_data['guide_chapter_idx'] : $db_data;
- }else{
- $data = -1;
- $redis->setex($key, 86400, $data);
- $data = null;
- }
- }
- return $data;
- }
- public function getWxTypeList()
- {
- return ['1' => '已认证', '2' => '未认证'];
- }
- public function getTypeList()
- {
- return ['1' => '阅读页推广', '2' => '首页推广'];
- }
- public function getGuideSexList()
- {
- return ['1' => '男频', '2' => '女频'];
- }
- public function getWxTypeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['wx_type'];
- $list = $this->getWxTypeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getTypeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['type'];
- $list = $this->getTypeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getPushList()
- {
- return ['0' => '外推', '1' => '内推'];
- }
- public function getPushTextAttr($value, $data)
- {
- $value = $value ? $value : $data['push'];
- $list = $this->getPushList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- // 一对一关联 book表
- public function book()
- {
- return $this->belongsTo('Book', 'book_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- /**
- * @param $referralId
- * @return int|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getDaoFenChapterId($referralId)
- {
- $referral = $this->where('id', '=', $referralId)->find();
- return $chapterId = $referral->guide_chapter_idx ?? 10;
- }
- /**
- * 检测VIP链接有效性
- * @param $id
- * @return int
- */
- public function checkVipReferralValid($id)
- {
- $state = 1;
- $key = 'RIV:'.$id;
- if (Redis::instance()->exists($key)) {
- $state = Redis::instance()->get($key);
- } else {
- $library = model("ReferralLibrary")->where('id', 'eq', $id)->find();
- if ($library) {
- $referral = $this->getone($library['referral_id'], false);
- $state = $referral['state'];
- } else {
- $state = 0;
- }
- Redis::instance()->set($key, $state, 600);
- }
- return $state;
- }
- public function mkParamsForInsert(ReferralObject $oReferral)
- {
- return [
- 'push' => $oReferral->push,
- 'manage_title_id' => $oReferral->manage_title_id,
- 'manage_cover_id' => $oReferral->manage_cover_id,
- 'manage_template_id' => $oReferral->manage_template_id,
- 'manage_preview_id' => $oReferral->manage_preview_id,
- 'book_id' => $oReferral->book_id,
- 'chapter_id' => $oReferral->chapter_id,
- 'chapter_name' => $oReferral->chapter_name,
- 'chapter_idx' => $oReferral->chapter_idx,
- 'guide_chapter_idx' => $oReferral->guide_chapter_idx,
- 'admin_id' => $oReferral->admin_id,
- 'name' => $oReferral->name,
- 'cost' => $oReferral->cost,
- 'wx_type' => $oReferral->wx_type,
- 'type' => $oReferral->type,
- 'source_url' => $oReferral->source_url,
- 'short_url_qq' => $oReferral->short_url_qq,
- 'short_url_weibo' => $oReferral->short_url_weibo,
- 'createtime' => $oReferral->createtime??time(),
- 'updatetime' => $oReferral->updatetime??time(),
- 'state' => $oReferral->state ?? 1,
- 'short_id' => $oReferral->short_id,
- 'guide_sex' => $oReferral->guide_sex,
- 'guide_idx' => $oReferral->guide_idx,
- 'guide_title' => $oReferral->guide_title,
- 'guide_image' => $oReferral->guide_image,
- 'utime' => $oReferral->utime,
- 'share_title' => $oReferral->share_title,
- 'share_image' => $oReferral->share_image,
- 'is_collect' => $oReferral->is_collect,
- ];
- }
- public function mkParamsForUpdate(ReferralObject $oReferral)
- {
- return [
- 'type' => $oReferral->type,
- 'wx_type' => $oReferral->wx_type,
- 'name' => $oReferral->name,
- 'push' => $oReferral->push,
- 'guide_chapter_idx' => $oReferral->guide_chapter_idx,
- 'chapter_id' => $oReferral->chapter_id,
- 'chapter_idx' => $oReferral->chapter_idx,
- 'chapter_name' => $oReferral->chapter_name,
- 'book_id' => $oReferral->book_id,
- 'share_title' => $oReferral->share_title,
- 'share_image' => $oReferral->share_image,
- 'updatetime' => time(),
- ];
- }
- }
|