提交其他文件

This commit is contained in:
2026-03-14 16:20:49 +08:00
parent a227deaecd
commit 0a19b334f8
1385 changed files with 73568 additions and 0 deletions

548
app/api/logic/UserLogic.php Normal file
View File

@ -0,0 +1,548 @@
<?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,
model\store\Store,
model\teamaster\TeamasterCollect,
model\teastore\TeaStore,
model\user\User,
model\user\UserAccountLog,
model\user\UserAddress,
model\user\UserAuth,
model\user\UserCoupon,
model\user\UserMember,
model\user\UserMoneyLog,
model\user\UserStoreMoney,
service\FileService,
service\sms\SmsDriver,
service\wechat\WeChatMnpService};
use think\facade\{Db, Config};
/**
* 会员逻辑层
* 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])
->field('id,sn,sex,account,password,nickname,real_name,avatar,mobile,create_time,user_money,member')
->findOrEmpty();
$store_money = 0;
$user['store_money'] =$store_money;
$user_store_money = UserStoreMoney::where('user_id',$userId)->find();
if($user_store_money){
$user['store_money'] = $user_store_money->money;
}
$user['has_password'] = !empty($user['password']);
$user['has_auth'] = self::hasWechatAuth($userId);
$user['version'] = config('project.version');
$user['collect_count'] = TeamasterCollect::where("user_id",$userId)
->where("status",1)
->count();
$user['coupon_count'] = UserCoupon::where("user_id",$userId)
->where("status",0)
->count();
$lastMonthStart = date('Y-m-01 00:00:00', strtotime('-1 month'));
$lastMonthEnd = date('Y-m-t 23:59:59', strtotime('-1 month'));
$user['last_month'] = UserAccountLog::where("user_id",$userId)
->where("action",2)
->whereTime('create_time', 'between', [$lastMonthStart, $lastMonthEnd])
->sum("amount");
// $user['avatar'] = FileService::getFileUrl($user['avatar']);
$user->hidden(['password']);
return $user->toArray();
}
/**
* @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 {
if ($params['field'] == "avatar") {
$params['value'] = FileService::setFileUrl($params['value']);
}
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->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;
}
}
/**
* @notes 地址列表
* @param $params
* @return array|false
* @author 胥聪
* @date 2022/10/21 15:01
*/
public static function addressList(array $params)
{
try {
$where = [
['user_id', '=', $params['user_id']],
['del', '=', 0]
];
if(isset($params['is_default'])){
if ($params['is_default'] == 1) {
$where = [
['is_default', '=', $params['is_default']]
];
}
}
return UserAddress::where($where)->select()->toArray();
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 地址列表
* @param $params
* @return array|false
* @author 胥聪
* @date 2022/10/21 15:01
*/
public static function addressDetails(array $params)
{
try {
$where = [
['id', '=', $params['id']],
['del', '=', 0]
];
$result = UserAddress::where($where)->find();
if ($result == null) {
throw new \Exception('地址不存在');
}
return [
'address_details'=>$result
];
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 创建地址
* @param $params
* @return array|bool
* @author 胥聪
* @date 2022/10/21 15:01
*/
public static function addAddress(array $params)
{
try {
UserAddress::create([
'user_id' => $params['user_id'],
'contact' => $params['contact'],
'telephone' => $params['telephone'],
'city_id' => isset($params['city_id']) ? $params['city_id'] : 0,
'address' => $params['address'],
'longitude' => isset($params['longitude']) ? $params['longitude'] : 0,
'latitude' => isset($params['latitude']) ? $params['latitude'] : 0,
'is_default' => isset($params['is_default']) ? $params['is_default'] : 0,
'create_time' => time()
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 修改地址
* @param array $params
* @return array|bool
* @throws \Exception
* @author 胥聪
* @date 2022/10/21 15:47
*/
public static function editAddress($params,$user_id)
{
Db::startTrans();
try {
$params['update_time'] = time();
// 更新登录信息
if(isset($params['is_default'])){
if($params['is_default'] == 1){
UserAddress::where("user_id",$user_id)->update(['is_default'=>0]);
}
}
self::updateUserAddress($params);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 修改地址
* @param array $params
* @return array|bool
* @throws \Exception
* @author 胥聪
* @date 2022/10/21 15:47
*/
public static function delAddress(array $params)
{
Db::startTrans();
try {
$params['del'] = 1;
$params['update_time'] = time();
// 更新登录信息
self::updateUserAddress($params);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 更新地址
* @param $userId
* @return UserAddress|array|bool
* @throws \Exception
* @author 胥聪
* @date 2022/10/21 15:47
*/
public static function updateUserAddress($params)
{
$userAddress = UserAddress::where("del",0)->findOrEmpty($params['id']);
if ($userAddress->isEmpty()) {
throw new \Exception('地址不存在');
}
$id = $params['id'];
unset($params['id']);
UserAddress::update($params, ['id' => $id]);
return true;
}
public static function getMoneyLogList($params,$user_id){
if (isset($params['month']) && !empty($params['month'])) {
// 将 "2025-11" 转换为时间戳范围
$month = $params['month'];
// 计算该月的开始和结束时间戳
$startTime = strtotime($month . '-01 00:00:00');
$endTime = strtotime(date('Y-m-t 23:59:59', $startTime));
$where[] = ['create_time', 'between', [$startTime, $endTime]];
}
$count = UserAccountLog::where('user_id',$user_id)->where($where ?? [])->count();
$lists = UserAccountLog::where('user_id',$user_id)
->where($where ?? [])
->page($params['page'], $params['size'])
->order('id desc')
->select();
$data = [
'list' => $lists,
'page' => $params['page'],
'size' => $params['size'],
'count' => $count,
'more' => is_more($count, $params['page'], $params['size'])
];
return $data;
}
public static function getUserStoreMoney($params,$user_id){
$result = UserStoreMoney::where("user_id",$user_id)
->where("store_id",$params['store_id'])
->find();
if($result == null){
$arr['money'] = 0;
return [
'data'=> $arr
];
}
return [
'data'=> $result
];
}
public static function userStoreMoneyList($user_id){
$result = UserStoreMoney::where("user_id",$user_id)
->select();
if($result){
foreach ($result as &$item){
$store = TeaStore::where('id',$item['store_id'])->find();
$item['store_name'] = $store->name;
$item['address'] = $store->address;
}
}
return $result->toArray();
}
public static function UserMember($user_id){
$result = UserMember::where("user_id",$user_id)
->where("status",1)
->find();
if($result!=null){
$result['expiration_time'] = date("Y-m-d",$result['expiration_time']);
}
return [
'data'=> $result
];
}
public static function accountDetails($data){
$result = UserAccountLog::where("id",$data['id'])->find();
$result['store'] = TeaStore::where("id",$result['store_id'])->find();
return [
'data'=> $result
];
}
}