feat(admin): 会员列表去角色组/邮箱/等级列,手机号全量展示;新增微信手机号快捷获取接口
需求:后台「会员列表」取消『角色组 / 电子邮箱 / 等级』三列;手机号要全部能取到并展示。 - user/user 列表:移除 group.name / email / level 三列,手机号列移到昵称之后并加格式化: 有绑定号→直接显示;未绑定但有发布信息→显示该会员最近一条发布信息的联系电话并标注「来自发布信息」;两者都没有→显示「未绑定」 - user/user 控制器 index():改用 toArray() 后按行补 mobile_source,未绑定手机号时回落到 fc_post.phone(不含软删) - 新增 A5 POST /api/v1/auth/wx-phone:用小程序 access_token 调 phonenumber.getPhoneNumber 换号写入 fa_user.mobile; access_token 带缓存(fc_config wx.accessToken/wx.accessTokenExpire,提前 5 分钟失效);微信侧失败时给出「请改用手动输入」 的业务文案,前端据此回落 A2 bind-phone —— 修复「微信会员手机号取不到」的根因(此前小程序侧绑定是假实现) - 路由登记:application/route.php 新增 api/v1/auth/wx-phone - tools/verify-api.mjs:新增 A5(缺 code 文案)与 A2(手机号格式)检查
这个提交包含在:
@@ -4,6 +4,7 @@ namespace app\admin\controller\user;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\Auth;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 会员管理
|
||||
@@ -45,11 +46,34 @@ class User extends Backend
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
$rows = [];
|
||||
foreach ($list as $k => $v) {
|
||||
$v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
|
||||
$v->hidden(['password', 'salt']);
|
||||
|
||||
$item = $v->toArray();
|
||||
|
||||
// 手机号全量展示(v1.0.8):
|
||||
// 微信登录注册的会员默认没有绑定手机号(fa_user.mobile 为空),
|
||||
// 此时回落到「该会员最近一条发布信息里的联系电话」,并标记来源 mobile_source=post;
|
||||
// 两者都没有则返回空串,前端显示「未绑定」。
|
||||
$mobile = trim((string)$item['mobile']);
|
||||
if ($mobile === '') {
|
||||
$postPhone = Db::name('fc_post')
|
||||
->where('user_id', $v->id)
|
||||
->where('phone', '<>', '')
|
||||
->whereNull('deletetime')
|
||||
->order('id desc')
|
||||
->value('phone');
|
||||
if ($postPhone) {
|
||||
$item['mobile'] = (string)$postPhone;
|
||||
$item['mobile_source'] = 'post';
|
||||
}
|
||||
}
|
||||
$rows[] = $item;
|
||||
}
|
||||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||||
$result = ["total" => $list->total(), "rows" => $rows];
|
||||
|
||||
return json($result);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ use think\Db;
|
||||
* 用户端 · 认证与登录(Module-AUTH)
|
||||
* 路由:
|
||||
* POST /api/v1/auth/wx-login A1 微信授权登录
|
||||
* POST /api/v1/auth/bind-phone A2 手机号绑定
|
||||
* POST /api/v1/auth/bind-phone A2 手机号绑定(手动输入)
|
||||
* POST /api/v1/auth/wx-phone A5 微信手机号快捷获取(getPhoneNumber)
|
||||
* POST /api/v1/auth/switch-type A3 个人/商家身份切换
|
||||
* GET /api/v1/auth/profile A4 个人资料
|
||||
*/
|
||||
@@ -91,6 +92,96 @@ class Auth extends V1
|
||||
$this->ok(['phone' => $phone]);
|
||||
}
|
||||
|
||||
/**
|
||||
* A5 微信手机号快捷获取
|
||||
* 入参 { code }(`<button open-type="getPhoneNumber">` 回调里的 `e.detail.code`)
|
||||
* → 出参 { phone }
|
||||
* 规则:用小程序 access_token 调 phonenumber.getPhoneNumber 换号并写入 fa_user.mobile;
|
||||
* 未配置微信参数 / 微信侧失败(快捷验证未开通、code 失效、用户拒绝)时返回业务失败,
|
||||
* 前端据此回落到「手动输入手机号」(A2 bindPhone),保证任何情况下都能拿到号码。
|
||||
*/
|
||||
public function wxPhone()
|
||||
{
|
||||
$user = $this->auth();
|
||||
|
||||
$code = trim((string)$this->input('code', ''));
|
||||
if ($code === '') {
|
||||
$this->fail('缺少手机号授权 code');
|
||||
}
|
||||
|
||||
$appid = (string)$this->cfg('wx.appid', '');
|
||||
$secret = (string)$this->cfg('wx.secret', '');
|
||||
if ($appid === '' || $secret === '') {
|
||||
$this->fail('未配置小程序 AppID/AppSecret,请改用手动输入');
|
||||
}
|
||||
|
||||
$token = $this->wxAccessToken($appid, $secret);
|
||||
if ($token === '') {
|
||||
$this->fail('微信 access_token 获取失败,请改用手动输入');
|
||||
}
|
||||
|
||||
$ctx = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => "Content-Type: application/json\r\n",
|
||||
'content' => json_encode(['code' => $code]),
|
||||
'timeout' => 10,
|
||||
'ignore_errors' => true,
|
||||
],
|
||||
]);
|
||||
$res = @json_decode(@file_get_contents(
|
||||
'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' . $token,
|
||||
false,
|
||||
$ctx
|
||||
), true);
|
||||
|
||||
$phone = '';
|
||||
if (!empty($res['phone_info']['phoneNumber'])) {
|
||||
$phone = (string)$res['phone_info']['phoneNumber'];
|
||||
} elseif (!empty($res['phone_info']['purePhoneNumber'])) {
|
||||
$phone = (string)$res['phone_info']['purePhoneNumber'];
|
||||
}
|
||||
if (!preg_match('/^1\d{10}$/', $phone)) {
|
||||
// 常见原因:手机号快捷验证未开通/额度用尽、code 已失效、用户拒绝授权
|
||||
$this->fail('微信手机号获取失败,请改用手动输入');
|
||||
}
|
||||
|
||||
Db::name('user')->where('id', $user['id'])->update([
|
||||
'mobile' => $phone, 'updatetime' => time(),
|
||||
]);
|
||||
|
||||
$this->ok(['phone' => $phone]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序 access_token(带缓存,提前 5 分钟失效)
|
||||
* 缓存写入 fc_config 的 wx.accessToken / wx.accessTokenExpire(仅服务端读取,不下发前端)
|
||||
* @param string $appid
|
||||
* @param string $secret
|
||||
* @return string 失败返回空串
|
||||
*/
|
||||
protected function wxAccessToken($appid, $secret)
|
||||
{
|
||||
$now = time();
|
||||
$cached = (string)$this->cfg('wx.accessToken', '');
|
||||
$expire = (int)$this->cfg('wx.accessTokenExpire', 0);
|
||||
if ($cached !== '' && $expire > $now + 300) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential'
|
||||
. '&appid=' . $appid . '&secret=' . $secret;
|
||||
$res = @json_decode(@file_get_contents($url), true);
|
||||
if (empty($res['access_token'])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->setCfg('wx.accessToken', (string)$res['access_token']);
|
||||
$this->setCfg('wx.accessTokenExpire', $now + (int)(!empty($res['expires_in']) ? $res['expires_in'] : 7200));
|
||||
|
||||
return (string)$res['access_token'];
|
||||
}
|
||||
|
||||
/**
|
||||
* A3 用户类型切换
|
||||
* 入参 { userType: 1|2, license? } → 出参 { userType, needAudit }
|
||||
|
||||
@@ -31,6 +31,7 @@ return [
|
||||
/* =============== 一、用户端 · 认证与登录(Module-AUTH) =============== */
|
||||
'api/v1/auth/wx-login' => 'api/Auth/wxLogin',
|
||||
'api/v1/auth/bind-phone' => 'api/Auth/bindPhone',
|
||||
'api/v1/auth/wx-phone' => 'api/Auth/wxPhone',
|
||||
'api/v1/auth/switch-type' => 'api/Auth/switchType',
|
||||
'api/v1/auth/profile' => 'api/Auth/profile',
|
||||
|
||||
|
||||
@@ -27,13 +27,22 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
|
||||
[
|
||||
{checkbox: true},
|
||||
{field: 'id', title: __('Id'), sortable: true},
|
||||
{field: 'group.name', title: __('Group')},
|
||||
{field: 'username', title: __('Username'), operate: 'LIKE'},
|
||||
{field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
|
||||
{field: 'email', title: __('Email'), operate: 'LIKE'},
|
||||
{field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
|
||||
{
|
||||
field: 'mobile', title: __('Mobile'), operate: 'LIKE',
|
||||
// 手机号全量展示(v1.0.8):未绑定手机号时后端回落「该会员最近一条发布信息里的联系电话」,
|
||||
// 并标记 mobile_source=post 以区分来源;两者都没有则显示「未绑定」
|
||||
formatter: function (value, row) {
|
||||
if (!value) {
|
||||
return '<span class="text-muted">未绑定</span>';
|
||||
}
|
||||
return value + (row.mobile_source === 'post'
|
||||
? ' <span class="label label-info">来自发布信息</span>'
|
||||
: '');
|
||||
}
|
||||
},
|
||||
{field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
|
||||
{field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
|
||||
{field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
|
||||
{field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
|
||||
{field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
|
||||
|
||||
@@ -118,6 +118,10 @@ async function userAuth() {
|
||||
await ok200('A4 GET /auth/profile', 'GET', '/auth/profile', null, true,
|
||||
(d) => (d && typeof d.balance === 'number' && 'isMember' in d ? '' : '缺 balance/isMember'))
|
||||
await expect401('A4 · 未登录应 401', 'GET', '/auth/profile', null)
|
||||
|
||||
// A5 微信手机号快捷获取(v1.0.8):无 code 必须给出明确文案(有 code 需真实微信凭证,无法在脚本内构造)
|
||||
await expectFail('A5 · 手机号快捷获取缺 code', 'POST', '/auth/wx-phone', {}, true, '缺少手机号授权 code')
|
||||
await expectFail('A2 · 手机号格式 (BR-02)', 'POST', '/auth/bind-phone', { phone: '123' }, true, '手机号格式不正确')
|
||||
}
|
||||
|
||||
/* ---------------- 二、首页与展示(HOME 4) ---------------- */
|
||||
|
||||
在新工单中引用
屏蔽一个用户