// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\MemberAddressModel; use App\Services\BaseService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; /** * 用户地址管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class MemberAddressService * @package App\Services\Api */ class MemberAddressService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * MemberAddressService constructor. */ public function __construct() { $this->model = new MemberAddressModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 列表数据 * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1,'a.status'=>1]; $status = isset($params['status']) ? $params['status'] : 0; if ($status > 0) { $where['a.status'] = $status; } $userId = isset($params['user_id']) ? $params['user_id'] : 0; if ($userId > 0) { $where['a.user_id'] = $userId; } $list = $this->model->from('member_address as a') ->where($where) ->select(['a.*']) ->orderBy('a.is_default', 'asc') ->orderBy('a.id', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : ''; $address = []; if($item['province']){ $address[] = $item['province']; } if($item['city']){ $address[] = $item['city']; } if($item['district']){ $address[] = $item['district']; } $item['area'] = $address? implode('/', $address) : ''; if($item['address']){ $address[] = $item['address']; } $item['address_text'] = $address? implode(' ',$address) : ''; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 保存数据 * @param $userId * @param $params * @return mixed */ public function saveData($userId, $params) { $id = isset($params['id']) ? $params['id'] : 0; $isDefault = isset($params['is_default']) ? $params['is_default'] : 2; $data = [ 'user_id'=> $userId, 'mobile'=> isset($params['mobile'])? $params['mobile'] : '', 'realname'=> isset($params['realname'])? $params['realname'] : '', 'province'=> isset($params['province'])? $params['province'] : '', 'city'=> isset($params['city'])? $params['city'] : '', 'district'=> isset($params['district'])? $params['district'] : '', 'codes'=> isset($params['codes'])? $params['codes'] : '', 'address'=> isset($params['address'])? $params['address'] : '', 'is_default'=> $isDefault==1? 1: 2, 'status'=> isset($params['status'])? $params['status'] : 1, 'update_time'=> time(), 'mark'=> 1, ]; DB::beginTransaction(); if($isDefault==1){ $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]); } RedisService::keyDel("caches:members:address:{$userId}*"); if($id && $id = $this->model->where(['id'=> $id])->value('id')){ $this->model->where(['id'=> $id])->update($data); $this->error = '保存成功'; DB::commit(); return ['id'=>$id]; }else{ $data['create_time'] = time(); if(!$id = $this->model->insertGetId($data)){ DB::rollBack(); $this->error = '添加失败'; return false; } DB::commit(); $this->error = '添加成功'; return ['id'=>$id]; } } /** * 默认 * @param $userId * @param $params * @return mixed */ public function setDefault($userId, $params) { $id = isset($params['id']) ? $params['id'] : 0; $isDefault = isset($params['is_default']) ? $params['is_default'] : 2; $data = [ 'user_id'=> $userId, 'is_default'=> $isDefault==1? 1: 2, 'update_time'=> time(), ]; if($isDefault==1){ $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]); } $this->model->where(['id'=> $id])->update($data); $this->error = '保存成功'; RedisService::keyDel("caches:members:address:{$userId}*"); return ['id'=>$id]; } /** * @param $id * @param $userId * @return array|mixed */ public function getInfo($id, $userId) { $cacheKey = "caches:members:address:{$userId}_info_".($id?'_'.$id:''); $info = RedisService::get($cacheKey); if($info){ return $info; } $where = ['id'=> $id,'user_id'=> $userId,'mark'=>1,'status'=>1]; if($id<=0){ unset($where['id']); } $info = $this->model->where($where)->first(); $info = $info? $info->toArray() : []; if($info){ $address = []; $info['mobile_text'] = format_mobile($info['mobile']); if(isset($info['province']) && $info['province']){ $address[] = $info['province']; } if(isset($info['city']) && $info['city']){ $address[] = $info['city']; } if(isset($info['district']) && $info['district']){ $address[] = $info['district']; } $info['area'] = $address? implode('/', $address) : ''; if($info['address']){ $address[] = $info['address']; } $info['address_text'] = $address? implode(' ', $address) : ''; RedisService::set($cacheKey, $info, rand(5, 10)); } return $info; } /** * @param $userId * @return array|mixed */ public function getBindInfo($userId, $addressId=0) { $cacheKey = "caches:members:address:{$userId}".($addressId?'_'.$addressId:''); $info = RedisService::get($cacheKey); if($info){ return $info; } $where = ['id'=> $addressId,'user_id'=> $userId,'mark'=>1,'status'=>1]; if($addressId<=0){ unset($where['id']); } $info = $this->model->where($where) ->orderBy('is_default','asc') ->orderBy('id','desc') ->first(); $info = $info? $info->toArray() : []; if($info){ $address = []; $info['mobile_text'] = format_mobile($info['mobile']); if(isset($info['province']) && $info['province']){ $address[] = $info['province']; } if(isset($info['city']) && $info['city']){ $address[] = $info['city']; } if(isset($info['district']) && $info['district']){ $address[] = $info['district']; } $info['area'] = $address? implode('/', $address) : ''; if($info['address']){ $address[] = $info['address']; } $info['address_text'] = $address? implode(' ', $address) : ''; RedisService::set($cacheKey, $info, rand(5, 10)); } return $info; } /** * @return array|false */ public function delete() { // 参数 $id = request()->post('id'); if (empty($id)) { $this->error = 2014; return false; } $this->error = 1002; $this->model->where(['id'=> $id,'mark'=>0])->where('update_time','<=', time() - 3*86400)->delete(); return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]); } }