multi-store/app/api/logic/user/AddressLogic.php
2024-06-15 11:13:52 +08:00

113 lines
3.0 KiB
PHP

<?php
namespace app\api\logic\user;
use app\common\logic\BaseLogic;
use app\common\model\user\UserAddress;
use think\facade\Db;
/**
* 地址逻辑
* Class OrderLogic
* @package app\api\logic\order
*/
class AddressLogic extends BaseLogic
{
/**
* @notes 添加地址表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
if(isset($params['is_default'])&&$params['is_default']==1){
UserAddress::where('uid',$params['uid'])->update(['is_default'=>0]);
}else{
$params['is_default']=1;
}
$id=UserAddress::insertGetId([
'uid' => $params['uid'],
'real_name' => $params['real_name'],
'phone' => $params['phone'],
'detail' => $params['detail'],
'is_default' => $params['is_default'],
]);
Db::commit();
return $id;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑地址表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
if($params['is_default']==1){
UserAddress::where('uid',$params['uid'])->update(['is_default'=>0]);
}
$data = [
'real_name' => $params['real_name'],
'phone' => $params['phone'],
'detail' => $params['detail'],
'is_default' => $params['is_default'],
'province' => $params['province'],
'city' => $params['city'],
'area' => $params['area'],
'street' => $params['street'],
'village' => $params['village'],
'brigade' => $params['brigade'],
];
UserAddress::where('uid', $params['uid'])->where('id', $params['id'])->update($data);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除地址表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function delete(array $params): bool
{
return UserAddress::destroy($params['address_id']);
}
/**
* @notes 获取地址详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function detail($params): array
{
return UserAddress::field('id,real_name,phone,province,city,area,street,village,brigade,detail,is_default')->where($params)->findOrEmpty()->toArray();
}
}