微信登录修复: - 根因:manifest.json 的 mp-weixin.appid 为空 → 开发者工具用测试号,code 与服务端 appid 不匹配 - 修复:填入 wx40ca4b02d4d1cb58 - WxLogin 增加错误码语义映射(40029 appid 不一致 / 40125 secret 错 / 45011 频繁 / 40226 风控) - 登录页新增手机号登录兜底通道(微信不可用时业务不被卡住) 商家(企业账号): - 新表 fa_merchant / fa_merchant_order / fa_invoice(纯新增,DB 40→45 表) - Merchant 控制器:apply/info/update/bills/invoices/invoice/bindBatch 入驻含企业信息+营业执照+开票信息+结算方式(预付/月结) 账单按月汇总、开票额度按已付-已开票校验、批量下单按批次归属 - Auth::switchRole 支持 merchant(2001/2002/2003 语义码 + redirect) - Auth::profile 增加 is_merchant / merchant 前端: - 登录页角色扩为 业主/师傅/商家 三类 - pages/merchant/apply 企业入驻;workbench 商家工作台(经营数据+按月账单+近期订单+六宫格) - pages/merchant/batch 批量下单(多地址×多数量)→ 逐单建单支付 → 绑定批次 - pages/merchant/invoice 开票申请与记录 - pages/mine 增加商家入驻/切换商家端入口;角色徽章三态 验证:163 项断言全通过(page_flow 47 + e2e 65 + phase2 27 + phase3 24)
199 行
6.9 KiB
PHP
199 行
6.9 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use fast\Random;
|
||
use think\Db;
|
||
use think\Validate;
|
||
|
||
/**
|
||
* 登录 / 角色 / 个人信息
|
||
*/
|
||
class Auth extends Base
|
||
{
|
||
protected $noNeedLogin = ['login', 'wxlogin'];
|
||
|
||
/**
|
||
* 手机号 + 验证码登录(验证码固定 123456,联调用;接入短信后走 Sms)
|
||
* @ApiMethod (POST)
|
||
*/
|
||
public function login()
|
||
{
|
||
$mobile = $this->request->post('mobile');
|
||
$code = $this->request->post('code');
|
||
if (!$mobile || !$code) {
|
||
$this->error('请填写手机号和验证码');
|
||
}
|
||
if (!Validate::regex($mobile, "^1\d{10}$")) {
|
||
$this->error('手机号格式不正确');
|
||
}
|
||
if ($code !== '123456') {
|
||
$this->error('验证码不正确');
|
||
}
|
||
|
||
$user = \app\common\model\User::getByMobile($mobile);
|
||
if ($user) {
|
||
if ($user['status'] !== 'normal') {
|
||
$this->error('账号已被禁用');
|
||
}
|
||
$this->auth->direct($user['id']);
|
||
} else {
|
||
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
|
||
if (!$ret) {
|
||
$this->error($this->auth->getError() ?: '注册失败');
|
||
}
|
||
}
|
||
|
||
$this->success('登录成功', [
|
||
'token' => $this->auth->getToken(),
|
||
'expires' => 2592000,
|
||
'userinfo' => $this->profileData(),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 我的信息(含角色与师傅状态)
|
||
*/
|
||
public function profile()
|
||
{
|
||
$this->success('', $this->profileData());
|
||
}
|
||
|
||
/**
|
||
* 组装个人信息
|
||
*/
|
||
protected function profileData()
|
||
{
|
||
$uid = $this->uid();
|
||
$user = Db::name('user')->field('id,nickname,mobile,avatar,money,score,createtime')->where('id', $uid)->find();
|
||
$master = Db::name('master')->where('user_id', $uid)->find();
|
||
|
||
$couponCount = Db::name('user_coupon')->where('user_id', $uid)->where('status', 'unused')->count();
|
||
$unread = Db::name('message')->where('receive_type', 'user')->where('receive_id', $uid)->where('is_read', 0)->count();
|
||
|
||
$masterInfo = null;
|
||
if ($master) {
|
||
$masterInfo = [
|
||
'id' => (int)$master['id'],
|
||
'realname' => $master['realname'],
|
||
'verify_status' => $master['verify_status'],
|
||
'audit_msg' => $master['audit_msg'],
|
||
'work_status' => $master['work_status'],
|
||
'level' => (int)$master['level'],
|
||
'score' => (float)$master['score'],
|
||
'order_count' => (int)$master['order_count'],
|
||
'money' => (float)$master['money'],
|
||
'can_take' => $master['verify_status'] === 'pass',
|
||
];
|
||
}
|
||
|
||
$merchant = Db::name('merchant')->where('user_id', $uid)->find();
|
||
$merchantInfo = null;
|
||
if ($merchant) {
|
||
$merchantInfo = [
|
||
'id' => (int)$merchant['id'],
|
||
'name' => $merchant['name'],
|
||
'verify_status' => $merchant['verify_status'],
|
||
'audit_msg' => $merchant['audit_msg'],
|
||
'settle_type' => $merchant['settle_type'],
|
||
'can_order' => $merchant['verify_status'] === 'pass',
|
||
];
|
||
}
|
||
|
||
return [
|
||
'id' => (int)$user['id'],
|
||
'nickname' => $user['nickname'] ?: ('用户' . substr((string)$user['mobile'], -4)),
|
||
'mobile' => $user['mobile'],
|
||
'avatar' => $user['avatar'] ?: '',
|
||
'balance' => (float)$user['money'],
|
||
'coupon_count' => (int)$couponCount,
|
||
'unread_count' => (int)$unread,
|
||
'is_master' => $master ? true : false,
|
||
'master' => $masterInfo,
|
||
'is_merchant' => $merchant ? true : false,
|
||
'merchant' => $merchantInfo,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 微信授权登录(小程序 uni.login 拿到的 code)
|
||
* @ApiMethod (POST)
|
||
*/
|
||
public function wxlogin()
|
||
{
|
||
$code = trim((string)$this->request->post('code'));
|
||
$nickname = trim((string)$this->request->post('nickname'));
|
||
$avatar = trim((string)$this->request->post('avatar'));
|
||
if ($code === '') {
|
||
$this->error('缺少微信登录凭证 code');
|
||
}
|
||
|
||
$wx = \app\common\library\WxLogin::jscode2session($code);
|
||
if (isset($wx['error'])) {
|
||
$this->error($wx['error']);
|
||
}
|
||
$ret = \app\common\library\WxLogin::provision(
|
||
$wx['openid'],
|
||
isset($wx['unionid']) ? $wx['unionid'] : '',
|
||
isset($wx['session_key']) ? $wx['session_key'] : '',
|
||
$nickname,
|
||
$avatar
|
||
);
|
||
$this->auth->direct($ret['user_id']);
|
||
|
||
$this->success('登录成功', [
|
||
'token' => $this->auth->getToken(),
|
||
'expires' => 2592000,
|
||
'userinfo' => $this->profileData(),
|
||
'is_new' => $ret['is_new'],
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 切换角色(用户 / 师傅)
|
||
* @ApiMethod (POST)
|
||
*/
|
||
public function switchRole()
|
||
{
|
||
$role = $this->request->post('role', 'user');
|
||
if (!in_array($role, ['user', 'master', 'merchant'], true)) {
|
||
$this->error('角色参数错误');
|
||
}
|
||
|
||
if ($role === 'master') {
|
||
$master = $this->masterInfo();
|
||
if (!$master) {
|
||
$this->error('请先完成师傅入驻', ['need_apply' => 1], 2001);
|
||
}
|
||
if ($master['verify_status'] === 'pending') {
|
||
$this->error('师傅资质审核中,请耐心等待', ['verify_status' => 'pending'], 2002);
|
||
}
|
||
if ($master['verify_status'] === 'reject') {
|
||
$this->error('资质未通过:' . $master['audit_msg'], ['verify_status' => 'reject'], 2003);
|
||
}
|
||
}
|
||
|
||
if ($role === 'merchant') {
|
||
$merchant = Db::name('merchant')->where('user_id', $this->uid())->find();
|
||
if (!$merchant) {
|
||
$this->error('请先完成商家入驻', ['need_apply' => 1], 2001);
|
||
}
|
||
if ($merchant['verify_status'] === 'pending') {
|
||
$this->error('商家资质审核中,请耐心等待', ['verify_status' => 'pending'], 2002);
|
||
}
|
||
if ($merchant['verify_status'] === 'reject') {
|
||
$this->error('资质未通过:' . $merchant['audit_msg'], ['verify_status' => 'reject'], 2003);
|
||
}
|
||
}
|
||
|
||
$redirect = '/YJYF/YJYF';
|
||
if ($role === 'master') {
|
||
$redirect = '/pages/master/workbench';
|
||
} elseif ($role === 'merchant') {
|
||
$redirect = '/pages/merchant/workbench';
|
||
}
|
||
|
||
$this->success('切换成功', ['role' => $role, 'redirect' => $redirect]);
|
||
}
|
||
}
|