ShareMessage.php 4.4 KB

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