122 行
4.3 KiB
PHP
122 行
4.3 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 后台管理端 · 会员管理(Module-ADM-MEMBER)
|
||
* 路由:
|
||
* GET /api/v1/admin/users AMB1 用户列表
|
||
* POST /api/v1/admin/member/benefit AMB2 会员权益配置
|
||
*/
|
||
class AdminMember extends V1
|
||
{
|
||
/** 管理端接口必须校验管理员身份(V1 默认关闭了 URI 权限校验) */
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->adminOnly();
|
||
}
|
||
|
||
/**
|
||
* AMB1 用户列表
|
||
* GET /api/v1/admin/users 入参 {page,size}
|
||
* → 分页 {list:[{id,nickname,phone,userType,isMember,status,registerAt}],page,size,total,hasMore}
|
||
*/
|
||
public function users()
|
||
{
|
||
// 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象
|
||
list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) {
|
||
$q = Db::name('user')->alias('u')
|
||
->join('fc_user_ext e', 'e.user_id = u.id', 'LEFT');
|
||
if ($forCount) {
|
||
return $q;
|
||
}
|
||
return $q->field('u.id,u.nickname,u.mobile,u.status,u.jointime,e.user_type,e.is_member,e.member_expiretime')
|
||
->order('u.id desc');
|
||
});
|
||
|
||
$now = time();
|
||
$list = [];
|
||
foreach ($rows as $row) {
|
||
$isMember = (int)$row['is_member'] === 1
|
||
&& !empty($row['member_expiretime']) && (int)$row['member_expiretime'] > $now;
|
||
$list[] = [
|
||
'id' => (int)$row['id'],
|
||
'nickname' => (string)$row['nickname'],
|
||
'phone' => (string)$row['mobile'],
|
||
'userType' => (int)$row['user_type'],
|
||
'isMember' => $isMember,
|
||
'status' => $row['status'] === 'normal' ? 1 : 0,
|
||
'registerAt' => $this->iso($row['jointime']),
|
||
];
|
||
}
|
||
|
||
$this->ok($this->paginate($list, $total, $page, $size));
|
||
}
|
||
|
||
/**
|
||
* AMB2 会员权益配置
|
||
* POST /api/v1/admin/member/benefit 入参 {freeView,postDiscount,distRate,benefits[]}
|
||
* → {freeView,postDiscount,distRate,benefits[]}
|
||
* 未被传入的字段保持原值;freeView <0 报错
|
||
*/
|
||
public function saveBenefit()
|
||
{
|
||
$current = $this->cfg('member.benefit', []);
|
||
if (!is_array($current)) {
|
||
$current = [];
|
||
}
|
||
|
||
$freeView = $this->input('freeView', null);
|
||
if ($freeView !== null && $freeView !== '') {
|
||
if ((int)$freeView < 0) {
|
||
$this->fail('免费查看条数不能为负');
|
||
}
|
||
$current['freeView'] = (int)$freeView;
|
||
}
|
||
|
||
$postDiscount = $this->input('postDiscount', null);
|
||
if ($postDiscount !== null && $postDiscount !== '') {
|
||
$current['postDiscount'] = (string)$postDiscount;
|
||
}
|
||
|
||
$distRate = $this->input('distRate', null);
|
||
if ($distRate !== null && $distRate !== '') {
|
||
$current['distRate'] = (string)$distRate;
|
||
}
|
||
|
||
// 注意:benefits 可能是数组,不能走 $this->input()(TP5.0 Request::input 默认按字符串强转,
|
||
// 传入数组会抛 "variable type error:array");统一从 inputs() 取原始值。
|
||
$allInputs = $this->inputs();
|
||
$benefits = array_key_exists('benefits', $allInputs) ? $allInputs['benefits'] : null;
|
||
if ($benefits !== null && $benefits !== '') {
|
||
if (is_array($benefits)) {
|
||
$current['benefits'] = array_values($benefits);
|
||
} else {
|
||
$decoded = json_decode((string)$benefits, true);
|
||
$current['benefits'] = is_array($decoded) ? $decoded : [(string)$benefits];
|
||
}
|
||
}
|
||
|
||
$defaults = [
|
||
'freeView' => 20,
|
||
'postDiscount' => '8 折',
|
||
'distRate' => '一级 20% / 二级 10%',
|
||
'benefits' => [],
|
||
];
|
||
$current = array_merge($defaults, $current);
|
||
|
||
$this->setCfg('member.benefit', $current);
|
||
$this->adminLog('修改会员权益配置');
|
||
|
||
$this->ok([
|
||
'freeView' => (int)$current['freeView'],
|
||
'postDiscount' => (string)$current['postDiscount'],
|
||
'distRate' => (string)$current['distRate'],
|
||
'benefits' => array_values((array)$current['benefits']),
|
||
]);
|
||
}
|
||
}
|