文件
yjyf/server/application/common/library/WxLogin.php
linda 4e73c8164a fix(wxlogin): 修复微信登录 40029 + 新增商家(企业账号)全流程
微信登录修复:
- 根因: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)
2026-10-28 16:22:20 +08:00

113 行
4.0 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\common\library;
use think\Db;
/**
* 微信登录
* - jscode2session:小程序 code 换 openid
* - provision:按 openid 落库(fa_user + fa_user_wechat),幂等
*/
class WxLogin
{
/**
* 小程序 code 换取 session
* @return array|false 成功返回 ['openid'=>..,'session_key'=>..,'unionid'=>..]
*/
public static function jscode2session($code)
{
$appid = \think\Env::get('wechat.appid');
$secret = \think\Env::get('wechat.secret');
if (!$appid || !$secret) {
return ['error' => '微信登录未配置'];
}
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid
. '&secret=' . $secret
. '&js_code=' . urlencode($code)
. '&grant_type=authorization_code';
$ctx = stream_context_create(['http' => ['timeout' => 8, 'ignore_errors' => true]]);
$resp = @file_get_contents($url, false, $ctx);
if ($resp === false) {
return ['error' => '微信服务不可达,请稍后重试'];
}
$wx = json_decode($resp, true);
if (empty($wx['openid'])) {
$ec = isset($wx['errcode']) ? (int)$wx['errcode'] : 0;
$em = isset($wx['errmsg']) ? $wx['errmsg'] : '未知错误';
$hint = [
40029 => '小程序 appid 与服务端配置不一致:请确认 manifest.json 的 mp-weixin.appid 为 ' . $appid,
40125 => 'appsecret 配置错误,请检查 .env 的 [wechat] secret',
45011 => '登录请求过于频繁,请稍后重试',
40226 => '该微信账号被风控限制,请稍后再试',
];
$msg = isset($hint[$ec]) ? $hint[$ec] : ('微信登录失败(' . $ec . '):' . $em);
return ['error' => $msg, 'errcode' => $ec];
}
return $wx;
}
/**
* 按 openid 落库(幂等)
* @return array ['user_id' => int, 'is_new' => 0|1]
*/
public static function provision($openid, $unionid = '', $sessionKey = '', $nickname = '', $avatar = '')
{
$openid = (string)$openid;
$bind = Db::name('user_wechat')->where('openid', $openid)->find();
$userId = $bind ? (int)$bind['user_id'] : 0;
// 绑定记录存在但用户已被删除 → 重建
if ($userId > 0 && !Db::name('user')->where('id', $userId)->find()) {
$userId = 0;
}
$isNew = 0;
if ($userId === 0) {
$isNew = 1;
$userId = Db::name('user')->insertGetId([
'group_id' => 1,
'username' => 'wx' . substr(md5($openid), 0, 10),
'nickname' => $nickname !== '' ? mb_substr($nickname, 0, 20) : ('用户' . substr($openid, -4)),
'avatar' => $avatar,
'status' => 'normal',
'jointime' => time(),
'createtime' => time(),
'updatetime' => time(),
]);
}
$data = [
'user_id' => $userId,
'openid' => $openid,
'unionid' => (string)$unionid,
'session_key' => (string)$sessionKey,
'updatetime' => time(),
];
if ($nickname !== '') {
$data['nickname'] = mb_substr($nickname, 0, 64);
}
if ($avatar !== '') {
$data['avatar'] = $avatar;
}
if ($bind) {
Db::name('user_wechat')->where('id', $bind['id'])->update($data);
} else {
$data['createtime'] = time();
Db::name('user_wechat')->insert($data);
}
$userUpdate = ['updatetime' => time()];
if ($nickname !== '') {
$userUpdate['nickname'] = mb_substr($nickname, 0, 20);
}
if ($avatar !== '') {
$userUpdate['avatar'] = $avatar;
}
Db::name('user')->where('id', $userId)->update($userUpdate);
return ['user_id' => $userId, 'is_new' => $isNew];
}
}