| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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'];
- }
- 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()]);
- }
- }
|