WokerTask/app/api/logic/UserLogic.php

432 lines
14 KiB
PHP
Raw Normal View History

2023-12-27 14:06:33 +08:00
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\logic;
use app\common\{enum\notice\NoticeEnum,
enum\user\UserTerminalEnum,
enum\YesNoEnum,
logic\BaseLogic,
2024-01-12 11:26:20 +08:00
logic\ShopRequestLogic,
2023-12-27 14:06:33 +08:00
model\Company,
model\user\User,
model\user\UserAuth,
model\user\UserRole,
2023-12-27 14:06:33 +08:00
model\user\Withdraw,
service\ConfigService,
2023-12-27 14:06:33 +08:00
service\sms\SmsDriver,
service\wechat\WeChatMnpService};
use app\common\model\dict\DictData;
use app\common\model\user\UserAccountLog;
use think\db\exception\DataNotFoundException;
use think\exception\ValidateException;
use think\facade\Config;
use think\facade\Db;
/**
* 会员逻辑层
* Class UserLogic
* @package app\shopapi\logic
*/
class UserLogic extends BaseLogic
{
/**
* @notes 个人中心
* @param array $userInfo
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2022/9/16 18:04
*/
public static function center(array $userInfo): array
{
$user = User::where(['id' => $userInfo['user_id']])
->field('id,sn,sex,account,nickname,real_name,avatar,mobile,create_time,is_new_user,user_money,password')
->findOrEmpty();
if (in_array($userInfo['terminal'], [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA])) {
$auth = UserAuth::where(['user_id' => $userInfo['user_id'], 'terminal' => $userInfo['terminal']])->find();
$user['is_auth'] = $auth ? YesNoEnum::YES : YesNoEnum::NO;
}
$user['has_password'] = !empty($user['password']);
$user->hidden(['password']);
return $user->toArray();
}
/**
* @notes 个人信息
* @param $userId
* @return array
* @author 段誉
* @date 2022/9/20 19:45
*/
public static function info(int $userId)
{
$user = User::where(['id' => $userId])->with(['company'])->append(['user_role_name'])
->withAttr('user_role_name', function ($value, $data) {
$userRole = UserRole::where(['id' => $data['group_id']])->find();
return $userRole ? $userRole['name'] : '普通用户';
})
2023-12-27 14:06:33 +08:00
->findOrEmpty();
// $user['avatar'] = $user['avatar'] ? Request()->host() . $user['avatar'] : $user['avatar'];
$user['day_money_count']=UserAccountLog::where(['user_id'=>$userId,'action'=>1])->whereDay('create_time')->sum('change_amount');
// $user['has_password'] = !empty($user['password']);
// $user['has_auth'] = self::hasWechatAuth($userId);
$user['version'] = config('project.version');
// $user->hidden(['password']);
$data=$user->toArray();
if(isset($data['company']['company_type'])){
$name=DictData::where('id',$data['company']['company_type'])->value('name');
$data['company']['company_type_name']=$name;
}
2024-01-12 13:50:48 +08:00
2024-01-20 17:52:41 +08:00
$inviteUserStatistics = ShopRequestLogic::getInviteUserInfo(['promotion_code' => $data['invite_code']]);
$data['register_num'] = $inviteUserStatistics['data']['user_count'];
// $data['merchant_num'] = $inviteUserStatistics['data']['merchant_count'];
$data['trade_amount'] = Db::name('user_invite_first_order_log')->where('user_id', $userId)->sum('order_money');
2024-01-22 17:37:44 +08:00
$data['share_url'] = env('url.shop_prefix').'/download/index.html?code='.$data['invite_code'];
2024-01-12 13:50:48 +08:00
2024-01-20 18:09:38 +08:00
return $data;
2023-12-27 14:06:33 +08:00
}
/**
* @notes 设置用户信息
* @param int $userId
* @param array $params
* @return User|false
* @author 段誉
* @date 2022/9/21 16:53
*/
public static function setInfo(int $userId, array $params)
{
try {
return User::update([
'id' => $userId,
$params['field'] => $params['value']]
);
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 是否有微信授权信息
* @param $userId
* @return bool
* @author 段誉
* @date 2022/9/20 19:36
*/
public static function hasWechatAuth(int $userId)
{
//是否有微信授权登录
$terminal = [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA, UserTerminalEnum::PC];
$auth = UserAuth::where(['user_id' => $userId])
->whereIn('terminal', $terminal)
->findOrEmpty();
return !$auth->isEmpty();
}
/**
* @notes 重置登录密码
* @param $params
* @return bool
* @author 段誉
* @date 2022/9/16 18:06
*/
public static function resetPassword(array $params)
{
try {
// 校验验证码
$smsDriver = new SmsDriver();
if (!$smsDriver->verify($params['mobile'], $params['code'], NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA)) {
throw new \Exception('验证码错误');
}
// 重置密码
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
// 更新
User::where('mobile', $params['mobile'])->update([
'password' => $password
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 修稿密码
* @param $params
* @param $userId
* @return bool
* @author 段誉
* @date 2022/9/20 19:13
*/
public static function changePassword(array $params, int $userId)
{
try {
$user = User::findOrEmpty($userId);
if ($user->isEmpty()) {
throw new \Exception('用户不存在');
}
// 密码盐
$passwordSalt = Config::get('project.unique_identification');
if (!empty($user['password'])) {
if (empty($params['old_password'])) {
throw new \Exception('请填写旧密码');
}
$oldPassword = create_password($params['old_password'], $passwordSalt);
if ($oldPassword != $user['password']) {
throw new \Exception('原密码不正确');
}
}
// 保存密码
$password = create_password($params['password'], $passwordSalt);
$user->password = $password;
$user->is_new_user=1;
$user->save();
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 获取小程序手机号
* @param array $params
* @return bool
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
* @author 段誉
* @date 2023/2/27 11:49
*/
public static function getMobileByMnp(array $params)
{
try {
$response = (new WeChatMnpService())->getUserPhoneNumber($params['code']);
$phoneNumber = $response['phone_info']['purePhoneNumber'] ?? '';
if (empty($phoneNumber)) {
throw new \Exception('获取手机号码失败');
}
$user = User::where([
['mobile', '=', $phoneNumber],
['id', '<>', $params['user_id']]
])->findOrEmpty();
if (!$user->isEmpty()) {
throw new \Exception('手机号已被其他账号绑定');
}
// 绑定手机号
User::update([
'id' => $params['user_id'],
'mobile' => $phoneNumber
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 绑定手机号
* @param $params
* @return bool
* @author 段誉
* @date 2022/9/21 17:28
*/
public static function bindMobile(array $params)
{
try {
// 变更手机号场景
$sceneId = NoticeEnum::CHANGE_MOBILE_CAPTCHA;
$where = [
['id', '=', $params['user_id']],
['mobile', '=', $params['mobile']]
];
// 绑定手机号场景
if ($params['type'] == 'bind') {
$sceneId = NoticeEnum::BIND_MOBILE_CAPTCHA;
$where = [
['mobile', '=', $params['mobile']]
];
}
// 校验短信
$checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
if (!$checkSmsCode) {
throw new \Exception('验证码错误');
}
$user = User::where($where)->findOrEmpty();
if (!$user->isEmpty()) {
throw new \Exception('该手机号已被使用');
}
User::update([
'id' => $params['user_id'],
'mobile' => $params['mobile'],
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function withdraw($params)
{
Db::startTrans();
try {
$user = User::findOrEmpty($params['user_id']);
if ($user->isEmpty()) {
throw new DataNotFoundException('用户不存在');
}
$company = Company::find(['id'=>$user->company_id]);
if ($company->isEmpty()) {
throw new DataNotFoundException('账户异常,关联公司不存在');
}
// 是否有在途的提现申请
$withdraw = Withdraw::where(['user_id'=>$params['user_id'], 'status'=>0])->find();
if (!empty($withdraw)) {
throw new ValidateException('您已有一笔审核中的提现申请,暂无法继续申请提现');
}
// 校验提现金额
if ($params['amount'] <= 0) {
throw new ValidateException('提现金额错误');
}
// 校验提现金额
if ($params['amount'] > $company->company_money) {
throw new ValidateException('余额不足');
}
// 发票校验
if (empty($params['invoice'])) {
throw new ValidateException('没有上传电子发票');
}
// 保存提现记录
$withdraw = new Withdraw();
$withdraw->user_id = $user['id'];
$withdraw->admin_id = $user['admin_id'];
$withdraw->order_sn = $withdraw->buildOrderSn();
$withdraw->amount = $params['amount'];
$withdraw->transfer_end_cycel = $params['transfer_end_cycel'];
$withdraw->invoice = $params['invoice'];
$withdraw->create_time = time();
$withdraw->update_time = time();
$withdraw->save();
// 扣除余额
// $user->user_money = $user['user_money'] - $params['amount'];
// $user->save();
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function withdrawList($userId, $page, $limit)
{
$query = Withdraw::where('user_id', $userId);
$count = $query->count();
$list = $query->order('id', 'desc')->page($page)->limit($limit)->select()->toArray();
return [$count, $list];
}
public static function register(array $params)
{
try {
// 手机号已被使用
$mobileUser = User::where(['account' => $params['account']])->find();
if (!empty($mobileUser)) {
self::setError('手机号已被注册');
return false;
}
// 生成用户编号
$userSn = User::createUserSn();
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
if ($params['avatar'] != '') {
$avatar = $params['avatar'];
} else {
$avatar = ConfigService::get('default_image', 'user_avatar');
}
User::create([
'sn' => $userSn,
'avatar' => $avatar,
'is_captain' => 0,
'nickname' => $params['nickname'],
'account' => $params['account'],
'mobile' => $params['account'],
'id_card' => $params['id_card'],
'password' => $password,
'channel' => $params['channel'],
'sex' => $params['sex'],
'province' => $params['province'],
'city' => $params['city'],
'area' => $params['area'],
'street' => $params['street'],
'village' => $params['village'],
'brigade' => $params['brigade'],
'address' => $params['address'],
'qualification' => json_encode($params['qualification']),
'company_id' => 0, // 暂时为0
'group_id' => $params['group_id'],
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
2023-12-27 14:06:33 +08:00
}