diff --git a/application/api/controller/Auth.php b/application/api/controller/Auth.php index 15af289..74119ec 100644 --- a/application/api/controller/Auth.php +++ b/application/api/controller/Auth.php @@ -10,15 +10,18 @@ use think\Db; * POST /api/v1/auth/wx-login A1 微信授权登录 * POST /api/v1/auth/bind-phone A2 手机号绑定(手动输入) * POST /api/v1/auth/wx-phone A5 微信手机号快捷获取(getPhoneNumber) + * POST /api/v1/auth/avatar A6 头像上传(用户端) * POST /api/v1/auth/switch-type A3 个人/商家身份切换 - * GET /api/v1/auth/profile A4 个人资料 + * GET|POST /api/v1/auth/profile A4 个人资料(GET 读取 / POST 更新昵称与头像) */ class Auth extends V1 { /** * A1 微信一键授权登录 - * 入参 { code } → 出参 { token, refreshToken, userInfo } - * 规则:code 必填;首次登录自动建档并生成邀请码 + * 入参 { code, nickname?, avatar? } → 出参 { token, refreshToken, userInfo } + * 规则:code 必填;首次登录自动建档并生成邀请码; + * v1.0.9 起支持携带微信昵称/头像(小程序用 `open-type="chooseAvatar"` + `` 采集), + * 新用户直接落库、老用户填了就更新,保证后台「会员列表」能看到真实微信昵称与头像。 */ public function wxLogin() { @@ -27,22 +30,28 @@ class Auth extends V1 $this->fail('缺少微信授权 code'); } + $nickname = trim((string)$this->input('nickname', '')); + $avatar = trim((string)$this->input('avatar', '')); + if (mb_strlen($nickname) > 32) { + $nickname = mb_substr($nickname, 0, 32); + } + $openid = $this->resolveOpenid($code); $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' => '同城用户' . mt_rand(1000, 9999), + 'nickname' => $nickname !== '' ? $nickname : '同城用户' . mt_rand(1000, 9999), 'password' => md5(mt_rand()), 'salt' => 'wx', 'email' => substr(md5($openid), 0, 12) . '@wx.local', 'mobile' => '', - 'avatar' => '', + 'avatar' => $avatar, 'level' => 1, 'money' => 0, 'score' => 0, @@ -60,6 +69,20 @@ class Auth extends V1 'createtime' => time(), 'updatetime' => time(), ]); $user = Db::name('user')->where('id', $uid)->find(); + } else { + // 老用户:仅在本次带上了新值且确实不同时更新(不覆盖运营在后台改过的资料) + $update = []; + if ($nickname !== '' && $nickname !== (string)$user['nickname']) { + $update['nickname'] = $nickname; + } + if ($avatar !== '' && $avatar !== (string)$user['avatar']) { + $update['avatar'] = $avatar; + } + 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') { @@ -225,13 +248,68 @@ class Auth extends V1 $this->ok(['userType' => $userType, 'needAudit' => $userType === 2]); } + /** + * A6 头像上传(用户端) + * POST /api/v1/auth/avatar multipart: file + * → { url, avatar } + * 说明:复用 FastAdmin 上传库(与后台上传同一套类型/大小校验与存储); + * `avatar` 返回相对路径(与 fa_user.avatar 存储一致,后台列表用 cdnurl 拼完整地址), + * `url` 为完整地址,供小程序立即展示。 + */ + public function uploadAvatar() + { + $user = $this->auth(); + + $file = $this->request->file('file'); + if (!$file) { + $this->fail('未接收到上传文件'); + } + try { + $upload = new \app\common\library\Upload($file); + $attachment = $upload->upload(); + } catch (\Exception $e) { + $this->fail($e->getMessage()); + } + + $path = (string)$attachment->url; + Db::name('user')->where('id', $user['id'])->update([ + 'avatar' => $path, + 'updatetime' => time(), + ]); + + $this->ok(['url' => cdnurl($path, true), 'avatar' => $path]); + } + /** * A4 个人资料 - * 返回 userInfo(含会员状态、余额) + * GET → userInfo(含会员状态、余额) + * POST { nickname?, avatar? } → 更新后的 userInfo(微信昵称/头像可在登录后随时补填) */ public function profile() { $user = $this->auth(); + + if ($this->request->isPost()) { + $nickname = trim((string)$this->input('nickname', '')); + $avatar = trim((string)$this->input('avatar', '')); + $update = []; + if ($nickname !== '') { + if (mb_strlen($nickname) > 32) { + $nickname = mb_substr($nickname, 0, 32); + } + $update['nickname'] = $nickname; + } + if ($avatar !== '') { + $update['avatar'] = $avatar; + } + if (!$update) { + $this->fail('没有需要更新的资料'); + } + $update['updatetime'] = time(); + Db::name('user')->where('id', $user['id'])->update($update); + $user = Db::name('user')->where('id', $user['id'])->find(); + } + $this->ok($this->userInfo($user)); } diff --git a/application/route.php b/application/route.php index 8bc88a4..bf96a21 100755 --- a/application/route.php +++ b/application/route.php @@ -32,6 +32,7 @@ return [ '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/avatar' => 'api/Auth/uploadAvatar', 'api/v1/auth/switch-type' => 'api/Auth/switchType', 'api/v1/auth/profile' => 'api/Auth/profile', diff --git a/tools/verify-api.mjs b/tools/verify-api.mjs index 532ddfd..628f881 100644 --- a/tools/verify-api.mjs +++ b/tools/verify-api.mjs @@ -109,6 +109,10 @@ async function userAuth() { (d) => (d && d.phone === '13800001111' ? '' : 'phone 不符')) await expectFail('A2 · 手机号格式 (BR-02)', 'POST', '/auth/bind-phone', { phone: '123' }, true, '手机号格式不正确') + // A6 头像上传(v1.0.9):不带文件必须明确报错;A4 资料更新空提交同样受控 + await expectFail('A6 · 头像上传缺文件', 'POST', '/auth/avatar', {}, true, '未接收到上传文件') + await expectFail('A4 · 资料更新空提交', 'POST', '/auth/profile', {}, true, '没有需要更新的资料') + await ok200('A3 POST /auth/switch-type', 'POST', '/auth/switch-type', { userType: 1 }, true, (d) => (d && d.userType === 1 && d.needAudit === false ? '' : 'userType/needAudit 不符')) await expectFail('A3 · 商家缺执照 (BR-01)', 'POST', '/auth/switch-type', { userType: 2 }, true,