123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 分享配置
- *
- */
- class ShareMessage extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'share_message';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- private $pagesList = [
- 'pages/index/index'=>'首页',
- 'pages/car/index'=>'购物车',
- 'pages/user/index'=>'个人中心',
- 'pages/score/clockin'=>'积分签到',
- 'pages/score/index'=>'积分产品',
- 'pages/score/lottery'=>'积分抽奖',
- 'pages/coupon/active'=>'领券活动',
- 'pages/recruitment/index'=>'拉新活动',
- 'pages/activity/index'=>'答题活动',
- 'pages/product/index'=>'产品详情',
- ];
- /**
- * 页面列表
- *
- */
- public function getPagesList()
- {
- // 返回结果
- return $this->pagesList;
- }
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 如果操作失败
- if( !$id ) return 0;
- // 更新缓存
- $this->getList(true);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where([['id','=',$id]])->update($data);
- // 如果操作失败
- if( !$result ) return $result;
- // 更新缓存
- $this->getList(true);
- // 返回结果
- return $result;
- }
- /**
- * 获取列表
- * @param Bool $force 是否强制更新
- *
- */
- public function getList($force = false)
- {
- // 结果数据
- $list = $force ? [] : cache('admin:share:message:list');
- // 不存在数据
- if ( !$list ) {
- // 从数据库获取数据
- $data = $this->query()->where([['status','=',0]])->get(['id','pages','title','image_url','item_id','path']);
- // 是否有数据
- $data = $data ? $data->toArray() : [];
- // 循环处理数据
- $list = [];
- // 进行更新
- foreach ($data as $value) {
- // 数据处理
- $value['image_url'] = $value['image_url'] ? path_compat($value['image_url']) : '';
- // 重组数据
- $list[$value['pages']][$value['item_id']] = $value;
- }
- // 存起来
- cache(['admin:share:message:list'=>$list]);
- }
- // 返回结果
- return $list;
- }
- /**
- * 获取配置
- *
- * @param string $pages 页面
- *
- */
- public function getListByPage($pages)
- {
- // 获取列表数据
- $list = $this->getList();
- // 获取数据
- $list = isset($list[$pages]) ? $list[$pages] : [];
- // 返回值
- return $list;
- }
- /**
- * 获取配置
- *
- * @param string $pages 页面
- * @param string $field 指定字段
- *
- */
- public function getOneByPage($pages,$itemId=0,$field='')
- {
- // 获取列表数据
- $list = $this->getListByPage($pages);
- // 获取数据
- $one = isset($list[$itemId]) ? $list[$itemId] : [];
- // 返回值
- return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
- }
- }
|