GoodsLogic.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. <?php
  2. namespace logicmodel;
  3. use app\common\model\Config;
  4. use comservice\GetRedis;
  5. use comservice\Response;
  6. use datamodel\Author;
  7. use datamodel\Goods;
  8. use datamodel\GoodsCategory;
  9. use datamodel\GoodsConvert;
  10. use datamodel\GoodsHash;
  11. use datamodel\GoodsOrders;
  12. use datamodel\GoodsTransfer;
  13. use datamodel\OrdersGoods;
  14. use datamodel\Users;
  15. use datamodel\UsersGoods;
  16. use logicmodel\award\Recommend;
  17. use think\Db;
  18. use think\Exception;
  19. class GoodsLogic
  20. {
  21. private $usersGoodsData;
  22. private $redis;
  23. public function __construct()
  24. {
  25. $this->usersGoodsData = new UsersGoods();
  26. $this->redis = GetRedis::getRedis();
  27. }
  28. /**
  29. * 搜索
  30. * @param $search
  31. * @return array
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \think\exception\DbException
  35. */
  36. public function search($search){
  37. $goods = (new Goods())->alias('g')
  38. ->join('goods_category gc','gc.id = g.goods_category_id')
  39. ->where(['g.is_show'=>1,'g.is_del'=>0,'g.name'=>['like','%'.$search.'%'],'gc.is_convert'=>0,'gc.is_show'=>1,'gc.is_del'=>0])
  40. ->field(['g.id','g.name','g.image'])
  41. ->order(['g.order asc'])
  42. ->select();
  43. $author = (new Author())->where(['is_show'=>1,'is_del'=>0,'name'=>['like','%'.$search.'%']])->select();
  44. $goods = !empty($goods)?collection($goods):[];
  45. $author = !empty($author)?collection($author):[];
  46. return Response::success('success',['goods'=>$goods,'author'=>$author]);
  47. }
  48. /**
  49. * 作品分类
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. */
  55. public function goodsCategoryList(){
  56. $data = (new GoodsCategory())->where(['is_show'=>1,'is_del'=>0,'is_convert'=>0])->order(['order asc'])->select();
  57. if($data){
  58. $data = collection($data)->toArray();
  59. return Response::success('success',$data);
  60. }
  61. return Response::success('success',[]);
  62. }
  63. /**
  64. * 作品分类
  65. * @return array
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public function goodsCategory(){
  71. $data = (new GoodsCategory())->where(['is_show'=>1,'is_del'=>0,'is_convert'=>0])->order(['order asc'])->limit(3)->select();
  72. if($data){
  73. $data = collection($data)->toArray();
  74. $goodsData = new Goods();
  75. $field = ['g.*','a.name author_name','a.image author_image','n.name network_name'];
  76. $order = ['g.order asc'];
  77. foreach ($data as $key=>&$value){
  78. $goods_category_id = $value['id'];
  79. $goods = $goodsData->alias('g')
  80. ->join('author a','a.id = g.author_id')
  81. ->join('network n','n.id = g.network_id')
  82. ->join('goods_category gc','gc.id = g.goods_category_id')
  83. ->where(['g.is_show'=>1,'g.is_del'=>0,'g.goods_category_id'=>$goods_category_id])
  84. ->order($order)
  85. ->field($field)
  86. ->limit(2)
  87. ->select();
  88. if(!empty($goods)){
  89. $goods = collection($goods)->toArray();
  90. $time = date('Y-m-d H:i:s');
  91. foreach ($goods as $k=>$v){
  92. if( $time >= $v['start_time']){
  93. if($v['surplus'] > 0){
  94. $goods[$k]['status'] = 1; //售卖中
  95. $goods[$k]['status_text'] = '抢购中';
  96. }else{
  97. $goods[$k]['status'] = 2; //已售罄
  98. $goods[$k]['status_text'] = '已售罄';
  99. }
  100. }else{
  101. $diff_time = strtotime($v['start_time']) - strtotime($time); //时间差
  102. if($diff_time >= 86400) {
  103. //未开售
  104. $goods[$k]['status'] = 3; //显示开售时间
  105. $goods[$k]['status_text'] = '敬请期待 '.date('m.d H:i',strtotime($v['start_time'])).' 开售';
  106. }else{
  107. //未开售
  108. $goods[$k]['status'] = 4; //显示倒计时
  109. $goods[$k]['status_text'] = '倒计时';
  110. $goods[$k]['diff_time'] = $diff_time;
  111. }
  112. }
  113. }
  114. $goods = addWebSiteUrl($goods,['image','author_image']);
  115. }else{
  116. $goods =[];
  117. }
  118. $data[$key]['goods'] = $goods;
  119. }
  120. return Response::success('success',$data);
  121. }
  122. return Response::success('success',[]);
  123. }
  124. /**
  125. * 新品商品列表
  126. * @param $goods_id
  127. * @param $author_id
  128. * @param $time_order
  129. * @param $price_order
  130. * @param $is_home
  131. * @param $goods_category_id
  132. * @param $page
  133. * @param $pagesize
  134. * @return array
  135. * @throws Exception
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. * @throws \think\exception\DbException
  139. */
  140. public function goodsList($goods_id,$author_id,$time_order,$price_order,$goods_category_id,$page,$pagesize){
  141. $where['g.is_del'] = 0;
  142. $where['g.is_show'] = 1;
  143. $where['gc.is_convert'] = 0;
  144. $where['gc.is_del'] = 0;
  145. $where['gc.is_show'] = 1;
  146. if(!empty($goods_category_id))$where['g.goods_category_id'] = $goods_category_id;
  147. if(!empty($goods_id)) $where['g.id'] = $goods_id;
  148. if(!empty($author_id)) $where['a.id'] = $author_id;
  149. if($time_order > 0){
  150. if($time_order == 1){
  151. $order = ['g.start_time asc','g.order asc'];
  152. }else{
  153. $order = ['g.start_time desc','g.order asc'];
  154. }
  155. }elseif ($price_order > 0){
  156. if($price_order == 1){
  157. $order = ['g.price asc','g.order asc'];
  158. }else{
  159. $order = ['g.price desc','g.order asc'];
  160. }
  161. }else{
  162. $order = ['g.order asc'];
  163. }
  164. $goodsData = new Goods();
  165. $count = $goodsData->alias('g')
  166. ->join('author a','a.id = g.author_id')
  167. ->join('network n','n.id = g.network_id')
  168. ->join('goods_category gc','gc.id = g.goods_category_id')
  169. ->where($where)
  170. ->count();
  171. if($count <= 0) return Response::success('暂无数据',['count'=>$count,'data'=>[],'page'=>$page,'pagesize'=>$pagesize]);
  172. $data = $goodsData
  173. ->alias('g')
  174. ->join('author a','a.id = g.author_id')
  175. ->join('network n','n.id = g.network_id')
  176. ->join('goods_category gc','gc.id = g.goods_category_id')
  177. ->where($where)
  178. ->order($order)
  179. ->field(['g.*','a.name author_name','a.image author_image','n.name network_name'])
  180. ->page($page,$pagesize)
  181. ->select();
  182. if(!empty($data)){
  183. $data = collection($data)->toArray();
  184. $time = date('Y-m-d H:i:s');
  185. foreach ($data as $k=>$v){
  186. if( $time >= $v['start_time']){
  187. if($v['surplus'] > 0){
  188. $data[$k]['status'] = 1; //售卖中
  189. $data[$k]['status_text'] = '抢购中';
  190. }else{
  191. $data[$k]['status'] = 2; //已售罄
  192. $data[$k]['status_text'] = '已售罄';
  193. }
  194. }else{
  195. $diff_time = strtotime($v['start_time']) - strtotime($time); //时间差
  196. if($diff_time >= 86400) {
  197. //未开售
  198. $data[$k]['status'] = 3; //显示开售时间
  199. $data[$k]['status_text'] = '敬请期待 '.date('m.d H:i',strtotime($v['start_time'])).' 开售';
  200. }else{
  201. //未开售
  202. $data[$k]['status'] = 4; //显示倒计时
  203. $data[$k]['status_text'] = '倒计时';
  204. $data[$k]['diff_time'] = $diff_time;
  205. }
  206. }
  207. }
  208. $data = addWebSiteUrl($data,['image','author_image']);
  209. }
  210. return Response::success('success',['count'=>$count,'data'=>$data,'page'=>$page,'pagesize'=>$pagesize]);
  211. }
  212. /**
  213. * 积分NFT兑换
  214. * @param $page
  215. * @param $pagesize
  216. * @return array
  217. * @throws Exception
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\ModelNotFoundException
  220. * @throws \think\exception\DbException
  221. */
  222. public function integralGoodsList($page,$pagesize){
  223. $where['g.is_del'] = 0;
  224. $where['g.is_show'] = 1;
  225. $where['gc.is_convert'] = 1;
  226. $where['gc.is_del'] = 0;
  227. $where['gc.is_show'] = 1;
  228. $order = ['g.order asc'];
  229. $goodsData = new Goods();
  230. $count = $goodsData->alias('g')
  231. ->join('author a','a.id = g.author_id')
  232. ->join('network n','n.id = g.network_id')
  233. ->join('goods_category gc','gc.id = g.goods_category_id')
  234. ->where($where)
  235. ->count();
  236. if($count <= 0) return Response::success('暂无数据',['count'=>$count,'data'=>[],'page'=>$page,'pagesize'=>$pagesize]);
  237. $data = $goodsData
  238. ->alias('g')
  239. ->join('author a','a.id = g.author_id')
  240. ->join('network n','n.id = g.network_id')
  241. ->join('goods_category gc','gc.id = g.goods_category_id')
  242. ->where($where)
  243. ->order($order)
  244. ->field(['g.*','a.name author_name','a.image author_image','n.name network_name'])
  245. ->page($page,$pagesize)
  246. ->select();
  247. if(!empty($data)){
  248. $data = collection($data)->toArray();
  249. $data = addWebSiteUrl($data,['image','author_image']);
  250. }
  251. return Response::success('success',['count'=>$count,'data'=>$data,'page'=>$page,'pagesize'=>$pagesize]);
  252. }
  253. /**
  254. * 商品详情
  255. * @param $id
  256. * @return array
  257. * @throws \think\Exception
  258. * @throws \think\db\exception\DataNotFoundException
  259. * @throws \think\db\exception\ModelNotFoundException
  260. * @throws \think\exception\DbException
  261. */
  262. public function goodsDetail($id){
  263. $where['g.is_del'] = 0;
  264. $where['g.is_show'] = 1;
  265. $where['g.id'] = $id;
  266. $field = ['g.*','gc.name goods_category_name','a.name author_name','a.image author_image','n.name network_name','gc.is_convert'];
  267. $field[] = 'c.name as buy_goods_name';//空头所需藏品
  268. $field[] = 'b.num as buy_need_num';//空头所需藏品数量
  269. $goodsData = new Goods();
  270. $data = $goodsData
  271. ->alias('g')
  272. ->join('author a','a.id = g.author_id')
  273. ->join('goods_category gc','gc.id = g.goods_category_id')
  274. ->join('network n','n.id = g.network_id')
  275. ->join('goods_buy b','g.id=b.goods_id','left')
  276. ->join('goods c','c.id=b.source_goods_id','left')
  277. ->where($where)
  278. ->field($field)
  279. ->find();
  280. if($data) {
  281. $data = $data->toArray();
  282. $time = date('Y-m-d H:i:s');
  283. if( $time >= $data['start_time']){
  284. if($data['surplus'] > 0){
  285. $data['status'] = 1; //售卖中
  286. $data['status_text'] = '抢购中';
  287. }else{
  288. $data['status'] = 2; //已售罄
  289. $data['status_text'] = '已售罄';
  290. }
  291. }else{
  292. $diff_time = strtotime($data['start_time']) - strtotime($time); //时间差
  293. if($diff_time >= 86400) {
  294. //未开售
  295. $data['status'] = 3; //显示开售时间
  296. $data['status_text'] = '敬请期待 '.date('m.d H:i',strtotime($data['start_time'])).' 开售';
  297. }else{
  298. //未开售
  299. $data['status'] = 4; //显示倒计时
  300. $data['status_text'] = '倒计时';
  301. $data['diff_time'] = $diff_time;
  302. }
  303. }
  304. $data = addWebSiteUrl($data,['author_image','image','images']);
  305. $data['content'] = content($data['content']);
  306. return Response::success('success',$data);
  307. }
  308. return Response::fail('商品信息错误');
  309. }
  310. /**
  311. * 交易记录
  312. * @param $goods_id
  313. * @param $page
  314. * @param $pagesize
  315. * @return array
  316. * @throws \think\Exception
  317. * @throws \think\db\exception\DataNotFoundException
  318. * @throws \think\db\exception\ModelNotFoundException
  319. * @throws \think\exception\DbException
  320. */
  321. public function ordersRecord($goods_id,$page,$pagesize){
  322. $where['o.goods_id'] = $goods_id;
  323. $where['o.status'] = 1;
  324. $goodsOrdersData= new GoodsOrders();
  325. $count = $goodsOrdersData->alias('o')
  326. ->join('users u','u.id = o.sale_uid')
  327. ->where($where)->count();
  328. if($count <= 0) return Response::success('暂无数据',['count'=>$count,'data'=>[],'page'=>$page,'pagesize'=>$pagesize]);
  329. $data = $goodsOrdersData->alias('o')
  330. ->join('users u','u.id = o.sale_uid')
  331. ->where($where)->order(['o.id desc'])
  332. ->field(['u.head_image','u.nick_name','o.price','o.create_time'])
  333. ->page($page,$pagesize)
  334. ->select();
  335. return Response::success('暂无数据',['count'=>$count,'data'=>$data,'page'=>$page,'pagesize'=>$pagesize]);
  336. }
  337. /**商品列表
  338. * @param $goods_id
  339. * @param $page
  340. * @param $pagesize
  341. * @param $users_goods_id
  342. * @return array
  343. * @throws \think\Exception
  344. * @throws \think\db\exception\DataNotFoundException
  345. * @throws \think\db\exception\ModelNotFoundException
  346. * @throws \think\exception\DbException
  347. */
  348. public function tradeRecord($goods_id,$users_goods_id,$page,$pagesize){
  349. $where['ug.goods_id'] = $goods_id;
  350. $where['ug.status'] = ['in',[2]];
  351. if(!empty($users_goods_id)) $where['ug.id'] = ['<>',$users_goods_id];
  352. $usersGoodsData = new UsersGoods();
  353. $count = $usersGoodsData->alias('ug')
  354. ->join('users u','u.id = ug.uid')
  355. ->join('goods g','g.id = ug.goods_id')
  356. ->where($where)
  357. ->count();
  358. if($count <= 0) return Response::success('暂无数据',['count'=>$count,'data'=>[],'page'=>$page,'pagesize'=>$pagesize]);
  359. $field = ['ug.*','g.name goods_name','u.nick_name','u.head_image','g.stock'];
  360. $data = $usersGoodsData->alias('ug')
  361. ->join('users u','u.id = ug.uid')
  362. ->join('goods g','g.id = ug.goods_id')
  363. ->field($field)
  364. ->where($where)
  365. ->order(['ug.order asc','ug.sales_time asc'])
  366. ->page($page,$pagesize)
  367. ->select();
  368. return Response::success('暂无数据',['count'=>$count,'data'=>$data,'page'=>$page,'pagesize'=>$pagesize]);
  369. }
  370. /**
  371. * NFT购买
  372. * @param $userInfo
  373. * @param $id
  374. * @param $pay_type
  375. * @return array
  376. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  377. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  378. * @throws \GuzzleHttp\Exception\GuzzleException
  379. * @throws \think\Exception
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\ModelNotFoundException
  382. * @throws \think\exception\DbException
  383. * @throws \think\exception\PDOException
  384. */
  385. public function apply($userInfo,$id,$pay_type){
  386. if($userInfo['is_auth'] == 0) return Response::bindAuth();
  387. if(!Config::getValue('wallet_open')){
  388. return Response::fail('暂未开放支付');
  389. }
  390. if( $this->redis->getItem('goods_order_'.$userInfo['id'])){
  391. return Response::fail('频繁操作');
  392. }
  393. $this->redis->setItem('goods_order_'.$userInfo['id'],0);
  394. $this->redis->settime('goods_order_'.$userInfo['id'],10);
  395. //虚拟用户购买
  396. $goodsData = new Goods();
  397. $goodsInfo = $goodsData->alias('g')
  398. ->join('goods_category gc','gc.id= g.goods_category_id')
  399. ->where(['g.id'=>$id,'g.is_show'=>1,'g.is_del'=>0])
  400. ->field(['g.*','gc.is_convert'])
  401. ->find();
  402. if(empty($goodsInfo)) return Response::fail('作品信息错误');
  403. if($goodsInfo['surplus'] <= 0 ) return Response::fail('已售罄');
  404. $is_convert = $goodsInfo['is_convert'];
  405. if($is_convert == 0){
  406. $time = date('Y-m-d H:i:s');
  407. if($time < $goodsInfo['start_time']) return Response::fail('未到开售时间');
  408. return $this->newGoods($userInfo,$goodsInfo,$pay_type);
  409. }else{
  410. return $this->integralGoods($userInfo['id'],$goodsInfo);
  411. }
  412. }
  413. /**
  414. * 新品下单
  415. * @param $userInfo
  416. * @param $goodsInfo
  417. * @param $pay_type
  418. * @return array
  419. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  420. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  421. * @throws \GuzzleHttp\Exception\GuzzleException
  422. * @throws \think\Exception
  423. * @throws \think\db\exception\DataNotFoundException
  424. * @throws \think\db\exception\ModelNotFoundException
  425. * @throws \think\exception\DbException
  426. * @throws \think\exception\PDOException
  427. */
  428. public function newGoods($userInfo,$goodsInfo,$pay_type){
  429. $ordersGoodsData = new OrdersGoods();
  430. $pay_type=1;
  431. $uid = $userInfo['id'];
  432. $count = $ordersGoodsData->where(['uid'=>$uid,'status'=>0,'create_time'=>['>=',date('Y-m-d')]])->count();
  433. if($count >= 3) return Response::fail('今日未付款次数太多,限制抢购');
  434. $price = $goodsInfo['price'];
  435. $goods_id = $goodsInfo['id'];
  436. $order_num = uniqueNum();
  437. $time = date('Y-m-d H:i:s');
  438. $hash=GoodsHash::zero()->queryGoods($goods_id)->lock(true)->find();
  439. if(!$hash){
  440. Db::rollback();
  441. return Response::fail('无可用hash');
  442. }
  443. if($pay_type == 1){
  444. //余额抵扣
  445. Db::startTrans();
  446. $result = (new AccountLogic())->subAccount($uid,1,$price,'购买新品','购买新品'.$goodsInfo['name'].'账户扣除');
  447. if(!$result){
  448. Db::rollback();
  449. return Response::fail('账户余额不足');
  450. }
  451. //生成购买订单
  452. $order['uid'] = $uid;
  453. $order['goods_id'] = $goods_id;
  454. $order['order_num'] = $order_num;
  455. $order['goods_category_id'] = 1;
  456. $order['number'] = 1;
  457. $order['price'] = $price;
  458. $order['total_price'] = $price;
  459. $order['pay_type'] = $pay_type;
  460. $order['pay_time'] = $time;
  461. $order['create_time'] = $time;
  462. $order['status'] = 1;
  463. $result = $ordersGoodsData->insertGetId($order);
  464. if(!$result){
  465. Db::rollback();
  466. return Response::fail('购买失败');
  467. }
  468. $hash->setOrder($result);
  469. $goods['uid'] = $uid;
  470. $goods['goods_id'] = $goods_id;
  471. $goods['price'] = $price;
  472. $goods['create_time'] = date('Y-m-d H:i:s');
  473. $result = $this->usersGoodsData->saveEntityAndGetId($goods);
  474. if($result >0){
  475. UsersGoods::get($result)->addHash($hash);
  476. Db::commit();
  477. $goodsData = new Goods();
  478. $goodsData->where(['id'=>$goods_id])->setInc('sales');
  479. $goodsData->where(['id'=>$goods_id])->setDec('surplus');
  480. return Response::success('购买成功',['is_pay'=>1]);
  481. }
  482. Db::rollback();
  483. return Response::fail('购买失败');
  484. }elseif($pay_type == 2){
  485. //微信支付
  486. if($userInfo['is_wx_auth'] == 0 || empty($userInfo['wx_open_id'])) return Response::wxAuth();
  487. //生成支付信息
  488. $notify_url = config('site.wx_orders_goods_notify');
  489. $pay = (new WxLogic())->pay($order_num,'购买藏品'.$goodsInfo['name'],$price,$userInfo['wx_open_id'],$notify_url);
  490. if($pay === false) return Response::fail('订单生成失败');
  491. //生成购买订单
  492. $order['uid'] = $uid;
  493. $order['goods_id'] = $goods_id;
  494. $order['order_num'] = $order_num;
  495. $order['goods_category_id'] = 1;
  496. $order['number'] = 1;
  497. $order['price'] = $price;
  498. $order['total_price'] = $price;
  499. $order['pay_type'] = $pay_type;
  500. $order['create_time'] = $time;
  501. $result = (new OrdersGoods())->insertGetId($order);
  502. $hash->setOrder($result);
  503. if($result){
  504. Db::commit();
  505. return Response::success('购买成功',['is_pay'=>0,'pay'=>$pay]);
  506. }
  507. Db::rollback();
  508. return Response::fail('购买失败');
  509. }elseif ($pay_type == 3){
  510. //微信APP支付
  511. $notify_url = config('site.wx_app_orders_goods_notify');
  512. $pay = (new WxLogic())->appPay($order_num,'购买藏品'.$goodsInfo['name'],$price,$notify_url);
  513. if($pay === false) return Response::fail('订单生成失败');
  514. //生成购买订单
  515. $order['uid'] = $uid;
  516. $order['goods_id'] = $goods_id;
  517. $order['order_num'] = $order_num;
  518. $order['goods_category_id'] = 1;
  519. $order['number'] = 1;
  520. $order['price'] = $price;
  521. $order['total_price'] = $price;
  522. $order['pay_type'] = $pay_type;
  523. $order['create_time'] = $time;
  524. $result = (new OrdersGoods())->insertGetId($order);
  525. $hash->setOrder($result);
  526. if($result){
  527. Db::commit();
  528. return Response::success('购买成功',['is_pay'=>0,'pay'=>$pay]);
  529. }
  530. Db::rollback();
  531. return Response::fail('购买失败');
  532. }elseif ($pay_type == 4){
  533. //支付宝APP支付
  534. $notify_url = config('site.ali_app_orders_goods_notify');
  535. $pay = (new AliLogic())->appPay($order_num,'购买藏品'.$goodsInfo['name'],$price,$notify_url);
  536. if($pay === false) return Response::fail('订单生成失败');
  537. //生成购买订单
  538. $order['uid'] = $uid;
  539. $order['goods_id'] = $goods_id;
  540. $order['order_num'] = $order_num;
  541. $order['goods_category_id'] = 1;
  542. $order['number'] = 1;
  543. $order['price'] = $price;
  544. $order['total_price'] = $price;
  545. $order['pay_type'] = $pay_type;
  546. $order['create_time'] = $time;
  547. $result = $ordersGoodsData->insertGetId($order);
  548. $hash->setOrder($result);
  549. if($result){
  550. Db::commit();
  551. return Response::success('购买成功',['is_pay'=>0,'pay'=>$pay]);
  552. }
  553. Db::rollback();
  554. return Response::fail('购买失败');
  555. }elseif ($pay_type == 5){
  556. //支付宝h5支付
  557. $notify_url = config('site.ali_app_orders_goods_notify');
  558. $pay = (new AliLogic())->wapPay($order_num,'购买藏品'.$goodsInfo['name'],$price,$notify_url);
  559. if($pay === false) return Response::fail('订单生成失败');
  560. //生成购买订单
  561. $order['uid'] = $uid;
  562. $order['goods_id'] = $goods_id;
  563. $order['order_num'] = $order_num;
  564. $order['goods_category_id'] = 1;
  565. $order['number'] = 1;
  566. $order['price'] = $price;
  567. $order['total_price'] = $price;
  568. $order['pay_type'] = $pay_type;
  569. $order['create_time'] = $time;
  570. $result = $ordersGoodsData->insertGetId($order);
  571. $hash->setOrder($result);
  572. if($result){
  573. Db::commit();
  574. return Response::success('购买成功',['is_pay'=>0,'pay'=>$pay]);
  575. }
  576. Db::rollback();
  577. return Response::fail('购买失败');
  578. }else{
  579. return Response::fail('支付方式错误');
  580. }
  581. }
  582. /**
  583. * 积分兑换
  584. * @param $uid
  585. * @param $goodsInfo
  586. * @return array
  587. * @throws \think\Exception
  588. * @throws \think\db\exception\DataNotFoundException
  589. * @throws \think\db\exception\ModelNotFoundException
  590. * @throws \think\exception\DbException
  591. * @throws \think\exception\PDOException
  592. */
  593. public function integralGoods($uid,$goodsInfo){
  594. $price = $goodsInfo['price'];
  595. $goods_id = $goodsInfo['id'];
  596. $order_num = uniqueNum();
  597. $time = date('Y-m-d H:i:s');
  598. $goodsConvertData = new GoodsConvert();
  599. $count = $goodsConvertData->where(['uid'=>$uid])->count();
  600. if($count >= 3) return Response::fail('超出兑换次数');
  601. Db::startTrans();
  602. $result = (new AccountLogic())->subAccount($uid,2,$price,'NFT兑换','兑换NFT'.$goodsInfo['name'].'账户扣除');
  603. if(!$result){
  604. Db::rollback();
  605. return Response::fail('积分不足');
  606. }
  607. $hash=GoodsHash::zero()->queryGoods($goods_id)->lock(true)->find();
  608. if(!$hash){
  609. Db::rollback();
  610. return Response::fail('无可用hash');
  611. }
  612. //生成购买订单
  613. $order['uid'] = $uid;
  614. $order['goods_id'] = $goods_id;
  615. $order['order_num'] = $order_num;
  616. $order['goods_category_id'] = 3;
  617. $order['number'] = 1;
  618. $order['price'] = $price;
  619. $order['total_price'] = $price;
  620. $order['pay_type'] = 5;
  621. $order['pay_time'] = $time;
  622. $order['create_time'] = $time;
  623. $order['status'] = 1;
  624. $result = (new OrdersGoods())->insertGetId($order);
  625. if(!$result){
  626. Db::rollback();
  627. return Response::fail('兑换失败');
  628. }
  629. $hash->setOrder($result);
  630. try {
  631. $orderGoods=OrdersGoods::get($result);
  632. $orderGoods->addHash($hash);
  633. }catch (\Exception $e){
  634. Db::rollback();
  635. return Response::fail('购买失败1');
  636. }
  637. $goods['uid'] = $uid;
  638. $goods['goods_id'] = $goods_id;
  639. $goods['price'] = $price;
  640. $goods['create_time'] = date('Y-m-d H:i:s');
  641. $result = $this->usersGoodsData->saveEntityAndGetId($goods);
  642. if($result >0){
  643. Db::commit();
  644. $goodsConvertData->insertGetId(['uid'=>$uid,'goods_id'=>$goods_id,'create_time'=>date('Y-m-d H:i:s')]);
  645. $goodsData = new Goods();
  646. $goodsData->where(['id'=>$goods_id])->setInc('sales');
  647. $goodsData->where(['id'=>$goods_id])->setDec('surplus');
  648. return Response::success('兑换成功',['is_pay'=>1]);
  649. }
  650. Db::rollback();
  651. return Response::fail('兑换失败');
  652. }
  653. /**
  654. * 会员NFT作品列表
  655. * @param $uid
  656. * @param $status
  657. * @return array
  658. * @throws \think\db\exception\DataNotFoundException
  659. * @throws \think\db\exception\ModelNotFoundException
  660. * @throws \think\exception\DbException
  661. */
  662. public function usersGoodsList($uid,$status){
  663. $where['ug.is_del'] = 0;
  664. $where['ug.uid'] = $uid;
  665. if($status > 1){
  666. $where['ug.status'] = ['in',[2,3,4]];
  667. } else{
  668. $where['ug.status'] = ['in',[1,5]];
  669. }
  670. $field = ['ug.id','ug.price','ug.is_show','ug.status','g.name','g.image','g.desc','a.name author_name','a.image author_image','n.name network_name'];
  671. $field[]='count(ug.id) as goods_num';
  672. $data = $this->usersGoodsData->alias('ug')
  673. ->join('goods g','g.id = ug.goods_id')
  674. ->join('author a','a.id = g.author_id')
  675. ->join('goods_category gc','gc.id = g.goods_category_id')
  676. ->join('network n','n.id = g.network_id')
  677. ->where($where)
  678. ->group('goods_id')
  679. ->field($field)
  680. ->order(['ug.id desc'])
  681. ->select();
  682. if(!empty($data)) {
  683. $data = collection($data)->toArray();
  684. $data = addWebSiteUrl($data,['image','author_image']);
  685. return Response::success('success',$data);
  686. }
  687. return Response::success('暂无数据',[]);
  688. }
  689. /**
  690. * 会员作品详情
  691. * @param $uid
  692. * @param $id
  693. * @return array
  694. * @throws Exception
  695. * @throws \think\db\exception\DataNotFoundException
  696. * @throws \think\db\exception\ModelNotFoundException
  697. * @throws \think\exception\DbException
  698. */
  699. public function usersGoodsDetail($uid,$id){
  700. $where['ug.id'] = $id;
  701. $where['ug.uid'] = $uid;
  702. $field = ['g.*','ug.price','ug.is_show','ug.status','ug.id','g.id goods_id','a.name author_name','a.image author_image','n.name network_name'];
  703. $data = $this->usersGoodsData->alias('ug')
  704. ->join('goods g','g.id = ug.goods_id')
  705. ->join('author a','a.id = g.author_id')
  706. ->join('goods_category gc','gc.id = g.goods_category_id')
  707. ->join('network n','n.id = g.network_id')
  708. ->where($where)
  709. ->field($field)
  710. ->find();
  711. if($data){
  712. $data = $data->toArray();
  713. $data = addWebSiteUrl($data,['image','author_image']);
  714. return Response::success('success',$data);
  715. }
  716. return Response::fail('盲盒信息错误');
  717. }
  718. /**
  719. * 出售藏品
  720. * @param $uid
  721. * @param $id
  722. * @return array
  723. * @throws \think\db\exception\DataNotFoundException
  724. * @throws \think\db\exception\ModelNotFoundException
  725. * @throws \think\exception\DbException
  726. */
  727. public function sale($uid,$id,$price){
  728. if($price < 0.01) return Response::fail('价格输入错误');
  729. $is_trade = Config::getValue('is_trade',0);
  730. if($is_trade == 0) return Response::fail('市场未开启');
  731. $info = $this->usersGoodsData->where(['uid'=>$uid,'id'=>$id,'status'=>1])->find();
  732. if(empty($info)) return Response::fail('盲盒信息错误');
  733. Users::get($uid)->checkPayPwd();
  734. $result = $this->usersGoodsData->where(['id'=>$id])->update(['status'=>2,'price'=>$price,'sales_time'=>date('Y-m-d H:i:s')]);
  735. if($result) return Response::success('出售成功');
  736. return Response::fail('出售失败');
  737. }
  738. /**
  739. * 转赠
  740. * @param $uid
  741. * @param $id
  742. * @param $phone
  743. * @return array
  744. * @throws \think\db\exception\DataNotFoundException
  745. * @throws \think\db\exception\ModelNotFoundException
  746. * @throws \think\exception\DbException
  747. */
  748. public function transfer($uid,$id,$phone){
  749. $allowTransfer=Config::getValue('system_allow_transfer',0);
  750. if(!$allowTransfer){
  751. return Response::fail('系统优化中,暂时无法转赠');
  752. }
  753. $targetInfo = (new Users())->where(['is_del'=>0,'phone'=>$phone])->find();
  754. if(empty($targetInfo)) return Response::fail('转赠信息错误');
  755. Users::get($uid)->checkPayPwd();
  756. $info = $this->usersGoodsData->where(['uid'=>$uid,'id'=>$id,'status'=>['in',[1,2]]])->find();
  757. if(empty($info)) return Response::fail('作品信息错误');
  758. if(!$info['is_cool']){
  759. api_error('冷却中...');
  760. }
  761. if($targetInfo['id']==$uid){
  762. return Response::fail('不能转赠给自己');
  763. }
  764. //生成转增记录
  765. $time = date('Y-m-d H:i:s');
  766. $transfer['users_goods_id'] = $id;
  767. $transfer['uid'] = $uid;
  768. $transfer['target_uid'] = $targetInfo['id'];
  769. $transfer['goods_id'] = $info['goods_id'];
  770. $transfer['price'] = $info['price'];
  771. $transfer['create_time'] = $time;
  772. Db::startTrans();
  773. $result = (new GoodsTransfer())->insertGetId($transfer);
  774. if(!$result){
  775. Db::rollback();
  776. return Response::fail('转赠失败');
  777. }
  778. $result = $this->usersGoodsData->where(['id'=>$id])->update(['status'=>5,'is_del'=>1]);
  779. if(!$result){
  780. Db::rollback();
  781. return Response::fail('转赠失败');
  782. }
  783. //生成新纪录
  784. $goods['uid'] = $targetInfo['id'];
  785. $goods['goods_id'] = $info['goods_id'];
  786. $goods['price'] = $info['price'];
  787. $goods['goods_num'] = $info['goods_num'];
  788. $goods['create_time'] = $time;
  789. $goods['goods_hash']=$info['goods_hash'];
  790. $goods['user_hash']=$info['user_hash'];
  791. $result = $this->usersGoodsData->insertGetId($goods);
  792. ChainLogic::instance()->transfer(Users::get($uid),$goods['goods_hash'],$targetInfo);
  793. if($result){
  794. Db::commit();
  795. return Response::success('转赠成功');
  796. }
  797. Db::rollback();
  798. return Response::fail('转赠失败');
  799. }
  800. /**
  801. * 修改藏品价格
  802. * @param $uid
  803. * @param $id
  804. * @param $price
  805. * @return array
  806. */
  807. public function updatePrice($uid,$id,$price){
  808. if($price < 0.01) return Response::fail('价格输入错误');
  809. $result = $this->usersGoodsData->where(['uid'=>$uid,'id'=>$id,'status'=>['in',[1,2]]])->update(['price'=>$price]);
  810. if($result) return Response::success('修改成功');
  811. return Response::fail('修改失败');
  812. }
  813. /**
  814. * 切换商品状态
  815. * @param $uid
  816. * @param $id
  817. * @param $is_show
  818. * @return array
  819. */
  820. public function updateShow($uid,$id,$is_show){
  821. $result = $this->usersGoodsData->where(['uid'=>$uid,'id'=>$id])->update(['is_show'=>$is_show]);
  822. if($result) return Response::success($is_show == 1?'上架成功':'下架成功');
  823. return Response::fail($is_show == 1?'上架失败':'下架失败');
  824. }
  825. public function preOrder($id,$uid){
  826. }
  827. }