ShareMessage.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 分享配置
  6. *
  7. */
  8. class ShareMessage extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'share_message';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. private $pagesList = [
  19. 'pages/index/index'=>'首页',
  20. 'pages/car/index'=>'购物车',
  21. 'pages/user/index'=>'个人中心',
  22. 'pages/score/clockin'=>'积分签到',
  23. 'pages/score/index'=>'积分产品',
  24. 'pages/score/lottery'=>'积分抽奖',
  25. 'pages/coupon/active'=>'领券活动',
  26. 'pages/recruitment/index'=>'拉新活动',
  27. 'pages/activity/index'=>'答题活动',
  28. 'pages/product/index'=>'产品详情',
  29. ];
  30. /**
  31. * 页面列表
  32. *
  33. */
  34. public function getPagesList()
  35. {
  36. // 返回结果
  37. return $this->pagesList;
  38. }
  39. /**
  40. * 添加数据
  41. *
  42. */
  43. public function add($data)
  44. {
  45. // 时间
  46. $data['insert_time'] = time();
  47. $data['update_time'] = time();
  48. // 写入数据表
  49. $id = $this->query()->insertGetId($data);
  50. // 如果操作失败
  51. if( !$id ) return 0;
  52. // 更新缓存
  53. $this->getList(true);
  54. // 返回结果
  55. return $id;
  56. }
  57. /**
  58. * 添加数据
  59. *
  60. */
  61. public function edit($id,$data)
  62. {
  63. // 更新时间
  64. $data['update_time'] = time();
  65. // 写入数据表
  66. $result = $this->query()->where([['id','=',$id]])->update($data);
  67. // 如果操作失败
  68. if( !$result ) return $result;
  69. // 更新缓存
  70. $this->getList(true);
  71. // 返回结果
  72. return $result;
  73. }
  74. /**
  75. * 获取列表
  76. * @param Bool $force 是否强制更新
  77. *
  78. */
  79. public function getList($force = false)
  80. {
  81. // 结果数据
  82. $list = $force ? [] : cache('admin:share:message:list');
  83. // 不存在数据
  84. if ( !$list ) {
  85. // 从数据库获取数据
  86. $data = $this->query()->where([['status','=',0]])->get(['id','pages','title','image_url','item_id','path']);
  87. // 是否有数据
  88. $data = $data ? $data->toArray() : [];
  89. // 循环处理数据
  90. $list = [];
  91. // 进行更新
  92. foreach ($data as $value) {
  93. // 数据处理
  94. $value['image_url'] = $value['image_url'] ? path_compat($value['image_url']) : '';
  95. // 重组数据
  96. $list[$value['pages']][$value['item_id']] = $value;
  97. }
  98. // 存起来
  99. cache(['admin:share:message:list'=>$list]);
  100. }
  101. // 返回结果
  102. return $list;
  103. }
  104. /**
  105. * 获取配置
  106. *
  107. * @param string $pages 页面
  108. *
  109. */
  110. public function getListByPage($pages)
  111. {
  112. // 获取列表数据
  113. $list = $this->getList();
  114. // 获取数据
  115. $list = isset($list[$pages]) ? $list[$pages] : [];
  116. // 返回值
  117. return $list;
  118. }
  119. /**
  120. * 获取配置
  121. *
  122. * @param string $pages 页面
  123. * @param string $field 指定字段
  124. *
  125. */
  126. public function getOneByPage($pages,$itemId=0,$field='')
  127. {
  128. // 获取列表数据
  129. $list = $this->getListByPage($pages);
  130. // 获取数据
  131. $one = isset($list[$itemId]) ? $list[$itemId] : [];
  132. // 返回值
  133. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  134. }
  135. }