TradeService.php 18 KB

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