Collect.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * @title 作品接口
  10. * @controller Collect
  11. * @group base
  12. */
  13. class Collect extends Base
  14. {
  15. public function initialize()
  16. {
  17. parent::check_login();
  18. }
  19. /**
  20. * @title 获取作品列表
  21. * @desc 获取作品列表
  22. * @author qc
  23. * @url /api/Collect/getCollectList
  24. * @method GET
  25. * @header name:Authorization require:1 desc:Token
  26. * @param name:page type:int default:1 desc:页数
  27. * @param name:page_num type:int : default:20 desc:每页数
  28. * @return name:goods_id type:int default:-- desc:商品id
  29. * @return name:goods_cover type:string default:-- desc:商品封面
  30. * @return name:goods_name type:string default:-- desc:商品名称
  31. * @return name:create_at type:string default:-- desc:获得时间
  32. * @return name:serial type:string default:-- desc:版本号
  33. * @return name:source type:int default:0 desc:来源(来源1购买2兑换3邀请获得4赠送)
  34. * @return name:hash_str type:string default:-- desc:哈希字符串
  35. * @return name:framer type:string default:-- desc:创作者
  36. * @return name:sc_name type:string default:-- desc:收藏者
  37. * @return name:issuer type:string default:-- desc:发行方
  38. * @return name:coll_intro type:string default:-- desc:作品介绍
  39. * @return name:total_num type:int default:-- desc:收藏数量
  40. * @return name:adv_type type:int default:-- desc:广告类型(1图片,2mp3,3视频)
  41. * @return name:goods_price type:float default:-- desc:价格
  42. * @return name:g_type type:int default:-- desc:(1售卖,2兑换,3邀请)
  43. */
  44. public function getCollectList(){
  45. $sel_where = [];
  46. $sel_where[] = ['c.status','=',1];
  47. $sel_where[] = ['c.user_id','=',$this->uid];
  48. $total_num = Db::table('goods_collect')->alias('c')->where($sel_where)->count();
  49. $list = Db::table('goods_collect')->alias('c')
  50. ->field('c.*,g.adv_type,g.goods_price,g.type as g_type')
  51. ->join('store_goods g','g.id = c.goods_id','LEFT')
  52. ->where($sel_where)
  53. ->order('c.id desc')
  54. ->limit($this->off_set,$this->page_num)
  55. ->select();
  56. $this->success('ok',['list'=>$list,'total_num'=>$total_num? $total_num:0]);
  57. }
  58. /**
  59. * @title 获取作品详情
  60. * @desc 获取作品详情
  61. * @author qc
  62. * @url /api/Collect/getCollectInfo
  63. * @method GET
  64. * @header name:Authorization require:1 desc:Token
  65. * @param name:cid type:int default:-- desc:收藏记录id
  66. * @return name:goods_cover type:string default:-- desc:商品封面
  67. * @return name:goods_name type:string default:-- desc:商品名称
  68. * @return name:create_at type:string default:-- desc:获得时间
  69. * @return name:serial type:string default:-- desc:版本号
  70. * @return name:source type:int default:0 desc:来源(来源1购买2兑换3邀请获得4赠送)
  71. * @return name:hash_str type:string default:-- desc:哈希字符串
  72. * @return name:buy_notice type:string default:-- desc:购买须知
  73. * @return name:is_gift type:string default:-- desc:是否允许赠送
  74. * @return name:gift_day type:string default:-- desc:天数限制
  75. * @return name:framer type:string default:-- desc:创作者(收藏详情)
  76. * @return name:sc_name type:string default:-- desc:收藏者
  77. * @return name:issuer type:string default:-- desc:发行方
  78. * @return name:coll_intro type:string default:-- desc:作品介绍
  79. * @return name:crystal type:string default:-- desc:转赠需要元石数量
  80. */
  81. public function getCollectInfo(){
  82. $cid = input('get.cid',0);
  83. $detail = Db::table('goods_collect')->alias('c')
  84. ->field('c.*,g.buy_notice,g.is_gift,g.gift_day,g.crystal,g.type as goods_type,g.adv_type,g.adv_cover,g.mp3_url,g.audio_line,g.audio_url')
  85. ->join('store_goods g','c.goods_id = g.id','LEFT')
  86. ->where('c.id',$cid)
  87. ->find();
  88. $detail['part_cal'] = 'ED25519';
  89. $detail['part_link'] = 'JD China';
  90. $detail['goods_detail_switch'] = intval(sysconf('goods_detail_switch'));
  91. $this->success('ok',['detail'=>$detail]);
  92. }
  93. /**
  94. * @title 获取作品证书
  95. * @desc 获取作品证书
  96. * @author qc
  97. * @url /api/Collect/getCollectCredential
  98. * @method GET
  99. * @header name:Authorization require:1 desc:Token
  100. * @param name:cid type:int default:-- desc:收藏记录id
  101. * @return name:goods_cover type:string default:-- desc:商品封面
  102. * @return name:goods_name type:string default:-- desc:商品名称
  103. * @return name:create_at type:string default:-- desc:获得时间
  104. * @return name:serial type:string default:-- desc:版本号
  105. * @return name:source type:int default:0 desc:来源(来源1购买2兑换3邀请获得4赠送)
  106. * @return name:hash_str type:string default:-- desc:哈希字符串
  107. * @return name:buy_notice type:string default:-- desc:购买须知
  108. * @return name:is_gift type:string default:-- desc:是否允许赠送
  109. * @return name:gift_day type:string default:-- desc:天数限制
  110. * @return name:framer type:string default:-- desc:创作者(收藏详情)
  111. * @return name:sc_name type:string default:-- desc:收藏者
  112. * @return name:issuer type:string default:-- desc:发行方
  113. * @return name:coll_intro type:string default:-- desc:作品介绍
  114. * @return name:crystal type:string default:-- desc:转赠需要元石数量
  115. * @return name:list type:array default:-- desc:溯源列表
  116. * @return name:list.sc_name type:string default:-- desc:收藏者
  117. * @return name:list.create_at type:string default:-- desc:时间
  118. * @return name:list.hash_str type:string default:-- desc:哈希值
  119. */
  120. public function getCollectCredential()
  121. {
  122. $cid = input('get.cid',0);
  123. $detail = Db::table('goods_collect')->alias('c')
  124. ->field('c.framer,c.source,c.original_id,c.hash_str,c.sc_name,c.goods_name,c.create_at,c.coll_intro,c.issuer,g.buy_notice,g.is_gift,g.gift_day,g.crystal,g.type as goods_type')
  125. ->join('store_goods g','c.goods_id = g.id','LEFT')
  126. ->where('c.id',$cid)
  127. ->find();
  128. $detail['part_cal'] = 'ED25519';
  129. $detail['part_link'] = 'JD China';
  130. $list = [];
  131. if($detail['source'] == 4 && $detail['original_id'] > 0) {
  132. $count_num = Db::table('goods_collect')->where('original_id|id',$detail['original_id'])->count();
  133. if($count_num > 5){
  134. $list = Db::table('goods_collect')->where('original_id',$detail['original_id'])
  135. ->field('id,sc_name,create_at,hash_str')->order('id desc')->limit(0,5)->select();
  136. }else{
  137. $list = Db::table('goods_collect')->where('original_id|id',$detail['original_id'])
  138. ->field('id,sc_name,create_at,hash_str')->order('id asc')->limit(0,5)->select();
  139. }
  140. array_multisort(array_column($list,'id'), SORT_ASC, $list);
  141. }
  142. //$detail['special_name'] = ''; //区块链查证信息概览
  143. // $detail['coll_name'] = ''; //收藏者
  144. $detail['special_name'] = '区块链查证信息概览';//
  145. $detail['coll_name'] = '收藏者';//
  146. $detail['goods_detail_switch'] = intval(sysconf('goods_detail_switch'));
  147. $this->success('ok',['detail'=>$detail,'list'=>$list]);
  148. }
  149. /**
  150. * @title 领取邀请商品
  151. * @desc 领取邀请商品
  152. * @author qc
  153. * @url /api/Collect/getInviteGoods
  154. * @method POST
  155. * @header name:Authorization require:1 desc:Token
  156. * @param name:goods_id type:int : default:0 desc:商品id
  157. */
  158. public function getInviteGoods()
  159. {
  160. $goods_id = input('post.goods_id',0);
  161. Db::startTrans();
  162. $is_commit = true;$msg= '领取成功!';
  163. try {
  164. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  165. $user_name = $user_info['name'];
  166. $goods_info = Db::table('store_goods')->where(['id'=>$goods_id,'status'=>1,'is_deleted'=>0])->find();
  167. if(empty($goods_info)) throw new Exception('商品已下架,嘤嘤嘤~~~');
  168. if(strtotime($goods_info['sell_time']) > time()) throw new Exception('商品还未开始领取!请耐心等待');
  169. if($goods_info['stock'] <= 0) throw new Exception('晚来一步,商品已被领完~');
  170. if($user_info['phone'] != 15853857773){
  171. if(!$user_info['is_auth']) throw new Exception('请实名认证后领取');
  172. if( $goods_info['invite_num'] == 0) {
  173. $is_get = Db::table('goods_collect')->where(['user_id'=>$this->uid,'goods_id'=>$goods_id,'source'=>3])->count();
  174. if(!empty($is_get)) throw new Exception('已经领取过,无法重复领取!');
  175. }else{
  176. $limit_num = $goods_info['invite_num'];// 需要邀请多少会员才能领取商品
  177. $invite_info = Db::table('invite_info')->where(['pid'=>$this->uid,'status'=>1])->limit(0,$limit_num)->select();
  178. if(count($invite_info) < $limit_num)throw new Exception('再邀请'.bcsub($limit_num ,count($invite_info)).'个会员就可以领取~');
  179. // 修改邀请记录
  180. Db::table('invite_info')->where(['id'=>array_column($invite_info,'id')])->update(['status'=>2]);
  181. }
  182. }
  183. $collect_num = Db::table('goods_collect')
  184. ->where(['goods_id'=>$goods_info['id']])
  185. ->where('status','in',[1,3])->count();
  186. $serial = intval($collect_num) + 1;
  187. // 作品详情
  188. $collect_info = [
  189. 'user_id' => $this->uid,
  190. 'goods_id' => $goods_info['id'],
  191. 'goods_cover'=> $goods_info['cover'],
  192. 'goods_name'=> $goods_info['name'],
  193. 'serial' => $serial,
  194. 'source' => 3,
  195. 'from_id' => 0,
  196. 'sc_name' =>$user_name,//收藏者
  197. 'issuer' =>$goods_info['issuer'],//发行方
  198. 'framer' =>$goods_info['framer'],//framer
  199. 'coll_intro'=>$goods_info['coll_intro'],//作品介绍
  200. ];
  201. $collect_info = array_merge($collect_info,get_goods_hash($this->uid,$goods_info['id'],$serial));
  202. Db::table('goods_collect')->insert($collect_info);
  203. // 扣减库存
  204. Db::table('store_goods')->where('id',$goods_id)->setDec('stock',1);
  205. goods_sell_info($goods_info,$this->uid);
  206. Db::commit();
  207. }catch (\Exception $e){
  208. Db::rollback();
  209. $is_commit =false;
  210. $msg = $e->getMessage();
  211. }
  212. $is_commit ? $this->success($msg):$this->error($msg);
  213. }
  214. /**
  215. * @title 兑换码兑换商品
  216. * @desc 兑换码兑换商品
  217. * @author qc
  218. * @url /api/Collect/getGoodsByCode
  219. * @method POST
  220. * @header name:Authorization require:1 desc:Token
  221. * @param name:code type:int : default:0 desc:兑换码
  222. */
  223. public function getGoodsByCode()
  224. {
  225. $code = input('post.code',0);
  226. Db::startTrans();
  227. $is_commit = true;$msg= '兑换成功!';
  228. try {
  229. $user_info = Db::name('store_member')->where('id',$this->uid)->find();
  230. $user_name = $user_info['name'];
  231. if(!$user_info['is_auth']) throw new Exception('请实名认证后兑换作品!');
  232. $code_info = Db::table('goods_code')->where(['code'=>$code,'status'=>1])->find();
  233. if(empty($code_info)) throw new Exception('兑换码有误!');
  234. $goods_id = $code_info['goods_id'];
  235. $goods_info = Db::table('store_goods')->where(['id'=>$goods_id,'status'=>1,'type'=>2,'is_deleted'=>0])->find();
  236. if(empty($goods_info)) throw new Exception('商品已下架,嘤嘤嘤~~~');
  237. if(strtotime($goods_info['sell_time']) > time()) throw new Exception('商品还未开始兑换!请耐心等待');
  238. if($goods_info['stock'] <= 0) throw new Exception('晚来一步,商品已兑换完~');
  239. $collect_num = Db::table('goods_collect')->where(['goods_id'=>$goods_info['id']])
  240. ->where('status','in',[1,3])->count();
  241. $serial = intval($collect_num) + 1;
  242. // 作品详情
  243. $collect_info = [
  244. 'user_id' => $this->uid,
  245. 'goods_id' => $goods_info['id'],
  246. 'goods_cover'=> $goods_info['cover'],
  247. 'goods_name'=> $goods_info['name'],
  248. 'serial' => $serial,
  249. 'source' => 2,
  250. 'from_id' => $code_info['id'],
  251. 'sc_name' =>$user_name,//收藏者
  252. 'issuer' =>$goods_info['issuer'],//发行方
  253. 'framer' =>$goods_info['framer'],//framer
  254. 'coll_intro'=>$goods_info['coll_intro'],//作品介绍
  255. ];
  256. $collect_info = array_merge($collect_info,get_goods_hash($this->uid,$goods_info['id'],$serial));
  257. Db::table('goods_collect')->insert($collect_info);
  258. // 扣减库存
  259. Db::table('store_goods')->where('id',$goods_id)->setDec('stock',1);
  260. //修改兑换码状态
  261. Db::table('goods_code')->where('id',$code_info['id'])->update(['user_id'=>$this->uid,'status'=>2,'use_time'=>date("Y-m-d H:i:s")]);
  262. goods_sell_info($goods_info,$this->uid,$code_info['id']);
  263. Db::commit();
  264. }catch (\Exception $e){
  265. Db::rollback();
  266. $is_commit =false;
  267. $msg = $e->getMessage();
  268. }
  269. $is_commit ? $this->success($msg):$this->error($msg);
  270. }
  271. /**
  272. * @title 作品转赠
  273. * @desc 作品转赠
  274. * @author qc
  275. * @url /api/Collect/goodsGiveAway
  276. * @method POST
  277. * @header name:Authorization require:1 desc:Token
  278. * @param name:cid type:int : default:1 desc:收藏记录id
  279. * @param name:to_user type:int : default:1 desc:会员手机号
  280. */
  281. public function goodsGiveAway()
  282. {
  283. $cid = input('post.cid',0);
  284. $to_user = input('post.to_user',0);
  285. Db::startTrans();
  286. $is_commit = true;$msg= '恭喜!转赠成功!';
  287. try {
  288. $user_crystal = Db::table('store_member')->where('id',$this->uid)->value('crystal');
  289. $collect_info = Db::table('goods_collect')->alias('c')
  290. ->field('c.*,g.goods_auth,g.buy_notice,g.is_gift,g.gift_day,g.crystal,g.type')
  291. ->join('store_goods g','c.goods_id = g.id','LEFT')
  292. ->where('c.id',$cid)
  293. ->where('c.status',1)
  294. ->find();
  295. if(empty($collect_info)) throw new Exception('作品信息有误!');
  296. if($collect_info['is_gift'] == 0) throw new Exception('作品禁止转赠!');
  297. $gift_time = strtotime($collect_info['create_at']) + 86400 * $collect_info['gift_day'];
  298. if($gift_time > time()) throw new Exception('您的作品在收藏满'.$collect_info['gift_day'].'天后可以转赠');
  299. $send_user = Db::table('store_member')->where('status',1)
  300. ->where('phone',$to_user)->where('is_deleted',0)->find();
  301. if($send_user['id'] == $this->uid) throw new Exception('无法转赠给自己!');
  302. if(empty($send_user)) throw new Exception('赠送会员不存在!');
  303. if(!$send_user['is_auth']) throw new Exception('赠送会员未实名认证!');
  304. if($collect_info['crystal'] > $user_crystal) throw new Exception('元石不足,无法转赠!');
  305. $get_hash = get_goods_hash($send_user['id'],$collect_info['goods_id'],$collect_info['serial']);
  306. $send_info = [
  307. 'user_id' => $send_user['id'],
  308. 'goods_id' => $collect_info['goods_id'],
  309. 'goods_cover' => $collect_info['goods_cover'],
  310. 'goods_name' => $collect_info['goods_name'],
  311. 'serial' => $collect_info['serial'],
  312. 'from_id' => $collect_info['id'],
  313. 'hash_str' => $get_hash['hash_str'],
  314. 'issuer' => $collect_info['issuer'],
  315. 'coll_intro' => $collect_info['coll_intro'],
  316. 'framer' => $collect_info['framer'],
  317. 'sc_name' => $send_user['name'],
  318. 'original_id' => $collect_info['original_id'] ? $collect_info['original_id'] :$collect_info['id'] ,
  319. 'source' => 4 ,
  320. ];
  321. // 转赠收藏记录
  322. Db::table('goods_collect')->insert($send_info);
  323. $new_cid = Db::getLastInsID();
  324. // 更改收藏记录
  325. Db::table('goods_collect')->where('id',$collect_info['id'])->update(['status'=>2]);
  326. // 收藏日志
  327. Db::table('goods_present_info')->insert([
  328. 'goods_id' => $collect_info['goods_id'],
  329. 'goods_type' => $collect_info['type'],
  330. 'from_user' => $this->uid,
  331. 'to_user' => $send_user['id'],
  332. 'from_cid' => $collect_info['id'],
  333. 'to_cid' => $new_cid,
  334. 'original_id' => $send_info['original_id'],
  335. ]);
  336. // 扣除转让费用
  337. if($collect_info['crystal'] > 0){
  338. Db::table('store_member')->where('id',$this->uid)->update(['crystal'=>bcsub($user_crystal,$collect_info['crystal'],2)]);
  339. crystal_log($this->uid,$collect_info['crystal']*-1,'转赠作品',2,$collect_info['id']);
  340. }
  341. Db::commit();
  342. }catch (\Exception $e){
  343. Db::rollback();
  344. $is_commit = false;
  345. $msg =$e->getMessage();
  346. }
  347. $is_commit ? $this->success($msg) : $this->error($msg);
  348. }
  349. /**
  350. * @title 获取转赠记录
  351. * @desc 获取转赠记录
  352. * @author qc
  353. * @url /api/Collect/getGiftLog
  354. * @method GET
  355. * @header name:Authorization require:1 desc:Token
  356. * @param name:page type:int default:1 desc:页数
  357. * @param name:page_num type:int : default:20 desc:每页数
  358. * @return name:goods_id type:int default:-- desc:商品id
  359. * @return name:headimg type:string default:-- desc:头像
  360. * @return name:user_name type:string default:-- desc:昵称
  361. * @return name:goods_cover type:string default:-- desc:封面
  362. * @return name:goods_name type:string default:-- desc:商品
  363. * @return name:adv_type type:int default:-- desc:广告类型(1图片,2mp3,3视频)
  364. * @return name:goods_price type:float default:-- desc:价格
  365. * @return name:g_type type:int default:-- desc:(1售卖,2兑换,3邀请)
  366. */
  367. public function getGiftLog()
  368. {
  369. $sel_where = [];
  370. $sel_where[] = ['l.from_user','=',$this->uid];
  371. $list = Db::table('goods_present_info')->alias('l')
  372. ->field('l.id,l.goods_id,l.create_at,m.name as user_name,m.headimg,g.adv_type,g.goods_price,g.type as g_type,c.goods_cover,c.goods_name,c.serial')
  373. ->join('store_member m','m.id = l.to_user','LEFT')
  374. ->join('goods_collect c','c.id = l.from_cid','LEFT')
  375. ->join('store_goods g','g.id = l.goods_id','LEFT')
  376. ->where($sel_where)
  377. ->order('l.id desc')
  378. ->limit($this->off_set,$this->page_num)
  379. ->select();
  380. $this->success('ok',['list'=>$list]);
  381. }
  382. }