Recommend.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\recommend;
  12. use app\wap\model\user\User;
  13. use basic\ModelBasic;
  14. use traits\ModelTrait;
  15. class Recommend extends ModelBasic
  16. {
  17. use ModelTrait;
  18. public static function getRecommend()
  19. {
  20. return self::where(['is_fixed' => 1, 'is_show' => 1])
  21. ->order('sort desc,add_time desc')
  22. ->field(['title','icon','type','link','grade_id','id'])
  23. ->select();
  24. }
  25. public static function getRecommendIdAll($uid)
  26. {
  27. $model = self::where(['is_fixed' => 0, 'is_show' => 1]);
  28. if ($grade_id = User::where('uid', $uid)->value('grade_id')) $model = $model->where('grade_id', 'in', [$grade_id, 0]);
  29. $all = $model->field('id,type')->select();
  30. $idsAll = [];
  31. foreach ($all as $item) {
  32. if (RecommendRelation::getRelationCount($item['id'], (int)$item['type'])) array_push($idsAll, $item['id']);
  33. }
  34. return [$idsAll, $grade_id];
  35. }
  36. /**
  37. * 获取主页推荐列表
  38. * $page 分页
  39. * $limit
  40. * */
  41. public static function getContentRecommend($page, $limit, $uid)
  42. {
  43. list($idsAll, $grade_id) = self::getRecommendIdAll($uid);
  44. $model = self::where(['is_fixed' => 0, 'is_show' => 1])->where('id', 'in', $idsAll)
  45. ->field(['id', 'typesetting', 'title', 'type', 'icon', 'image', 'grade_id', 'show_count'])
  46. ->page($page, $limit)->order('sort desc,add_time desc');
  47. if ($grade_id) $model = $model->where('grade_id', $grade_id);
  48. $recommend = $model->select();
  49. $recommend = count($recommend) ? $recommend->toArray() : [];
  50. foreach ($recommend as &$item) {
  51. $item['sum_count'] = RecommendRelation::getRelationCount($item['id'], (int)$item['type']);
  52. $item['list'] = RecommendRelation::getRelationList($item['id'], (int)$item['type'], $item['typesetting'], $item['show_count']);
  53. if ($item['typesetting'] == 4) {
  54. list($ceilCount, $data) = self::getTypesettingList($item['list']);
  55. $item['data'] = $data;
  56. $item['ceilCount'] = $ceilCount;
  57. } else {
  58. $item['data'] = [];
  59. $item['ceilCount'] = 0;
  60. }
  61. $item['courseIndex'] = 1;
  62. }
  63. $page++;
  64. return compact('recommend', 'page');
  65. }
  66. /**
  67. * 获取数组
  68. * @param array $list
  69. * @return array
  70. */
  71. public static function getTypesettingList(array $list)
  72. {
  73. $ceilCount = ceil(count($list) / 3);
  74. $data = [];
  75. for ($i = 0; $i < $ceilCount; $i++) {
  76. $data[] = ['value' => array_slice($list, $i * 3, $i * 3 + 3)];
  77. }
  78. return [$ceilCount, $data];
  79. }
  80. }