request->post('mobile', $this->request->post('account', ''))); $mobile = preg_replace('/[^\d]/', '', $mobile); $password = (string)$this->request->post('password', ''); $code = trim((string)$this->request->post('code', '')); if ($password === '' && $code !== '') { $this->error('短信验证码通道已移除,请使用手机号密码或微信一键登录', null, 2010); } if ($mobile === '') { $this->error('请填写手机号'); } if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) { $this->error('手机号格式不正确'); } if ($password === '') { $this->error('请填写登录密码'); } $master = Db::name('master')->where('mobile', $mobile)->find(); if (!$master) { if (!preg_match('/^\S{6,30}$/', $password)) { $this->error('登录密码须为6至30位非空字符'); } if (!$this->masterAuth->register($mobile, $password)) { $this->error($this->masterAuth->getError() ?: '注册登录失败'); } } else { if (isset($master['status']) && $master['status'] !== 'normal') { $this->error('师傅账号已被禁用'); } if (empty($master['password'])) { $this->error('该师傅账号尚未设置登录密码,请使用微信一键登录后在账号安全中设置密码'); } try { MasterSecurity::verify((int)$master['id'], 'login', $password); } catch (\Throwable $e) { $this->error($e->getMessage() === '密码不正确' ? '手机号或密码不正确' : $e->getMessage()); } if (!$this->masterAuth->direct((int)$master['id'])) { $this->error($this->masterAuth->getError() ?: '登录失败'); } } $this->success('登录成功', [ 'token' => $this->masterAuth->getToken(), 'expires' => 2592000, 'masterinfo' => $this->profileData(), ]); } /** * 师傅端微信小程序 OpenID 静默/授权登录 * @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 = WxLogin::jscode2session($code, 'master'); if (isset($wx['error'])) { $this->error($wx['error'], ['wx_errcode' => isset($wx['errcode']) ? (int)$wx['errcode'] : 0]); } $ret = WxLogin::provisionMaster( $wx['openid'], isset($wx['unionid']) ? $wx['unionid'] : '', isset($wx['session_key']) ? $wx['session_key'] : '', $nickname, $avatar ); if (!$this->masterAuth->direct($ret['master_id'])) { $this->error($this->masterAuth->getError() ?: '登录失败'); } $this->success('登录成功', [ 'token' => $this->masterAuth->getToken(), 'expires' => 2592000, 'masterinfo' => $this->profileData(), 'is_new' => $ret['is_new'], ]); } /** * 师傅端微信手机号授权快捷登录 * @ApiMethod (POST) */ public function wxPhoneLogin() { $phoneCode = trim((string)$this->request->post('phone_code')); $loginCode = trim((string)$this->request->post('login_code')); if ($phoneCode === '') { $this->error('缺少微信手机号授权凭证'); } $phoneInfo = WxLogin::getPhoneNumber($phoneCode, 'master'); if (isset($phoneInfo['error'])) { $this->error('获取微信手机号失败:' . $phoneInfo['error'], ['wx_errcode' => isset($phoneInfo['errcode']) ? (int)$phoneInfo['errcode'] : 0]); } $mobile = isset($phoneInfo['purePhoneNumber']) ? (string)$phoneInfo['purePhoneNumber'] : (string)($phoneInfo['phoneNumber'] ?? ''); if ($mobile === '') { $this->error('解析手机号失败'); } $openid = ''; $sessionKey = ''; $unionid = ''; if ($loginCode !== '') { $wx = WxLogin::jscode2session($loginCode, 'master'); if (isset($wx['error']) || empty($wx['openid'])) { $errMsg = isset($wx['error']) && $wx['error'] !== '' ? (string)$wx['error'] : '微信登录凭证换取 OpenID 失败,请重进小程序再试'; \think\Log::error('[MasterAuth::wxPhoneLogin] jscode2session 失败: ' . $errMsg); $this->error($errMsg, ['wx_errcode' => isset($wx['errcode']) ? (int)$wx['errcode'] : 0]); } $openid = $wx['openid']; $sessionKey = isset($wx['session_key']) ? $wx['session_key'] : ''; $unionid = isset($wx['unionid']) ? $wx['unionid'] : ''; } $ret = WxLogin::provisionMasterWithMobile($mobile, $openid, $unionid, $sessionKey); if (!$this->masterAuth->direct($ret['master_id'])) { $this->error($this->masterAuth->getError() ?: '登录失败'); } $this->success('登录成功', [ 'token' => $this->masterAuth->getToken(), 'expires' => 2592000, 'masterinfo' => $this->profileData(), 'is_new' => $ret['is_new'], 'mobile' => $mobile, ]); } /** * 当前登录师傅基础信息 */ public function profile() { $this->success('', $this->profileData()); } /** * 退出登录 * @ApiMethod (POST) */ public function logout() { $this->masterAuth->logout(); $this->success('已退出登录'); } /** * 安全状态查询(登录密码 / 支付密码) */ public function securityStatus() { $mid = $this->masterId(); $master = Db::name('master')->field('password,mobile')->where('id', $mid)->find(); $security = Db::name('master_security')->where('master_id', $mid)->find(); $this->success('', [ 'has_login_password' => !empty($master['password']), 'has_payment_password' => $security && $security['payment_password_hash'] !== '', ]); } /** * 设置或修改师傅登录密码 * @ApiMethod (POST) */ public function changeLoginPassword() { $mid = $this->masterId(); $new = (string)$this->request->post('new_password', ''); $old = (string)$this->request->post('old_password', ''); if (!preg_match('/^\S{6,30}$/', $new)) { $this->error('登录密码须为6至30位非空字符'); } $master = Db::name('master')->field('password,mobile')->where('id', $mid)->find(); if (!empty($master['password'])) { try { MasterSecurity::verify($mid, 'login', $old); } catch (\Throwable $e) { $this->error($e->getMessage()); } $ignoreOld = false; } else { $this->verifyBoundPhone($this->request->post('phone_code', ''), (string)$master['mobile']); $ignoreOld = true; } if (!$this->masterAuth->changepwd($new, $old, $ignoreOld)) { $this->error($this->masterAuth->getError() ?: '设置失败'); } MasterToken::clear($mid); $this->success('登录密码已更新,请重新登录'); } /** * 首次设置师傅支付密码(支持已设登录密码走登录密码验证,或未设登录密码走绑定微信手机号验证) * @ApiMethod (POST) */ public function setPaymentPassword() { $mid = $this->masterId(); $new = (string)$this->request->post('new_password', ''); if (!preg_match('/^\d{6}$/', $new)) { $this->error('支付密码须为6位数字'); } $master = Db::name('master')->field('password,mobile')->where('id', $mid)->find(); $phoneCode = trim((string)$this->request->post('phone_code', '')); if ($phoneCode !== '') { $this->verifyBoundPhone($phoneCode, (string)($master['mobile'] ?? '')); } elseif (!empty($master['password'])) { try { MasterSecurity::verify($mid, 'login', (string)$this->request->post('login_password', '')); } catch (\Throwable $e) { $this->error($e->getMessage()); } } else { $this->error('请先验证绑定手机号或设置登录密码'); } MasterSecurity::ensure($mid); $updated = Db::name('master_security') ->where('master_id', $mid) ->where('payment_password_hash', '') ->update(['payment_password_hash' => password_hash($new, PASSWORD_DEFAULT)]); if (!$updated) { $this->error('支付密码已设置,请使用修改功能'); } $this->success('支付密码设置成功'); } /** * 修改师傅支付密码 * @ApiMethod (POST) */ public function changePaymentPassword() { $mid = $this->masterId(); $new = (string)$this->request->post('new_password', ''); if (!preg_match('/^\d{6}$/', $new)) { $this->error('支付密码须为6位数字'); } $oldHash = (string)Db::name('master_security')->where('master_id', $mid)->value('payment_password_hash'); try { MasterSecurity::verify($mid, 'payment', (string)$this->request->post('old_password', '')); } catch (\Throwable $e) { $this->error($e->getMessage()); } $updated = Db::name('master_security') ->where('master_id', $mid) ->where('payment_password_hash', $oldHash) ->update(['payment_password_hash' => password_hash($new, PASSWORD_DEFAULT)]); if (!$updated) { $this->error('支付密码已变更,请重新操作'); } $this->success('支付密码修改成功'); } protected function verifyBoundPhone($code, $mobile) { if (trim((string)$code) === '') { $this->error('请先验证绑定的微信手机号'); } $phone = WxLogin::getPhoneNumber((string)$code, 'master'); $verified = isset($phone['purePhoneNumber']) ? (string)$phone['purePhoneNumber'] : (string)($phone['phoneNumber'] ?? ''); if (isset($phone['error']) || $verified === '' || !hash_equals((string)$mobile, $verified)) { $this->error('手机号验证失败,请使用绑定手机号重新验证'); } } protected function profileData() { $mid = $this->masterId(); $master = Db::name('master')->where('id', $mid)->find(); if (!$master) { return null; } $unread = (int)Db::name('message') ->where('receive_type', 'master') ->where('receive_id', $mid) ->where('is_read', 0) ->count(); return [ 'id' => (int)$master['id'], 'nickname' => !empty($master['nickname']) ? (string)$master['nickname'] : (!empty($master['realname']) ? (string)$master['realname'] : ('师傅' . substr((string)$master['mobile'], -4))), 'realname' => (string)$master['realname'], 'mobile' => (string)$master['mobile'], 'avatar' => !empty($master['avatar']) ? \app\common\library\MasterAvatar::url($master['avatar']) : '', 'city' => (string)$master['city'], 'verify_status' => (string)$master['verify_status'], 'audit_msg' => (string)$master['audit_msg'], 'work_status' => (string)$master['work_status'], 'level' => (int)$master['level'], 'score' => (float)$master['score'], 'order_count' => (int)$master['order_count'], 'money' => (float)$master['money'], 'frozen_money' => (float)$master['frozen_money'], 'can_take' => $master['verify_status'] === 'pass', 'needs_apply' => ($master['verify_status'] === 'draft'), 'unread_count' => $unread, ]; } }