// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AdvertModel; use App\Models\CapitalLogModel; use App\Models\MemberModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; /** * 用户挂单广告-服务类 * Class AdvertService * @package App\Services\Api */ class AdvertService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @since 2020/11/10 * AdvertService constructor. */ public function __construct() { $this->model = new AdvertModel(); $this->memberModel = new MemberModel(); $this->capitalModel = new CapitalLogModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取挂单广告列表 * @param $params 参数 * @param int $pageSize 分页大小:默认 15 * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $type = isset($params['type'])? $params['type'] : 1; $status = isset($params['status'])? $params['status'] : 0; $advertType = isset($params['advert_type'])? $params['advert_type'] : 0; $userId = isset($params['user_id'])? $params['user_id'] : 0; if($type>0){ $where['a.type'] = $type; } if($status>0){ $where['a.status'] = $status; } if($advertType>0){ $where['a.type'] = $advertType; } if($userId>0){ $where['a.user_id'] = $userId; } $list = $this->model->from('advert as a') ->leftJoin('member as m', 'm.id', '=', 'a.user_id') ->where($where) ->where('a.expired_at', '>', date('Y-m-d H:i:s')) ->select(['a.*', 'm.username']) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ $field = $type == 1? 'usdt_buy_price' : 'usdt_sell_price'; $marketPrice = \App\Services\ConfigService::make()->getConfigByCode($field); foreach($list['data'] as &$item){ $expiredAt = isset($item['expired_at']) && $item['expired_at']? strtotime($item['expired_at']) : 0; $item['expired'] = max(0,$expiredAt-time()); $item['expired_text'] = $expiredAt>0? formatTime($expiredAt - time()) : '已过期'; // 固定价格和市场浮动价格处理 $item['source_price'] = $item['price']; $item['logo_index'] = intval($item['user_id']%5); $item['username_text'] = $item['username']? mb_substr($item['username'], 0,1, 'utf-8'):''; $item['username_hidden'] = $item['username']? format_account($item['username']):''; $item['limit_min'] = moneyFormat($item['limit_min'], 2); $item['limit_max'] = moneyFormat($item['limit_max'], 2); $priceType = isset($item['price_type'])? intval($item['price_type']) : 0; $item['price'] = $priceType==2? moneyFormat($item['price']+$marketPrice, 2) : $item['price']; } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } public function saveAdvert($userId) { $params = request()->all(); $userInfo = MemberService::make()->getInfo($userId); // 交易密码 $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : ''; $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : ''; if (empty($password)) { return message(2015, false); } if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) { return message(2016, false); } $data = [ 'id'=> isset($params['id'])? $params['id'] : 0, 'type'=> isset($params['type'])? $params['type'] : 1, 'user_id'=> $userId, 'advert_type'=> 2, 'coin_type'=> 1, 'linit_min'=> isset($params['linit_min'])? floatval($params['linit_min']) : 0.00, 'linit_max'=> isset($params['linit_max'])? floatval($params['linit_max']) : 0.00, 'price'=> isset($params['price'])? floatval($params['price']) : 0.00, 'price_type'=> isset($params['price_type'])? intval($params['price_type']) : 1, 'pay_type'=> isset($params['pay_type'])? intval($params['pay_type']) : 1, 'expired_at'=> isset($params['expired_at']) && $params['expired_at']? $params['expired_at'] : date('Y-m-d H:i:s'), ]; return parent::edit($data); } }