feat: 新增地址选择插件及订阅消息等多项功能优化

- 新增地址选择插件,支持地图位置选择并返回地址和经纬度
- 新增微信订阅消息发送功能,支持审核结果、提现通知等场景
- 新增订单支付状态查询、提现收款人姓名保存等接口
- 新增服务区域管理,支持按省市区划分并同步到信息发布
- 新增资金流水和营收报表导出功能
- 新增可用优惠券查询、订单退款拆分处理等功能
- 优化后台角标显示、首页数据统计、UI配置下发等
- 修复订单支付并发问题、地区管理逻辑等
这个提交包含在:
2026-10-22 17:14:56 +08:00
父节点 088a08d43a
当前提交 cb12bf5fc8
共有 180 个文件被更改,包括 9695 次插入 和 1680 次删除

查看文件

@@ -4,6 +4,7 @@ namespace app\api\controller;
use think\Db;
use app\common\library\DistributionService;
use app\common\library\WxMini;
/**
* 用户端 · 认证与登录(Module-AUTH)
@@ -31,6 +32,9 @@ class Auth extends V1
$this->fail('缺少微信授权 code');
}
// 频率限制:同一 IP 每分钟最多 30 次授权登录
$this->rateLimit('wxlogin:' . $this->request->ip(), 30, 60);
$nickname = trim((string)$this->input('nickname', ''));
$avatar = trim((string)$this->input('avatar', ''));
if (mb_strlen($nickname) > 32) {
@@ -42,7 +46,12 @@ class Auth extends V1
$manualPhone = trim((string)$this->input('phone', ''));
$phone = '';
if ($phoneCode !== '') {
$phone = $this->resolveWxPhone($phoneCode);
try {
$phone = WxMini::phoneNumber($phoneCode);
} catch (\Throwable $e) {
// 手机号属于可选增强:拿不到不阻断登录,前端可回落到手动绑定(A2 bindPhone)
$phone = '';
}
} elseif ($manualPhone !== '' && preg_match('/^1\d{10}$/', $manualPhone)) {
$phone = $manualPhone;
}
@@ -51,70 +60,86 @@ class Auth extends V1
$inviterId = (int)$this->input('inviterId', $this->input('inviter_id', 0));
$inviteCode = trim((string)$this->input('inviteCode', $this->input('invite_code', '')));
$ext = Db::name('fc_user_ext')->where('openid', $openid)->find();
$user = $ext ? Db::name('user')->where('id', $ext['user_id'])->find() : null;
// 同一 openid 串行建号:避免并发首次登录建出两个账号(usage: MySQL GET_LOCK)
$lockName = 'fc_wxlogin_' . md5($openid);
Db::query('SELECT GET_LOCK(?, 5)', [$lockName]);
if (!$user) {
// 首次登录:自动建号(有微信昵称/头像就落库,否则用占位昵称 + 字母头像)
$uid = Db::name('user')->insertGetId([
'group_id' => 1,
'username' => 'wx' . substr(md5($openid), 0, 10),
'nickname' => $nickname !== '' ? $nickname : '同城用户' . mt_rand(1000, 9999),
'password' => md5(mt_rand()),
'salt' => 'wx',
'email' => substr(md5($openid), 0, 12) . '@wx.local',
'mobile' => $phone,
'avatar' => $avatar,
'level' => 1,
'money' => 0,
'score' => 0,
'status' => 'normal',
'jointime' => time(),
'joinip' => $this->request->ip(),
'createtime' => time(),
'updatetime' => time(),
]);
Db::name('fc_user_ext')->insert([
'user_id' => $uid, 'openid' => $openid, 'user_type' => 1, 'realname_status' => 0,
'is_member' => 0, 'member_level' => '', 'member_expiretime' => null,
'invite_code' => strtoupper(substr(md5('fc' . $uid), 0, 6)), 'inviter_id' => 0,
'free_view_date' => date('Y-m-d'), 'free_view_used' => 0,
'createtime' => time(), 'updatetime' => time(),
]);
// 首次登录:绑定分销推荐关系
DistributionService::bindRelation($uid, $inviterId, $inviteCode);
$user = Db::name('user')->where('id', $uid)->find();
} else {
// 老用户:若未绑定过推荐人,本次传入有效推荐人则尝试绑定
if ($inviterId > 0 || $inviteCode !== '') {
DistributionService::bindRelation($user['id'], $inviterId, $inviteCode);
try {
Db::startTrans();
$ext = Db::name('fc_user_ext')->where('openid', $openid)->find();
$user = $ext ? Db::name('user')->where('id', $ext['user_id'])->find() : null;
if (!$user) {
// 首次登录:自动建号(有微信昵称/头像就落库,否则用占位昵称 + 字母头像)
$uid = Db::name('user')->insertGetId([
'group_id' => 1,
'username' => 'wx' . substr(md5($openid), 0, 10),
'nickname' => $nickname !== '' ? $nickname : '同城用户' . mt_rand(1000, 9999),
'password' => md5(mt_rand()),
'salt' => 'wx',
'email' => substr(md5($openid), 0, 12) . '@wx.local',
'mobile' => $phone,
'avatar' => $avatar,
'level' => 1,
'money' => 0,
'score' => 0,
'status' => 'normal',
'jointime' => time(),
'joinip' => $this->request->ip(),
'createtime' => time(),
'updatetime' => time(),
]);
Db::name('fc_user_ext')->insert([
'user_id' => $uid, 'openid' => $openid, 'user_type' => 1, 'realname_status' => 0,
'is_member' => 0, 'member_level' => '', 'member_expiretime' => null,
'invite_code' => strtoupper(substr(md5('fc' . $uid), 0, 6)), 'inviter_id' => 0,
'free_view_date' => date('Y-m-d'), 'free_view_used' => 0,
'createtime' => time(), 'updatetime' => time(),
]);
// 首次登录:绑定分销推荐关系
DistributionService::bindRelation($uid, $inviterId, $inviteCode);
$user = Db::name('user')->where('id', $uid)->find();
} else {
// 老用户:若未绑定过推荐人,本次传入有效推荐人则尝试绑定
if ($inviterId > 0 || $inviteCode !== '') {
DistributionService::bindRelation($user['id'], $inviterId, $inviteCode);
}
// 老用户:仅在本次带上了新值且确实不同时更新(不覆盖运营在后台改过的资料)
$update = [];
if ($nickname !== '' && $nickname !== (string)$user['nickname']) {
$update['nickname'] = $nickname;
}
if ($avatar !== '' && $avatar !== (string)$user['avatar']) {
$update['avatar'] = $avatar;
}
if ($phone !== '' && (string)$user['mobile'] === '') {
$update['mobile'] = $phone;
}
if ($update) {
$update['updatetime'] = time();
Db::name('user')->where('id', $user['id'])->update($update);
$user = Db::name('user')->where('id', $user['id'])->find();
}
}
// 老用户:仅在本次带上了新值且确实不同时更新(不覆盖运营在后台改过的资料)
$update = [];
if ($nickname !== '' && $nickname !== (string)$user['nickname']) {
$update['nickname'] = $nickname;
}
if ($avatar !== '' && $avatar !== (string)$user['avatar']) {
$update['avatar'] = $avatar;
}
if ($phone !== '' && (string)$user['mobile'] === '') {
$update['mobile'] = $phone;
}
if ($update) {
$update['updatetime'] = time();
Db::name('user')->where('id', $user['id'])->update($update);
$user = Db::name('user')->where('id', $user['id'])->find();
if ($user['status'] != 'normal') {
throw new \RuntimeException('账号已被禁用,请联系客服');
}
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
Db::query('SELECT RELEASE_LOCK(?)', [$lockName]);
$this->fail($e->getMessage() ?: '微信登录失败,请重试');
}
if ($user['status'] != 'normal') {
$this->fail('账号已被禁用,请联系客服');
}
Db::query('SELECT RELEASE_LOCK(?)', [$lockName]);
$token = $this->issueToken($user['id']);
$this->ok([
'token' => $token,
'refreshToken' => md5($token . 'refresh'),
'refreshToken' => $this->issueRefreshToken($user['id']),
'userInfo' => $this->userInfo($user),
]);
}
@@ -141,7 +166,7 @@ class Auth extends V1
* A5 微信手机号快捷获取
* 入参 { code }(`<button open-type="getPhoneNumber">` 回调里的 `e.detail.code`)
* → 出参 { phone }
* 规则:用小程序 access_token 调 phonenumber.getPhoneNumber 换号并写入 fa_user.mobile;
* 规则:由 EasyWeChat 的 `getuserphonenumber` 换号并写入 fa_user.mobile(见 \app\common\library\WxMini);
* 未配置微信参数 / 微信侧失败(快捷验证未开通、code 失效、用户拒绝)时返回业务失败,
* 前端据此回落到「手动输入手机号」(A2 bindPhone),保证任何情况下都能拿到号码。
*/
@@ -154,29 +179,11 @@ class Auth extends V1
$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 获取失败,请改用手动输入');
}
$url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' . $token;
$res = $this->wxCurlPost($url, ['code' => $code]);
$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)) {
try {
$phone = WxMini::phoneNumber($code);
} catch (\Throwable $e) {
// 常见原因:手机号快捷验证未开通/额度用尽、code 已失效、用户拒绝授权
$this->fail('微信手机号获取失败,请改用手动输入');
$this->fail($e->getMessage() ?: '微信手机号获取失败,请改用手动输入');
}
Db::name('user')->where('id', $user['id'])->update([
@@ -186,35 +193,6 @@ class Auth extends V1
$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 = $this->wxCurlGet($url);
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 }
@@ -325,58 +303,19 @@ class Auth extends V1
/**
* code -> openid
* 已配置微信参数时走 jscode2session;未配置(本地/联调)时用确定性派生,便于前后端联调。
* 由 EasyWeChat SDK 调 jscode2session(\app\common\library\WxMini)。
* 兼容:本地未配置 AppID/AppSecret 且开启 debug 时,用确定性派生便于前后端联调。
*/
protected function resolveOpenid($code)
{
$appid = (string)$this->cfg('wx.appid', '');
$secret = (string)$this->cfg('wx.secret', '');
if ($appid && $secret) {
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid
. '&secret=' . $secret . '&js_code=' . urlencode($code) . '&grant_type=authorization_code';
$res = $this->wxCurlGet($url);
if (!empty($res['openid'])) {
return $res['openid'];
try {
return WxMini::openid($code);
} catch (\Throwable $e) {
if (!WxMini::isConfigured() && config('app_debug')) {
return strpos($code, 'openid_') === 0 ? $code : 'openid_' . md5($code);
}
$this->fail($e->getMessage() ?: '微信登录失败,请重试');
}
// 未配置微信参数:若 code 本身即为 openid 标识则直接采用,否则派生
if (strpos($code, 'openid_') === 0) {
return $code;
}
return 'openid_' . md5($code);
}
/**
* 微信手机号快捷 code 解析为真实手机号
* @param string $phoneCode 微信开放能力获取的临时凭证
* @return string 成功返回11位手机号,失败返回空字符串
*/
protected function resolveWxPhone($phoneCode)
{
if ($phoneCode === '') {
return '';
}
$appid = (string)$this->cfg('wx.appid', '');
$secret = (string)$this->cfg('wx.secret', '');
if ($appid === '' || $secret === '') {
return '';
}
$token = $this->wxAccessToken($appid, $secret);
if ($token === '') {
return '';
}
$url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' . $token;
$res = $this->wxCurlPost($url, ['code' => $phoneCode]);
$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'];
}
return preg_match('/^1\d{10}$/', $phone) ? $phone : '';
}
/**
@@ -522,6 +461,66 @@ class Auth extends V1
$this->ok(['auditStatus' => 0, 'needAudit' => true], '商家资质已提交,等待平台审核');
}
/**
* A11 保存收款人真实姓名(提现用)
* POST /api/v1/auth/realname {realname}
* 规则:2-20 位中文(含 ·),需与微信实名一致,否则商家转账会校验失败
*/
public function realname()
{
$user = $this->auth();
$realname = trim((string)$this->input('realname', ''));
if (!preg_match('/^[\x{4e00}-\x{9fa5}·]{2,20}$/u', $realname)) {
$this->fail('请输入 2-20 位中文真实姓名');
}
$this->ext($user['id']);
Db::name('fc_user_ext')->where('user_id', $user['id'])->update([
'realname' => $realname,
'realname_status' => 1,
'updatetime' => time(),
]);
$this->ok(['realname' => $realname]);
}
/**
* A10 刷新访问令牌
* POST /api/v1/auth/refresh { refreshToken }
* 规则:refreshToken 必须为 rf_ 前缀且未过期;换新后旧刷新令牌立即作废(轮换)
*/
public function refresh()
{
$refresh = trim((string)$this->input('refreshToken', $this->input('refresh_token', '')));
if ($refresh === '') {
$this->fail('缺少 refreshToken');
}
$this->rateLimit('refresh:' . $this->request->ip(), 20, 60);
if (strpos($refresh, 'rf_') !== 0) {
$this->fail('refreshToken 无效');
}
$row = Db::name('user_token')->where('token', $refresh)->find();
if (!$row || (int)$row['expiretime'] < time()) {
$this->fail('登录已过期,请重新登录', 401, 401);
}
$user = Db::name('user')->where('id', (int)$row['user_id'])->find();
if (!$user || $user['status'] !== 'normal') {
$this->fail('账号状态异常,请重新登录', 401, 401);
}
// 轮换:旧刷新令牌立即失效(fa_user_token 主键是 token,没有 id 列)
Db::name('user_token')->where('token', $refresh)->delete();
$this->ok([
'token' => $this->issueToken($user['id']),
'refreshToken' => $this->issueRefreshToken($user['id']),
]);
}
/**
* A9 手机号密码登录(支持 H5 / 调试环境)
* POST /api/v1/auth/password-login
@@ -543,55 +542,28 @@ class Auth extends V1
$this->fail('密码长度不能少于 6 位');
}
// 频率限制:同一手机号 + IP,1 分钟最多 5 次
$this->rateLimit('pwdlogin:' . $phone . ':' . $this->request->ip(), 5, 60);
$inviterId = (int)$this->input('inviterId', $this->input('inviter_id', 0));
$inviteCode = trim((string)$this->input('inviteCode', $this->input('invite_code', '')));
// 查用户
// 查用户:不存在直接失败,不再自动注册(防止批量造号)
$user = Db::name('user')->where('mobile', $phone)->find();
if (!$user) {
// 为方便 H5 快速调试,如果手机号不存在,则自动初始化该测试账号
$salt = substr(md5(uniqid(rand(), true)), 0, 4);
$hashPwd = md5(md5($password) . $salt);
$uid = Db::name('user')->insertGetId([
'group_id' => 1,
'username' => 'u' . $phone,
'nickname' => '同城用户' . substr($phone, -4),
'password' => $hashPwd,
'salt' => $salt,
'email' => $phone . '@local.phone',
'mobile' => $phone,
'avatar' => '',
'level' => 1,
'money' => 0,
'score' => 0,
'status' => 'normal',
'jointime' => time(),
'joinip' => $this->request->ip(),
'createtime' => time(),
'updatetime' => time(),
]);
Db::name('fc_user_ext')->insert([
'user_id' => $uid, 'openid' => 'h5_' . md5($phone), 'user_type' => 1, 'realname_status' => 0,
'is_member' => 0, 'member_level' => '', 'member_expiretime' => null,
'invite_code' => strtoupper(substr(md5('fc' . $uid), 0, 6)), 'inviter_id' => 0,
'free_view_date' => date('Y-m-d'), 'free_view_used' => 0,
'createtime' => time(), 'updatetime' => time(),
]);
// 首次登录:绑定分销推荐关系
DistributionService::bindRelation($uid, $inviterId, $inviteCode);
$user = Db::name('user')->where('id', $uid)->find();
} else {
// 老用户:若未绑定过推荐人,本次传入有效推荐人则尝试绑定
if ($inviterId > 0 || $inviteCode !== '') {
DistributionService::bindRelation($user['id'], $inviterId, $inviteCode);
}
// 校验密码(支持初始密码 123456 或正确加盐密码)
$salt = isset($user['salt']) ? (string)$user['salt'] : '';
$checkHash = md5(md5($password) . $salt);
$isCorrect = ($user['password'] === $checkHash) || ($password === '123456');
if (!$isCorrect) {
$this->fail('密码错误,请重新输入');
}
$this->fail('手机号未注册,请先使用微信登录');
}
// 老用户:若未绑定过推荐人,本次传入有效推荐人则尝试绑定
if ($inviterId > 0 || $inviteCode !== '') {
DistributionService::bindRelation($user['id'], $inviterId, $inviteCode);
}
// 校验密码:只认加盐哈希(已移除历史遗留的 123456 万能密码)
$salt = isset($user['salt']) ? (string)$user['salt'] : '';
$checkHash = md5(md5($password) . $salt);
if ((string)$user['password'] !== $checkHash) {
$this->fail('密码错误,请重新输入');
}
if ($user['status'] !== 'normal') {
@@ -601,59 +573,8 @@ class Auth extends V1
$token = $this->issueToken($user['id']);
$this->ok([
'token' => $token,
'refreshToken' => md5($token . 'refresh'),
'refreshToken' => $this->issueRefreshToken($user['id']),
'userInfo' => $this->userInfo($user),
]);
}
/**
* 微信官方 API 专用 cURL GET 请求
* 彻底解决 Windows 与部分 Linux 下 file_get_contents SSL 握手失败问题
*
* @param string $url 目标请求地址
* @param int $timeout 超时时间(秒)
* @return array 解析后的关联数组
*/
protected function wxCurlGet($url, $timeout = 8)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$res = curl_exec($ch);
curl_close($ch);
if (!$res) {
return [];
}
$json = @json_decode($res, true);
return is_array($json) ? $json : [];
}
/**
* 微信官方 API 专用 cURL POST 请求
*
* @param string $url 目标请求地址
* @param array $data 提交的 JSON 结构数据
* @param int $timeout 超时时间(秒)
* @return array 解析后的关联数组
*/
protected function wxCurlPost($url, array $data, $timeout = 8)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8']);
$res = curl_exec($ch);
curl_close($ch);
if (!$res) {
return [];
}
$json = @json_decode($res, true);
return is_array($json) ? $json : [];
}
}