error(__('User center already closed')); } } /** * 会员中心 */ public function index() { $this->success('', ['welcome' => $this->auth->nickname]); } /** * 会员登录 * * @ApiMethod (POST) * @ApiParams (name="account", type="string", required=true, description="账号") * @ApiParams (name="password", type="string", required=true, description="密码") */ public function login() { $account = $this->request->post('account'); $password = $this->request->post('password'); if (!$account || !$password) { $this->error(__('Invalid parameters')); } $ret = $this->auth->login($account, $password); if ($ret) { $data = ['userinfo' => $this->auth->getUserinfo()]; $this->success(__('Logged in successful'), $data); } else { $this->error($this->auth->getError()); } } /** * 手机验证码登录(已移除) * * @ApiMethod (POST) */ public function mobilelogin() { $this->error('短信验证码通道已移除,请使用手机号密码或微信一键登录'); } /** * 注册会员(短信验证通道已移除) * * @ApiMethod (POST) */ public function register() { $this->error('短信验证码通道已移除,请使用手机号密码或微信一键登录'); } /** * 退出登录 * @ApiMethod (POST) */ public function logout() { if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } $this->auth->logout(); $this->success(__('Logout successful')); } /** * 修改会员个人信息 * * @ApiMethod (POST) * @ApiParams (name="avatar", type="string", required=true, description="头像地址") * @ApiParams (name="username", type="string", required=true, description="用户名") * @ApiParams (name="nickname", type="string", required=true, description="昵称") * @ApiParams (name="bio", type="string", required=true, description="个人简介") */ public function profile() { $user = $this->auth->getUser(); $username = $this->request->post('username'); $nickname = $this->request->post('nickname'); $bio = $this->request->post('bio'); $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); if ($username) { $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find(); if ($exists) { $this->error(__('Username already exists')); } $user->username = $username; } if ($nickname) { $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find(); if ($exists) { $this->error(__('Nickname already exists')); } $user->nickname = $nickname; } if ($avatar) { //判断是否匹配config('upload.cdnurl')开头以及当前$SERVER['HTTP_HOST']开头。 if (preg_match('/^' . preg_quote(config('upload.cdnurl') . '/', '/') . '/i', $avatar) || preg_match('/^' . preg_quote(substr(config('upload.savekey'), 0, strpos(config('upload.savekey'), '{')), '/') . '/i', $avatar) || preg_match('/^' . preg_quote($_SERVER['HTTP_HOST'] . '/', '/') . '/i', $avatar)) { $user->avatar = $avatar; } } $user->bio = $bio; $user->save(); $this->success(); } /** * 修改邮箱 * * @ApiMethod (POST) * @ApiParams (name="email", type="string", required=true, description="邮箱") * @ApiParams (name="captcha", type="string", required=true, description="验证码") */ public function changeemail() { $user = $this->auth->getUser(); $email = $this->request->post('email'); $captcha = $this->request->post('captcha'); if (!$email || !$captcha) { $this->error(__('Invalid parameters')); } if (!Validate::is($email, "email")) { $this->error(__('Email is incorrect')); } if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) { $this->error(__('Email already exists')); } $result = Ems::check($email, $captcha, 'changeemail', true); if (!$result) { $this->error(__('Captcha is incorrect')); } $verification = $user->verification; $verification->email = 1; $user->verification = $verification; $user->email = $email; $user->save(); Ems::flush($email, 'changeemail'); $this->success(); } /** * 修改手机号(短信验证通道已移除) * * @ApiMethod (POST) */ public function changemobile() { $this->error('短信验证码通道已移除,暂不支持短信换绑手机号'); } /** * 第三方登录 * * @ApiMethod (POST) * @ApiParams (name="platform", type="string", required=true, description="平台名称") * @ApiParams (name="code", type="string", required=true, description="Code码") */ public function third() { $url = url('user/index'); $platform = $this->request->post("platform"); $code = $this->request->post("code"); $config = get_addon_config('third'); if (!$config || !isset($config[$platform])) { $this->error(__('Invalid parameters')); } $app = new \addons\third\library\Application($config); //通过code换access_token和绑定会员 $result = $app->{$platform}->getUserInfo(['code' => $code]); if ($result) { $loginret = \addons\third\library\Service::connect($platform, $result); if ($loginret) { $data = [ 'userinfo' => $this->auth->getUserinfo(), 'thirdinfo' => $result ]; $this->success(__('Logged in successful'), $data); } } $this->error(__('Operation failed'), $url); } /** * 重置密码 * * @ApiMethod (POST) * @ApiParams (name="mobile", type="string", required=true, description="手机号") * @ApiParams (name="newpassword", type="string", required=true, description="新密码") * @ApiParams (name="captcha", type="string", required=true, description="验证码") */ public function resetpwd() { $type = $this->request->post("type", "mobile"); $mobile = $this->request->post("mobile"); $email = $this->request->post("email"); $newpassword = $this->request->post("newpassword"); $captcha = $this->request->post("captcha"); if (!$newpassword || !$captcha) { $this->error(__('Invalid parameters')); } //验证Token if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) { $this->error(__('Password must be 6 to 30 characters')); } if ($type == 'mobile') { $this->error('短信验证码通道已移除,暂不支持手机短信找回密码'); } else { if (!Validate::is($email, "email")) { $this->error(__('Email is incorrect')); } $user = \app\common\model\User::getByEmail($email); if (!$user) { $this->error(__('User not found')); } $ret = Ems::check($email, $captcha, 'resetpwd', true); if (!$ret) { $this->error(__('Captcha is incorrect')); } Ems::flush($email, 'resetpwd'); } //模拟一次登录 $this->auth->direct($user->id); $ret = $this->auth->changepwd($newpassword, '', true); if ($ret) { $this->success(__('Reset password successful')); } else { $this->error($this->auth->getError()); } } /** * 获取用户钱包资产看板(可用余额与收支统计) * @ApiMethod (GET) */ public function wallet() { $uid = $this->auth->id; $user = Db::name('user')->field('id,nickname,mobile,avatar,money')->where('id', $uid)->find(); if (!$user) { $this->error('用户不存在'); } $totalRecharge = Db::name('wallet_log') ->where('user_type', 'user') ->where('user_id', $uid) ->where('type', 'recharge') ->sum('amount'); $totalConsume = Db::name('wallet_log') ->where('user_type', 'user') ->where('user_id', $uid) ->where('type', 'consume') ->sum('amount'); $totalRefund = Db::name('wallet_log') ->where('user_type', 'user') ->where('user_id', $uid) ->where('type', 'refund') ->sum('amount'); $totalCount = Db::name('wallet_log') ->where('user_type', 'user') ->where('user_id', $uid) ->count(); $rechargeConfig = FinanceConfigService::getRechargeConfig(); $this->success('', [ 'balance' => (float)$user['money'], 'total_recharge' => round((float)$totalRecharge, 2), 'total_consume' => round(abs((float)$totalConsume), 2), 'total_refund' => round((float)$totalRefund, 2), 'total_count' => (int)$totalCount, 'recharge_config' => $rechargeConfig, ]); } /** * 分页查询用户资金收支账单流水 * @ApiMethod (GET) */ public function walletLogs() { $uid = $this->auth->id; $type = trim((string)$this->request->get('type', 'all')); $page = max(1, (int)$this->request->get('page', 1)); $limit = min(50, max(1, (int)$this->request->get('limit', 20))); $query = Db::name('wallet_log') ->where('user_type', 'user') ->where('user_id', $uid); if (in_array($type, ['recharge', 'consume', 'refund'], true)) { $query->where('type', $type); } $total = $query->count(); $rows = $query->order('id desc')->page($page, $limit)->select(); $orderIds = []; foreach ($rows as $r) { if ((int)$r['order_id'] > 0) { $orderIds[] = (int)$r['order_id']; } } $orderMap = []; if (!empty($orderIds)) { $orders = Db::name('order')->where('id', 'in', array_unique($orderIds))->field('id,order_no,service_name')->select(); foreach ($orders as $o) { $orderMap[(int)$o['id']] = $o; } } $list = []; foreach ($rows as $r) { $oid = (int)$r['order_id']; $ord = $oid > 0 ? ($orderMap[$oid] ?? null) : null; $list[] = [ 'id' => (int)$r['id'], 'type' => $r['type'], 'type_label' => $this->formatWalletTypeLabel($r['type']), 'amount' => (float)$r['amount'], 'balance' => (float)$r['balance'], 'order_id' => $oid, 'order_no' => $ord ? $ord['order_no'] : '', 'service_name' => $ord ? $ord['service_name'] : '', 'remark' => (string)$r['remark'], 'createtime' => (int)$r['createtime'], 'created_at' => $r['createtime'] ? date('Y-m-d H:i', (int)$r['createtime']) : '', ]; } $this->success('', [ 'list' => $list, 'total' => (int)$total, 'page' => $page, 'limit' => $limit, 'has_more' => ($page * $limit) < $total, ]); } /** * 发起微信在线充值(生成 JSAPI 支付参数) * @ApiMethod (POST) */ public function recharge() { $uid = $this->auth->id; $amount = round((float)$this->request->post('amount'), 2); $wxCode = trim((string)$this->request->post('wx_code', '')); if ($amount < 0.01) { $this->error('充值金额必须大于 ¥0.00'); return; } try { $res = PaymentService::createRechargeJsapiPay($uid, $amount, $wxCode); $this->success('请完成微信支付', $res); } catch (\think\exception\HttpResponseException $e) { throw $e; } catch (\InvalidArgumentException $e) { $this->error($e->getMessage()); } catch (\Throwable $e) { \think\Log::error('[User::recharge] ' . $e->getMessage()); $this->error('发起充值支付失败:' . $e->getMessage()); } } /** * 充值完成后小程序端主动查单幂等核销 * @ApiMethod (POST) */ public function confirmRecharge() { $uid = $this->auth->id; $outTradeNo = trim((string)$this->request->post('out_trade_no', '')); if ($outTradeNo === '') { $this->error('缺少充值单号'); return; } $payOrder = Db::name('pay_order')->where('out_trade_no', $outTradeNo)->find(); if (!$payOrder) { $this->error('充值支付单不存在'); return; } if ((int)$payOrder['user_id'] !== $uid) { $this->error('无权操作该充值单'); return; } try { $res = PaymentService::confirmRechargePay($outTradeNo); $this->success('充值已成功到账', $res); } catch (\think\exception\HttpResponseException $e) { throw $e; } catch (\InvalidArgumentException $e) { $this->error($e->getMessage()); } catch (\Throwable $e) { \think\Log::error('[User::confirmRecharge] ' . $e->getMessage()); $this->error('充值确认失败:' . $e->getMessage()); } } protected function formatWalletTypeLabel($type) { $map = [ 'recharge' => '微信在线充值', 'consume' => '订单余额消费', 'refund' => '售后/取消退款', 'income' => '奖励补贴入账', 'settle' => '工单收益结算', 'withdraw' => '余额提现出账', ]; return $map[$type] ?? '资金变动'; } }