AdvertService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AdvertModel;
  13. use App\Models\CapitalLogModel;
  14. use App\Models\MemberModel;
  15. use App\Services\BaseService;
  16. /**
  17. * 用户挂单广告-服务类
  18. * Class AdvertService
  19. * @package App\Services\Api
  20. */
  21. class AdvertService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @since 2020/11/10
  28. * AdvertService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new AdvertModel();
  33. $this->memberModel = new MemberModel();
  34. $this->capitalModel = new CapitalLogModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 获取挂单广告列表
  49. * @param $params 参数
  50. * @param int $pageSize 分页大小:默认 15
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $where = ['a.mark' => 1];
  56. $type = isset($params['type'])? $params['type'] : 1;
  57. $status = isset($params['status'])? $params['status'] : 0;
  58. $advertType = isset($params['advert_type'])? $params['advert_type'] : 0;
  59. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  60. if($type>0){
  61. $where['a.type'] = $type;
  62. }
  63. if($status>0){
  64. $where['a.status'] = $status;
  65. }
  66. if($advertType>0){
  67. $where['a.type'] = $advertType;
  68. }
  69. if($userId>0){
  70. $where['a.user_id'] = $userId;
  71. }
  72. $list = $this->model->from('advert as a')
  73. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  74. ->where($where)
  75. ->where('a.expired_at', '>', date('Y-m-d H:i:s'))
  76. ->select(['a.*', 'm.username'])
  77. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  78. $list = $list? $list->toArray() :[];
  79. if($list){
  80. $field = $type == 1? 'usdt_buy_price' : 'usdt_sell_price';
  81. $marketPrice = \App\Services\ConfigService::make()->getConfigByCode($field);
  82. foreach($list['data'] as &$item){
  83. $expiredAt = isset($item['expired_at']) && $item['expired_at']? strtotime($item['expired_at']) : 0;
  84. $item['expired'] = max(0,$expiredAt-time());
  85. $item['expired_text'] = $expiredAt>0? formatTime($expiredAt - time()) : '已过期';
  86. // 固定价格和市场浮动价格处理
  87. $item['source_price'] = $item['price'];
  88. $item['logo_index'] = intval($item['user_id']%5);
  89. $item['username_text'] = $item['username']? mb_substr($item['username'], 0,1, 'utf-8'):'';
  90. $item['username_hidden'] = $item['username']? format_account($item['username']):'';
  91. $item['limit_min'] = moneyFormat($item['limit_min'], 2);
  92. $item['limit_max'] = moneyFormat($item['limit_max'], 2);
  93. $priceType = isset($item['price_type'])? intval($item['price_type']) : 0;
  94. $item['price'] = $priceType==2? moneyFormat($item['price']+$marketPrice, 2) : $item['price'];
  95. }
  96. }
  97. return [
  98. 'pageSize'=> $pageSize,
  99. 'total'=>isset($list['total'])? $list['total'] : 0,
  100. 'list'=> isset($list['data'])? $list['data'] : []
  101. ];
  102. }
  103. public function saveAdvert($userId)
  104. {
  105. $params = request()->all();
  106. $userInfo = \App\Services\Common\MemberService::make()->getInfo($userId);
  107. // 交易密码
  108. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  109. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  110. if (empty($password)) {
  111. return message(2015, false);
  112. }
  113. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  114. return message(2016, false);
  115. }
  116. $data = [
  117. 'id'=> isset($params['id'])? $params['id'] : 0,
  118. 'type'=> isset($params['type'])? $params['type'] : 1,
  119. 'user_id'=> $userId,
  120. 'advert_type'=> 2,
  121. 'coin_type'=> 1,
  122. 'linit_min'=> isset($params['linit_min'])? floatval($params['linit_min']) : 0.00,
  123. 'linit_max'=> isset($params['linit_max'])? floatval($params['linit_max']) : 0.00,
  124. 'price'=> isset($params['price'])? floatval($params['price']) : 0.00,
  125. 'price_type'=> isset($params['price_type'])? intval($params['price_type']) : 1,
  126. 'pay_type'=> isset($params['pay_type'])? intval($params['pay_type']) : 1,
  127. 'expired_at'=> isset($params['expired_at']) && $params['expired_at']? $params['expired_at'] : date('Y-m-d H:i:s'),
  128. ];
  129. return parent::edit($data);
  130. }
  131. }