Files
yycea/application/api/controller/wdsxh/member/MemberBenefits.php
2026-04-28 17:37:39 +08:00

144 lines
5.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者,助力商协会发展
// +----------------------------------------------------------------------
// | Copyright (c) 20172024 www.wdsxh.cn All rights reserved.
// +----------------------------------------------------------------------
// | 沃德商协会系统并不是自由软件,不加密,并不代表开源,未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn> 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 $article_model = null;
protected $benefits_model = null;
protected $benefits_project_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->article_model = new \app\api\model\wdsxh\article\Article();
$this->benefits_model = new \app\api\model\wdsxh\member\MemberBenefits();
$this->benefits_project_model = new \app\api\model\wdsxh\member\MemberBenefitsProject();
}
/**
* 入会类型
*
* @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_project', 'name', 'image', 'fees', 'content'])
->where('join_config_id', $join_config_id)
->where('status', 'normal')
->order('weigh', 'asc')
->select();
// 查询该入会类型下的所有会员权益(按排序值升序,排除禁用的)
$levels_id = array_column($levels, 'id');
$all_benefits = $this->benefits_model
->field(['id', 'join_config_id', 'level_id', 'title', 'image', 'desc'])
->where('join_config_id', $join_config_id)
->where('level_id', 'in', $levels_id)
->where('disabled', 0)
->order('sort', 'asc')
->select();
$benefis_maps = [];
foreach ($all_benefits as $key => $val) {
$benefis_maps['b_' . $val['level_id']][] = $val;
}
// 查询该入会类型下的所有会员权益项目(按排序值升序,排除禁用的)
$all_benefits_project = $this->benefits_project_model
->field(['id', 'join_config_id', 'name'])
->where('join_config_id', $join_config_id)
->where('disabled', 0)
->order('sort', 'asc')
->select();
$levels = collection($levels)->toArray();
// 为每个级别构建完整的权益列表(含 has 标记)
$result_levels = [];
foreach ($levels as $level) {
if (!isset($level['benefits'])) {
$level['benefits'] = [];
}
if (!empty($benefis_maps['b_' . $level['id']])) {
$level['benefits'][] = $benefis_maps['b_' . $level['id']];
}
$owned_ids = json_decode($level['benefits_project'], true) ?: [];
$owned_ids = array_map('intval', $owned_ids);
unset($level['benefits_project']);
if (!isset($level['all_benefits_projects'])) {
$level['all_benefits_projects'] = [];
}
foreach ($all_benefits_project as $v) {
$level['all_benefits_projects'][] = [
'id' => $v['id'],
'join_config_id' => $v['join_config_id'],
'name' => $v['name'],
'has' => !empty($owned_ids) && in_array($val['id'], $owned_ids, true),
];
}
$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,
]);
}
}