TradeService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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\AccountLogModel;
  13. use App\Models\MemberBankModel;
  14. use App\Models\MemberModel;
  15. use App\Models\TradeModel;
  16. use App\Models\VideoCollectModel;
  17. use App\Models\VideoModel;
  18. use App\Services\BaseService;
  19. use App\Services\ConfigService;
  20. use App\Services\RedisService;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 承兑商交易管理-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. * @package App\Services\Api
  27. */
  28. class TradeService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * GoodsService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new TradeModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 列表数据
  55. * @param $params
  56. * @param int $pageSize
  57. * @return array
  58. */
  59. public function getDataList($params, $pageSize = 18, $field = '', $userId = 0)
  60. {
  61. $where = ['a.mark' => 1, 'b.mark' => 1];
  62. $field = $field ? $field : 'lev_a.*';
  63. $order = 'lev_a.id desc';
  64. $list = $this->model->with(['member', 'acceptor'])->from('trade as a')
  65. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  66. ->where($where)
  67. ->where(function ($query) use ($params, $userId) {
  68. $type = isset($params['type']) ? $params['type'] : 0;
  69. if ($type > 0) {
  70. $query->where('a.type', $type);
  71. }
  72. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  73. if ($uid > 0) {
  74. $query->where('a.user_id', $uid);
  75. }else{
  76. $query->where('a.user_id', $userId);
  77. }
  78. $appectorUid = isset($params['acceptor_uid']) ? $params['acceptor_uid'] : 0;
  79. if ($appectorUid > 0) {
  80. $query->where('a.acceptor_uid', $appectorUid);
  81. }
  82. $appectorId = isset($params['acceptor_id']) ? $params['acceptor_id'] : 0;
  83. if ($appectorId > 0) {
  84. $query->where('a.acceptor_id', $appectorId);
  85. }
  86. })
  87. ->where(function ($query) use ($params) {
  88. $keyword = isset($params['kw']) ? $params['kw'] : '';
  89. if ($keyword) {
  90. $query->where('a.order_no', 'like', "%{$keyword}%")
  91. ->orWhere('b.nickname', 'like', "%{$keyword}%")
  92. ->orWhere('b.id', '=', "%{$keyword}%");
  93. }
  94. })
  95. ->selectRaw($field)
  96. ->orderByRaw($order)
  97. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  98. $list = $list ? $list->toArray() : [];
  99. if ($list && $list['data']) {
  100. foreach ($list['data'] as &$item) {
  101. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  102. $member = isset($item['member']) ? $item['member'] : [];
  103. if ($member) {
  104. $member['avatar'] = isset($member['avatar']) && $member['avatar'] ? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  105. }
  106. $item['member'] = $member;
  107. }
  108. }
  109. return [
  110. 'pageSize' => $pageSize,
  111. 'total' => isset($list['total']) ? $list['total'] : 0,
  112. 'list' => isset($list['data']) ? $list['data'] : []
  113. ];
  114. }
  115. /**
  116. * 详情
  117. * @param $id
  118. * @return array
  119. */
  120. public function getInfo($id, $userId = 0, $field = [])
  121. {
  122. $field = $field ? $field : ['a.*'];
  123. $info = $this->model->with(['member', 'acceptor'])->from('trade as a')
  124. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  125. ->where(['a.id' => $id, 'a.mark' => 1, 'b.mark' => 1])
  126. ->select($field)
  127. ->first();
  128. $info = $info ? $info->toArray() : [];
  129. if ($info) {
  130. $member = isset($info['member']) ? $info['member'] : [];
  131. if ($member) {
  132. $member['avatar'] = isset($member['avatar']) && $member['avatar'] ? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  133. }
  134. $info['member'] = $member;
  135. $info['time_text'] = isset($info['create_time']) ? dateFormat($info['create_time'], 'Y-m-d H:i') : '';
  136. $info['memberBank'] = MemberBankModel::where(['id' => $info['account_id']])->first();
  137. if (isset($info['memberBank'])) {
  138. $info['memberBank']['qrcode'] = get_image_url($info['memberBank']['qrcode']);
  139. }
  140. $info['custom_uid'] = ConfigService::make()->getConfigByCode('xl_custom_id',100001);
  141. }
  142. return $info;
  143. }
  144. /**
  145. * 卖出
  146. * @param $goodsId
  147. * @return bool
  148. */
  149. public function sell($userId, $params, $request)
  150. {
  151. $goodsId = isset($params['goods_id']) ? intval($params['goods_id']) : 0;
  152. $type = isset($params['type']) ? intval($params['type']) : 2;
  153. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  154. ->select(['id', 'nickname', 'status'])
  155. ->first();
  156. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  157. $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
  158. if (empty($userInfo) || $status != 1) {
  159. $this->error = 2017;
  160. return false;
  161. }
  162. $thumb = isset($params['thumb']) ? trim($params['thumb']) : '';
  163. if ($thumb) {
  164. $result = upload_base64($thumb);
  165. $thumb = isset($result['file_path']) ? $result['file_path'] : '';
  166. if (empty($thumb)) {
  167. $this->error = 2208;
  168. return false;
  169. }
  170. }
  171. // 音乐
  172. $musicUrl = isset($params['music_url']) ? trim($params['music_url']) : '';
  173. if ($musicUrl) {
  174. $result = upload_remote_image($musicUrl, 'mp3', 'music');
  175. $musicUrl = isset($result['file_path']) ? $result['file_path'] : '';
  176. if (empty($musicUrl)) {
  177. $this->error = 2209;
  178. return false;
  179. }
  180. }
  181. // 视频
  182. $fileUrl = isset($params['file_url']) ? trim($params['file_url']) : '';
  183. $albums = isset($params['album_urls']) ? json_decode($params['album_urls'], true) : [];
  184. if ($type == 1) {
  185. if ($fileUrl) {
  186. $result = upload_video($request, 'video');
  187. $data = isset($result['data']) ? $result['data'] : [];
  188. $fileUrl = isset($data['file_path']) ? $data['file_path'] : '';
  189. if (empty($fileUrl)) {
  190. $this->error = 2212;
  191. return false;
  192. }
  193. } else {
  194. $this->error = 2213;
  195. return false;
  196. }
  197. } // 相册
  198. else if ($type == 2) {
  199. if (empty($albums) || !is_array($albums)) {
  200. $this->error = 2211;
  201. return false;
  202. }
  203. $albums = get_format_images($albums);
  204. }
  205. $title = isset($params['title']) && $params['title'] ? trim($params['title']) : "{$nickname} 发布的短视频";
  206. $description = isset($params['description']) && $params['description'] ? trim($params['description']) : "{$nickname} 发布的短视频";
  207. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check', 0);
  208. $publishCheck = $publishCheck > 0 ? $publishCheck : 0;
  209. $tags = isset($params['tags']) && $params['tags'] ? $params['tags'] : '';
  210. $data = [
  211. 'user_id' => $userId,
  212. 'title' => $title,
  213. 'type' => $type,
  214. 'description' => $description,
  215. 'tags' => $tags && is_array($tags) ? implode(',', $tags) : $tags,
  216. 'file_url' => $fileUrl,
  217. 'thumb' => $thumb,
  218. 'albums' => $albums ? $albums : '',
  219. 'music_hash' => isset($params['music_hash']) ? trim($params['music_hash']) : '',
  220. 'music_name' => isset($params['music_name']) ? trim($params['music_name']) : '',
  221. 'music_url' => $musicUrl,
  222. 'goods_id' => $goodsId,
  223. 'visible_type' => isset($params['visible_type']) ? intval($params['visible_type']) : 1,
  224. 'is_comment' => isset($params['is_comment']) ? intval($params['is_comment']) : 1,
  225. 'status' => $publishCheck ? 1 : 2,
  226. 'mark' => 1,
  227. 'publish_at' => date('Y-m-d H:i:s'),
  228. 'create_time' => time(),
  229. ];
  230. RedisService::set('caches:videos:publish', ['params' => $params, 'data' => $data], 600);
  231. if ($id = $this->model->insertGetId($data)) {
  232. $this->error = 1023;
  233. // 发布视频任务处理
  234. TaskService::make()->updateTask($userId, 5, $id);
  235. return ['id' => $id];
  236. }
  237. $this->error = 1024;
  238. return false;
  239. }
  240. /**
  241. * 状态设置
  242. * @return bool
  243. */
  244. public function status()
  245. {
  246. $id = request()->post('id', 0);
  247. $status = request()->post('status', 1);
  248. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  249. $this->error = 2981;
  250. return false;
  251. }
  252. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['status' => $status, 'update_time' => time()])) {
  253. $this->error = 1002;
  254. return true;
  255. }
  256. $this->error = 1003;
  257. return true;
  258. }
  259. /**
  260. * 删除
  261. * @return bool
  262. */
  263. public function delete()
  264. {
  265. // 参数
  266. $param = request()->all();
  267. $id = getter($param, "id");
  268. if (empty($id)) {
  269. $this->error = 2014;
  270. return false;
  271. }
  272. if (!$this->model->where(['id' => $id])->value('id')) {
  273. $this->error = 1039;
  274. return false;
  275. }
  276. if ($this->model->where(['id' => $id])->update(['mark' => 0, 'update_time' => time()])) {
  277. $this->model->where(['mark' => 0])->where('update_time', '<=', time() - 3 * 86400)->delete();
  278. $this->error = 1025;
  279. return true;
  280. } else {
  281. $this->error = 1026;
  282. return false;
  283. }
  284. }
  285. /**
  286. * C2C交易,确认
  287. * @param $userId
  288. * @param $params
  289. * @return array|false
  290. */
  291. public function confirm($userId, $params)
  292. {
  293. $cacheKey = "caches:trade:sellxd:lock_{$userId}";
  294. $role = isset($params['role']) ? trim($params['role']) : '';
  295. if (RedisService::get($cacheKey)) {
  296. $this->error = 1034;
  297. return false;
  298. }
  299. // 交易订单记录
  300. $info = $this->getInfo($params['id']);
  301. if (!in_array($info['status'], [1, 2])) {
  302. $this->error = 1003;
  303. return false;
  304. }
  305. $confirmUser = '';
  306. // 购买 确认付款是用户user_id 确认完成是承兑商acceptor_uid
  307. // 卖出 确认付款是承兑商acceptor_uid 确认完成是用户user_id
  308. if (($info['type'] == 1 && $info['status'] == 1) || ($info['type'] == 2 && $info['status'] == 2)) {
  309. $confirmUser = $info['user_id'];
  310. }
  311. if (($info['type'] == 1 && $info['status'] == 2) || ($info['type'] == 2 && $info['status'] == 1)) {
  312. $confirmUser = $info['user_id'];
  313. }
  314. if ($info['type'] == 1) {
  315. if ($info['status'] == 1) {
  316. $confirmUser = $info['acceptor_uid'];
  317. }
  318. if ($info['status'] == 2) {
  319. $confirmUser = $info['acceptor_uid'];
  320. }
  321. }
  322. // 卖出, 确认收款主体是用户
  323. $member = MemberModel::where(['id' => $confirmUser])->first();
  324. if (empty($member)) {
  325. $this->error = 1003;
  326. return false;
  327. }
  328. // 确认密码
  329. $payPassword = isset($params['pay_password']) ? trim($params['pay_password']) : '';
  330. if ($member['pay_password'] != get_password($payPassword)) {
  331. $this->error = 2038;
  332. return false;
  333. }
  334. // 星豆
  335. DB::beginTransaction();
  336. RedisService::set($cacheKey, $info, rand(2, 3));
  337. // 买入
  338. // 更新trade状态
  339. // 更新account 状态
  340. // 更新member.balance
  341. if ($info['status'] == 1) {
  342. TradeModel::where(['id' => $info['id']])->update(['update_time' => time(), 'remark' => ($info['type'] == 1 ? '购买' : '卖出') . '金豆,订单已付款', 'status' => 2]);
  343. // 消息
  344. if ($info['type'] == 1) {
  345. MessageService::make()->pushMessage($userId, '星豆购买已确认', "您在{$info['create_time']}(UTC+8)下单¥{$info['total']}购买{$info['num']}星豆,已确认付款", 3);
  346. } else {
  347. MessageService::make()->pushMessage($userId, '星豆卖出已确认', "您在{$info['create_time']}(UTC+8)下单¥{$info['total']}卖出{$info['num']}星豆,已确认付款", 3);
  348. }
  349. }
  350. // 订单已完成,放币
  351. if ($info['status'] == 2) {
  352. TradeModel::where(['id' => $info['id']])->update(['update_time' => time(), 'remark' => ($info['type'] == 1 ? '购买' : '卖出') . '金豆,订单完成', 'status' => 3]);
  353. AccountLogModel::where(['source_id' => $info['id']])->update(['update_time' => time(), 'status' => 1]);
  354. if ($info['type'] == 1) {
  355. $updateData = ['balance' => DB::raw("balance + {$info['num']}"), 'update_time' => time()];
  356. if (!MemberModel::where(['id' => $info['user_id']])->update($updateData)) {
  357. $this->error = 2036;
  358. DB::rollBack();
  359. return false;
  360. }
  361. } else {
  362. $updateData = ['balance' => DB::raw("balance + {$info['num']}"), 'update_time' => time()];
  363. if (!MemberModel::where(['id' => $info['acceptor_uid']])->update($updateData)) {
  364. $this->error = 2036;
  365. DB::rollBack();
  366. return false;
  367. }
  368. }
  369. // 承兑商佣金添加
  370. $updateData = ['usdt' => DB::raw("usdt + {$info['bonus_usdt']}"), 'update_time' => time()];
  371. if (!MemberModel::where(['id' => $info['acceptor_uid']])->update($updateData)) {
  372. $this->error = 2036;
  373. DB::rollBack();
  374. return false;
  375. }
  376. // 佣金明细
  377. $acceptorUserInfo = MemberModel::where(['id'=>$info['acceptor_uid']])->first();
  378. // 增加accountLog记录
  379. $log = [
  380. 'user_id' => $info['acceptor_uid'],
  381. 'source_id' => $info['id'],
  382. 'source_order_no' => $info['order_no'],
  383. 'type' => 10,
  384. 'coin_type' => 1,
  385. 'user_type' => 3,
  386. 'money' => $acceptorUserInfo['usdt'] + round($info['bonus_usdt'], 4),
  387. 'actual_money' => round($info['bonus_usdt'], 4),
  388. 'balance' => $acceptorUserInfo['usdt'],
  389. 'create_time' => time(),
  390. 'update_time' => time(),
  391. 'remark' => "C2C交易-承兑商佣金",
  392. 'status' => 1,
  393. 'mark' => 1,
  394. ];
  395. if (!AccountLogModel::insertGetId($log)) {
  396. $this->error = 2407;
  397. DB::rollBack();
  398. RedisService::clear($cacheKey);
  399. return false;
  400. }
  401. // 用户交易获取待返积分
  402. $c2cRewardPoints = ConfigService::make()->getConfigByCode('c2c_reward_points', 100);
  403. $poins = round($c2cRewardPoints * $info['total'] * 10000)/10000;
  404. $updateData = ['wait_score' => DB::raw("wait_score + {$poins}"), 'update_time' => time()];
  405. if (!MemberModel::where(['id' => $info['user_id']])->update($updateData)) {
  406. $this->error = 2036;
  407. DB::rollBack();
  408. return false;
  409. }
  410. $userInfo = MemberModel::where(['id'=>$info['user_id']])->first();
  411. // 增加accountLog记录
  412. $log = [
  413. 'user_id' => $info['user_id'],
  414. 'source_id' => $info['id'],
  415. 'source_order_no' => $info['order_no'],
  416. 'type' => 102,
  417. 'coin_type' => 4,
  418. 'user_type' => 1,
  419. 'money' => $userInfo['usdt'],
  420. 'actual_money' => $poins,
  421. 'balance' => $userInfo['wait_score'],
  422. 'create_time' => time(),
  423. 'update_time' => time(),
  424. 'remark' => "C2C交易-用户待返积分",
  425. 'status' => 1,
  426. 'mark' => 1,
  427. ];
  428. if (!AccountLogModel::insertGetId($log)) {
  429. $this->error = 2407;
  430. DB::rollBack();
  431. RedisService::clear($cacheKey);
  432. return false;
  433. }
  434. // 消息
  435. if ($info['type'] == 1) {
  436. MessageService::make()->pushMessage($userId, '星豆购买已完成', "您在{$info['create_time']}(UTC+8)下单¥{$info['total']}购买{$info['num']}星豆,已成功到账", 3);
  437. } else {
  438. MessageService::make()->pushMessage($userId, '星豆卖出已完成', "您在{$info['create_time']}(UTC+8)下单¥{$info['total']}卖出{$info['num']}星豆,已成功扣减", 3);
  439. }
  440. }
  441. DB::commit();
  442. $this->error = 2037;
  443. RedisService::clear($cacheKey);
  444. return ['trade_id' => $info['id']];
  445. }
  446. /**
  447. * 取消订单
  448. * @param $userId
  449. * @param $params
  450. * @return bool
  451. */
  452. public function cancel($userId, $params)
  453. {
  454. $id = request()->post('id', 0);
  455. $type = request()->post('type', 1);
  456. if (!$id || !$info = $this->model->where(['id' => $id, 'mark' => 1])->select(['id','user_id','acceptor_uid','status'])->first()) {
  457. $this->error = 3031;
  458. return false;
  459. }
  460. if($info['status'] != 1){
  461. $this->error = 3032;
  462. return false;
  463. }
  464. if($userId && ($userId != $info['user_id'] || $userId != $info['acceptor_uid'])){
  465. $this->error = 2913;
  466. return false;
  467. }
  468. if($type <=0){
  469. $this->error = 3033;
  470. return false;
  471. }
  472. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['status' => -1,'cancel_type'=> $type, 'update_time' => time()])) {
  473. $this->error = 1002;
  474. return true;
  475. }
  476. $this->error = 1003;
  477. return true;
  478. }
  479. }