www.maiwd.cn // +---------------------------------------------------------------------- namespace app\api\controller\wdsxh\member; use app\common\controller\Api; use think\Collection; class MemberBenefits extends Api { protected $noNeedLogin = ['join_config', 'index']; protected $noNeedRight = ['*']; protected $join_config_model = null; protected $level_model = null; protected $benefits_model = null; protected $article_model = null; public function _initialize() { parent::_initialize(); $this->join_config_model = new \app\api\model\wdsxh\member\JoinConfig(); $this->level_model = new \app\api\model\wdsxh\member\Level(); $this->benefits_model = new \app\api\model\wdsxh\member\MemberBenefits(); $this->article_model = new \app\api\model\wdsxh\article\Article(); } /** * 入会类型 * * @param type $param * @return \think\Response */ public function join_config() { $list = $this->join_config_model->field(['id', 'name']) ->where('status', 'normal') ->order(['weigh' => 'asc', 'id' => 'asc']) ->select(); $this->success('获取成功', $list); } /** * 根据入会类型查询会员级别及权益(每个级别下展示入会类型所有权益,并标记拥有状态) * * @param int $join_config_id 入会类型ID * @return \think\Response */ public function index() { $join_config_id = $this->request->param('join_config_id', 0); if (empty($join_config_id)) { $this->error('入会类型参数不能为空'); } // 查询该入会类型下的所有会员级别(按权重升序) $levels = $this->level_model ->field(['id', 'join_config_id', 'benefits_id', 'name', 'image', 'fees', 'content']) ->where('join_config_id', $join_config_id) ->where('status', 'normal') ->order('weigh', 'asc') ->select(); // 查询该入会类型下的所有会员权益(按排序值升序,排除禁用的) $all_benefits = $this->benefits_model ->field(['id', 'join_config_id', 'title', 'image', 'desc']) ->where('join_config_id', $join_config_id) ->where('disabled', 0) ->order('sort', 'asc') ->select(); // 为每个级别构建完整的权益列表(含 has 标记) $result_levels = []; foreach ($levels as $level) { $owned_ids = json_decode($level['benefits_id'], true) ?: []; $owned_ids = array_map('intval', $owned_ids); $benefits_with_flag = []; foreach ($all_benefits as $key => $val) { $benefits_with_flag[] = [ 'id' => $val['id'], 'join_config_id' => $val['join_config_id'], 'title' => $val['title'], 'image' => $val['image'], 'desc' => $val['desc'], 'has' => !empty($owned_ids) && in_array($val['id'], $owned_ids, true), ]; } $level['benefits'] = $benefits_with_flag; $result_levels[] = $level; } // 会员动态 $result_articles = $this->article_model->alias('a') ->field(['a.id', 'a.type', 'a.title', 'a.image', 'a.link', 'a.createtime']) ->join('wdsxh_article_cat c', 'c.id = a.cat_id') ->where('c.id', 4) ->where('c.status', '1') ->where('a.status', '1') ->order([ 'a.weigh' => 'desc', 'a.id' => 'desc' ]) ->find(); $this->success('请求成功', [ 'levels_benefits' => $result_levels, 'articles' => $result_articles, ]); } }