ShareMessage.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/product'=>'积分产品',
  25. 'pages/score/lottery'=>'积分抽奖',
  26. 'pages/coupon/active'=>'领券活动',
  27. 'pages/recruitment/index'=>'拉新活动',
  28. 'pages/activity/index'=>'答题活动',
  29. 'pages/activity/lottery'=>'答题抽奖',
  30. 'pages/product/index'=>'产品详情',
  31. 'pages/recruitment/lottery_new'=>'拉新抽奖',
  32. 'pages/video/index'=>'视频课程',
  33. 'pages/video/detail'=>'视频详情',
  34. ];
  35. /**
  36. * 页面列表
  37. *
  38. */
  39. public function getPagesList()
  40. {
  41. // 返回结果
  42. return $this->pagesList;
  43. }
  44. /**
  45. * 添加数据
  46. *
  47. */
  48. public function add($data)
  49. {
  50. // 时间
  51. $data['insert_time'] = time();
  52. $data['update_time'] = time();
  53. // 写入数据表
  54. $id = $this->query()->insertGetId($data);
  55. // 如果操作失败
  56. if( !$id ) return 0;
  57. // 更新缓存
  58. $this->getList(true);
  59. // 返回结果
  60. return $id;
  61. }
  62. /**
  63. * 添加数据
  64. *
  65. */
  66. public function edit($id,$data)
  67. {
  68. // 更新时间
  69. $data['update_time'] = time();
  70. // 写入数据表
  71. $result = $this->query()->where([['id','=',$id]])->update($data);
  72. // 如果操作失败
  73. if( !$result ) return $result;
  74. // 更新缓存
  75. $this->getList(true);
  76. // 返回结果
  77. return $result;
  78. }
  79. /**
  80. * 获取列表
  81. * @param Bool $force 是否强制更新
  82. *
  83. */
  84. public function getList($force = false)
  85. {
  86. // 结果数据
  87. $list = $force ? [] : cache('admin:share:message:list');
  88. // 不存在数据
  89. if ( !$list ) {
  90. // 从数据库获取数据
  91. $data = $this->query()->where([['status','=',0]])->get(['id','pages','title','image_url','item_id','path']);
  92. // 是否有数据
  93. $data = $data ? $data->toArray() : [];
  94. // 循环处理数据
  95. $list = [];
  96. // 进行更新
  97. foreach ($data as $value) {
  98. // 数据处理
  99. $value['image_url'] = $value['image_url'] ? path_compat($value['image_url']) : '';
  100. // 重组数据
  101. $list[$value['pages']][$value['item_id']] = $value;
  102. }
  103. // 存起来
  104. cache(['admin:share:message:list'=>$list]);
  105. }
  106. // 返回结果
  107. return $list;
  108. }
  109. /**
  110. * 获取配置
  111. *
  112. * @param string $pages 页面
  113. *
  114. */
  115. public function getListByPage($pages)
  116. {
  117. // 获取列表数据
  118. $list = $this->getList();
  119. // 获取数据
  120. $list = isset($list[$pages]) ? $list[$pages] : [];
  121. // 返回值
  122. return $list;
  123. }
  124. /**
  125. * 获取配置
  126. *
  127. * @param string $pages 页面
  128. * @param string $field 指定字段
  129. *
  130. */
  131. public function getOneByPage($pages,$itemId=0,$field='')
  132. {
  133. // 获取列表数据
  134. $list = $this->getListByPage($pages);
  135. // 获取数据
  136. $one = isset($list[$itemId]) ? $list[$itemId] : [];
  137. // 返回值
  138. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  139. }
  140. }