TradeService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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\MemberModel;
  13. use App\Models\TradeModel;
  14. use App\Models\VideoCollectModel;
  15. use App\Models\VideoModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 承兑商交易管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * @package App\Services\Api
  25. */
  26. class TradeService extends BaseService
  27. {
  28. // 静态对象
  29. protected static $instance = null;
  30. /**
  31. * 构造函数
  32. * @author laravel开发员
  33. * @since 2020/11/11
  34. * GoodsService constructor.
  35. */
  36. public function __construct()
  37. {
  38. $this->model = new TradeModel();
  39. }
  40. /**
  41. * 静态入口
  42. * @return static|null
  43. */
  44. public static function make()
  45. {
  46. if (!self::$instance) {
  47. self::$instance = (new static());
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * 列表数据
  53. * @param $params
  54. * @param int $pageSize
  55. * @return array
  56. */
  57. public function getDataList($params, $pageSize = 18, $field = '', $userId=0)
  58. {
  59. $where = ['a.mark' => 1,'b.mark'=>1];
  60. $field = $field? $field : 'lev_a.*';
  61. $order = 'lev_a.id desc';
  62. $list = $this->model->with(['member','acceptor'])->from('trade as a')
  63. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  64. ->where($where)
  65. ->where(function ($query) use ($params, $userId) {
  66. $type = isset($params['type']) ? $params['type'] : 0;
  67. if ($type > 0) {
  68. $query->where('a.type', $type);
  69. }
  70. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  71. if ($uid > 0) {
  72. $query->where('a.user_id', $uid);
  73. }
  74. $appectorUid = isset($params['acceptor_uid']) ? $params['acceptor_uid'] : 0;
  75. if ($appectorUid > 0) {
  76. $query->where('a.acceptor_uid', $appectorUid);
  77. }
  78. $appectorId = isset($params['acceptor_id']) ? $params['acceptor_id'] : 0;
  79. if ($appectorId > 0) {
  80. $query->where('a.acceptor_id', $appectorId);
  81. }
  82. })
  83. ->where(function ($query) use ($params) {
  84. $keyword = isset($params['kw']) ? $params['kw'] : '';
  85. if ($keyword) {
  86. $query->where('a.order_no', 'like', "%{$keyword}%")
  87. ->orWhere('b.nickname', 'like', "%{$keyword}%")
  88. ->orWhere('b.id', '=', "%{$keyword}%");
  89. }
  90. })
  91. ->selectRaw($field)
  92. ->orderByRaw($order)
  93. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  94. $list = $list ? $list->toArray() : [];
  95. if ($list && $list['data']) {
  96. foreach ($list['data'] as &$item) {
  97. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  98. $member = isset($item['member'])? $item['member'] : [];
  99. if($member){
  100. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  101. }
  102. $item['member'] = $member;
  103. }
  104. }
  105. return [
  106. 'pageSize' => $pageSize,
  107. 'total' => isset($list['total']) ? $list['total'] : 0,
  108. 'list' => isset($list['data']) ? $list['data'] : []
  109. ];
  110. }
  111. /**
  112. * 详情
  113. * @param $id
  114. * @return array
  115. */
  116. public function getInfo($id, $userId=0, $field=[])
  117. {
  118. $field = $field? $field : ['a.*'];
  119. $info = $this->model->with(['member','acceptor'])->from('trade as a')
  120. ->leftJoin('member as b','b.id','=','a.user_id')
  121. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  122. ->select($field)
  123. ->first();
  124. $info = $info? $info->toArray() : [];
  125. if($info){
  126. $member = isset($info['member'])? $info['member'] : [];
  127. if($member){
  128. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  129. }
  130. $info['member'] = $member;
  131. $info['time_text'] = isset($info['create_time']) ? dateFormat($info['create_time'], 'Y-m-d H:i') : '';
  132. }
  133. return $info;
  134. }
  135. /**
  136. * 卖出
  137. * @param $goodsId
  138. * @return bool
  139. */
  140. public function sell($userId, $params, $request)
  141. {
  142. $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
  143. $type = isset($params['type'])? intval($params['type']) : 2;
  144. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  145. ->select(['id','nickname','status'])
  146. ->first();
  147. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  148. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  149. if(empty($userInfo) || $status != 1){
  150. $this->error = 2017;
  151. return false;
  152. }
  153. $thumb = isset($params['thumb'])? trim($params['thumb']) : '';
  154. if($thumb){
  155. $result = upload_base64($thumb);
  156. $thumb = isset($result['file_path'])? $result['file_path'] : '';
  157. if(empty($thumb)){
  158. $this->error = 2208;
  159. return false;
  160. }
  161. }
  162. // 音乐
  163. $musicUrl = isset($params['music_url'])? trim($params['music_url']) : '';
  164. if($musicUrl){
  165. $result = upload_remote_image($musicUrl,'mp3','music');
  166. $musicUrl = isset($result['file_path'])? $result['file_path'] : '';
  167. if(empty($musicUrl)){
  168. $this->error = 2209;
  169. return false;
  170. }
  171. }
  172. // 视频
  173. $fileUrl = isset($params['file_url'])? trim($params['file_url']) : '';
  174. $albums = isset($params['album_urls'])?json_decode( $params['album_urls'],true) : [];
  175. if($type == 1) {
  176. if($fileUrl){
  177. $result = upload_video($request,'video');
  178. $data = isset($result['data'])? $result['data'] : [];
  179. $fileUrl = isset($data['file_path'])? $data['file_path'] : '';
  180. if(empty($fileUrl)){
  181. $this->error = 2212;
  182. return false;
  183. }
  184. }else{
  185. $this->error = 2213;
  186. return false;
  187. }
  188. }
  189. // 相册
  190. else if($type == 2){
  191. if(empty($albums) || !is_array($albums)){
  192. $this->error = 2211;
  193. return false;
  194. }
  195. $albums = get_format_images($albums);
  196. }
  197. $title = isset($params['title']) && $params['title']? trim($params['title']) : "{$nickname} 发布的短视频";
  198. $description = isset($params['description']) && $params['description']? trim($params['description']) : "{$nickname} 发布的短视频";
  199. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check',0);
  200. $publishCheck = $publishCheck>0? $publishCheck : 0;
  201. $tags = isset($params['tags']) && $params['tags']? $params['tags'] : '';
  202. $data = [
  203. 'user_id' => $userId,
  204. 'title' => $title,
  205. 'type' => $type,
  206. 'description' => $description,
  207. 'tags' => $tags && is_array($tags)? implode(',', $tags) : $tags,
  208. 'file_url' => $fileUrl,
  209. 'thumb' => $thumb,
  210. 'albums' => $albums? $albums : '',
  211. 'music_hash' => isset($params['music_hash'])? trim($params['music_hash']) : '',
  212. 'music_name' => isset($params['music_name'])? trim($params['music_name']) : '',
  213. 'music_url' => $musicUrl,
  214. 'goods_id' => $goodsId,
  215. 'visible_type' => isset($params['visible_type'])? intval($params['visible_type']) : 1,
  216. 'is_comment' => isset($params['is_comment'])? intval($params['is_comment']) : 1,
  217. 'status' => $publishCheck? 1 : 2,
  218. 'mark' => 1,
  219. 'publish_at' => date('Y-m-d H:i:s'),
  220. 'create_time' => time(),
  221. ];
  222. RedisService::set('caches:videos:publish', ['params'=> $params,'data'=> $data], 600);
  223. if($id = $this->model->insertGetId($data)){
  224. $this->error = 1023;
  225. // 发布视频任务处理
  226. TaskService::make()->updateTask($userId,5, $id);
  227. return ['id'=> $id];
  228. }
  229. $this->error = 1024;
  230. return false;
  231. }
  232. /**
  233. * 状态设置
  234. * @return bool
  235. */
  236. public function status()
  237. {
  238. $id = request()->post('id', 0);
  239. $status = request()->post('status', 1);
  240. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  241. $this->error = 2981;
  242. return false;
  243. }
  244. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  245. $this->error = 1002;
  246. return true;
  247. }
  248. $this->error = 1003;
  249. return true;
  250. }
  251. /**
  252. * 删除
  253. * @return bool
  254. */
  255. public function delete()
  256. {
  257. // 参数
  258. $param = request()->all();
  259. $id = getter($param, "id");
  260. if (empty($id)) {
  261. $this->error = 2014;
  262. return false;
  263. }
  264. if(!$this->model->where(['id'=> $id])->value('id')){
  265. $this->error = 1039;
  266. return false;
  267. }
  268. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  269. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  270. $this->error = 1025;
  271. return true;
  272. }else{
  273. $this->error = 1026;
  274. return false;
  275. }
  276. }
  277. }