72 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			72 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace app\traits\account;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use think\Model;
							 | 
						||
| 
								 | 
							
								use app\model\Base;
							 | 
						||
| 
								 | 
							
								use think\Collection;
							 | 
						||
| 
								 | 
							
								use app\model\AccountAddress;
							 | 
						||
| 
								 | 
							
								use think\db\exception\DbException;
							 | 
						||
| 
								 | 
							
								use think\db\exception\DataNotFoundException;
							 | 
						||
| 
								 | 
							
								use think\db\exception\ModelNotFoundException;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								trait AccountAddressTrait
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 获取地址详情
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param  int  $id
							 | 
						||
| 
								 | 
							
								     * @param  array  $fields
							 | 
						||
| 
								 | 
							
								     * @return array|Model|null
							 | 
						||
| 
								 | 
							
								     * @throws DataNotFoundException
							 | 
						||
| 
								 | 
							
								     * @throws DbException
							 | 
						||
| 
								 | 
							
								     * @throws ModelNotFoundException
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function findAddressById(int $id, array $fields = [])
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return AccountAddress::findById($id, $fields);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 获取地址列表
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param  int  $userId
							 | 
						||
| 
								 | 
							
								     * @param  array  $fields
							 | 
						||
| 
								 | 
							
								     * @return Collection
							 | 
						||
| 
								 | 
							
								     * @throws DataNotFoundException
							 | 
						||
| 
								 | 
							
								     * @throws DbException
							 | 
						||
| 
								 | 
							
								     * @throws ModelNotFoundException
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function findAddressByUid(int $userId, array $fields = []): Collection
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return AccountAddress::where('user_id', $userId)->order('id', 'desc')->field($fields)->select();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 保存地址
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param  array  $data
							 | 
						||
| 
								 | 
							
								     * @return AccountAddress|Base|Model
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function saveAddress(array $data)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if ($data['is_default'] == 1) {
							 | 
						||
| 
								 | 
							
								            AccountAddress::update(['is_default' => 0], ['user_id' => $data['user_id']]);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if (isset($data['id']) && !empty($data['id'])) {
							 | 
						||
| 
								 | 
							
								            return AccountAddress::updateById($data['id'], $data);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return AccountAddress::create($data);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 删除地址
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param  int  $id
							 | 
						||
| 
								 | 
							
								     * @return bool
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function delAddress(int $id): bool
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return AccountAddress::deleteById($id);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |