文件
yjyf/server/application/common/library/WxLogin.php
linda 1d92202b1a feat: 微信授权登录 + 我的中心角色切换 + 师傅工作台全模块 + 图标美化
登录与角色:
- Auth::wxlogin 微信 code → jscode2session → 落库发 token(appid/secret 走 .env)
- 新增 fa_user_wechat 表,openid 幂等建号(CLI 已验证 is_new 1→0 不重复建号)
- Auth::switchRole 角色切换(未入驻 2001 / 审核中 2002 / 未通过 2003)
- pages/login 改为微信一键授权;pages/mine 我的中心(师傅入口 + 切换师傅端)

师傅工作台(对齐需求文档):
- pages/master/workbench 工作台:接单开关 / 待处理待验收 / 可提现 / 六宫格入口 / 信用四指标 / 技能标签
- pages/master/hall 订单大厅:类型筛选(一口价/报价) + 4 种排序 + 抢单倒计时 + 报价入口
- pages/master/quote 报价竞标:金额/说明/有效期 + 报价记录
- pages/master/wallet 钱包:可提现/今日/累计 + 账单明细 + 提现校验
- pages/master/credit 信用与申诉:五项指标 + 规则 + 申诉提交/记录
- 新增 fa_appeal 表

视觉:
- 15 个类目图标重画为双色扁平 SVG,用无头 Chrome 渲染为 192px 透明 PNG
- 首页改用 <image src=static/icons/*.png>,移除 200 行 CSS 图标数据

验证:page_flow 47 + e2e_flow 65 + phase2_verify 27 = 139 项断言全通过
2026-10-28 16:10:34 +08:00

106 行
3.5 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']) ? $wx['errcode'] : '-';
$em = isset($wx['errmsg']) ? $wx['errmsg'] : '未知错误';
return ['error' => '微信登录失败(' . $ec . '):' . $em];
}
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];
}
}