123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
- namespace app\api\controller;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use think\Db;
- use think\Exception;
- /**
- * @title 作品接口
- * @controller Collect
- * @group base
- */
- class Collect extends Base
- {
- public function initialize()
- {
- parent::check_login();
- }
- /**
- * @title 获取作品列表
- * @desc 获取作品列表
- * @author qc
- * @url /api/Collect/getCollectList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int : default:20 desc:每页数
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_cover type:string default:-- desc:商品封面
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:create_at type:string default:-- desc:获得时间
- * @return name:serial type:string default:-- desc:版本号
- * @return name:source type:int default:0 desc:来源(来源1购买2兑换3邀请获得4赠送)
- * @return name:hash_str type:string default:-- desc:哈希字符串
- * @return name:framer type:string default:-- desc:创作者
- * @return name:sc_name type:string default:-- desc:收藏者
- * @return name:issuer type:string default:-- desc:发行方
- * @return name:coll_intro type:string default:-- desc:作品介绍
- * @return name:total_num type:int default:-- desc:收藏数量
- * @return name:adv_type type:int default:-- desc:广告类型(1图片,2mp3,3视频)
- * @return name:goods_price type:float default:-- desc:价格
- * @return name:g_type type:int default:-- desc:(1售卖,2兑换,3邀请)
- */
- public function getCollectList(){
- $sel_where = [];
- $sel_where[] = ['c.status','=',1];
- $sel_where[] = ['c.user_id','=',$this->uid];
- $total_num = Db::table('goods_collect')->alias('c')->where($sel_where)->count();
- $list = Db::table('goods_collect')->alias('c')
- ->field('c.*,g.adv_type,g.goods_price,g.type as g_type')
- ->join('store_goods g','g.id = c.goods_id','LEFT')
- ->where($sel_where)
- ->order('c.id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('ok',['list'=>$list,'total_num'=>$total_num? $total_num:0]);
- }
- /**
- * @title 获取作品详情
- * @desc 获取作品详情
- * @author qc
- * @url /api/Collect/getCollectInfo
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:cid type:int default:-- desc:收藏记录id
- * @return name:goods_cover type:string default:-- desc:商品封面
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:create_at type:string default:-- desc:获得时间
- * @return name:serial type:string default:-- desc:版本号
- * @return name:source type:int default:0 desc:来源(来源1购买2兑换3邀请获得4赠送)
- * @return name:hash_str type:string default:-- desc:哈希字符串
- * @return name:buy_notice type:string default:-- desc:购买须知
- * @return name:is_gift type:string default:-- desc:是否允许赠送
- * @return name:gift_day type:string default:-- desc:天数限制
- * @return name:framer type:string default:-- desc:创作者(收藏详情)
- * @return name:sc_name type:string default:-- desc:收藏者
- * @return name:issuer type:string default:-- desc:发行方
- * @return name:coll_intro type:string default:-- desc:作品介绍
- * @return name:crystal type:string default:-- desc:转赠需要元石数量
- */
- public function getCollectInfo(){
- $cid = input('get.cid',0);
- $detail = Db::table('goods_collect')->alias('c')
- ->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')
- ->join('store_goods g','c.goods_id = g.id','LEFT')
- ->where('c.id',$cid)
- ->find();
- $detail['part_cal'] = 'ED25519';
- $detail['part_link'] = 'JD China';
- $detail['goods_detail_switch'] = intval(sysconf('goods_detail_switch'));
- $this->success('ok',['detail'=>$detail]);
- }
- /**
- * @title 获取作品证书
- * @desc 获取作品证书
- * @author qc
- * @url /api/Collect/getCollectCredential
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:cid type:int default:-- desc:收藏记录id
- * @return name:goods_cover type:string default:-- desc:商品封面
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:create_at type:string default:-- desc:获得时间
- * @return name:serial type:string default:-- desc:版本号
- * @return name:source type:int default:0 desc:来源(来源1购买2兑换3邀请获得4赠送)
- * @return name:hash_str type:string default:-- desc:哈希字符串
- * @return name:buy_notice type:string default:-- desc:购买须知
- * @return name:is_gift type:string default:-- desc:是否允许赠送
- * @return name:gift_day type:string default:-- desc:天数限制
- * @return name:framer type:string default:-- desc:创作者(收藏详情)
- * @return name:sc_name type:string default:-- desc:收藏者
- * @return name:issuer type:string default:-- desc:发行方
- * @return name:coll_intro type:string default:-- desc:作品介绍
- * @return name:crystal type:string default:-- desc:转赠需要元石数量
- * @return name:list type:array default:-- desc:溯源列表
- * @return name:list.sc_name type:string default:-- desc:收藏者
- * @return name:list.create_at type:string default:-- desc:时间
- * @return name:list.hash_str type:string default:-- desc:哈希值
- */
- public function getCollectCredential()
- {
- $cid = input('get.cid',0);
- $detail = Db::table('goods_collect')->alias('c')
- ->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')
- ->join('store_goods g','c.goods_id = g.id','LEFT')
- ->where('c.id',$cid)
- ->find();
- $detail['part_cal'] = 'ED25519';
- $detail['part_link'] = 'JD China';
- $list = [];
- if($detail['source'] == 4 && $detail['original_id'] > 0) {
- $count_num = Db::table('goods_collect')->where('original_id|id',$detail['original_id'])->count();
- if($count_num > 5){
- $list = Db::table('goods_collect')->where('original_id',$detail['original_id'])
- ->field('id,sc_name,create_at,hash_str')->order('id desc')->limit(0,5)->select();
- }else{
- $list = Db::table('goods_collect')->where('original_id|id',$detail['original_id'])
- ->field('id,sc_name,create_at,hash_str')->order('id asc')->limit(0,5)->select();
- }
- array_multisort(array_column($list,'id'), SORT_ASC, $list);
- }
- //$detail['special_name'] = ''; //区块链查证信息概览
- // $detail['coll_name'] = ''; //收藏者
- $detail['special_name'] = '区块链查证信息概览';//
- $detail['coll_name'] = '收藏者';//
- $detail['goods_detail_switch'] = intval(sysconf('goods_detail_switch'));
- $this->success('ok',['detail'=>$detail,'list'=>$list]);
- }
- /**
- * @title 领取邀请商品
- * @desc 领取邀请商品
- * @author qc
- * @url /api/Collect/getInviteGoods
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int : default:0 desc:商品id
- */
- public function getInviteGoods()
- {
- $goods_id = input('post.goods_id',0);
- Db::startTrans();
- $is_commit = true;$msg= '领取成功!';
- try {
- $user_info = Db::table('store_member')->where('id',$this->uid)->find();
- $user_name = $user_info['name'];
- $goods_info = Db::table('store_goods')->where(['id'=>$goods_id,'status'=>1,'is_deleted'=>0])->find();
- if(empty($goods_info)) throw new Exception('商品已下架,嘤嘤嘤~~~');
- if(strtotime($goods_info['sell_time']) > time()) throw new Exception('商品还未开始领取!请耐心等待');
- if($goods_info['stock'] <= 0) throw new Exception('晚来一步,商品已被领完~');
- if($user_info['phone'] != 15853857773){
- if(!$user_info['is_auth']) throw new Exception('请实名认证后领取');
- if( $goods_info['invite_num'] == 0) {
- $is_get = Db::table('goods_collect')->where(['user_id'=>$this->uid,'goods_id'=>$goods_id,'source'=>3])->count();
- if(!empty($is_get)) throw new Exception('已经领取过,无法重复领取!');
- }else{
- $limit_num = $goods_info['invite_num'];// 需要邀请多少会员才能领取商品
- $invite_info = Db::table('invite_info')->where(['pid'=>$this->uid,'status'=>1])->limit(0,$limit_num)->select();
- if(count($invite_info) < $limit_num)throw new Exception('再邀请'.bcsub($limit_num ,count($invite_info)).'个会员就可以领取~');
- // 修改邀请记录
- Db::table('invite_info')->where(['id'=>array_column($invite_info,'id')])->update(['status'=>2]);
- }
- }
- $collect_num = Db::table('goods_collect')
- ->where(['goods_id'=>$goods_info['id']])
- ->where('status','in',[1,3])->count();
- $serial = intval($collect_num) + 1;
- // 作品详情
- $collect_info = [
- 'user_id' => $this->uid,
- 'goods_id' => $goods_info['id'],
- 'goods_cover'=> $goods_info['cover'],
- 'goods_name'=> $goods_info['name'],
- 'serial' => $serial,
- 'source' => 3,
- 'from_id' => 0,
- 'sc_name' =>$user_name,//收藏者
- 'issuer' =>$goods_info['issuer'],//发行方
- 'framer' =>$goods_info['framer'],//framer
- 'coll_intro'=>$goods_info['coll_intro'],//作品介绍
- ];
- $collect_info = array_merge($collect_info,get_goods_hash($this->uid,$goods_info['id'],$serial));
- Db::table('goods_collect')->insert($collect_info);
- // 扣减库存
- Db::table('store_goods')->where('id',$goods_id)->setDec('stock',1);
- goods_sell_info($goods_info,$this->uid);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $is_commit =false;
- $msg = $e->getMessage();
- }
- $is_commit ? $this->success($msg):$this->error($msg);
- }
- /**
- * @title 兑换码兑换商品
- * @desc 兑换码兑换商品
- * @author qc
- * @url /api/Collect/getGoodsByCode
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:code type:int : default:0 desc:兑换码
- */
- public function getGoodsByCode()
- {
- $code = input('post.code',0);
- Db::startTrans();
- $is_commit = true;$msg= '兑换成功!';
- try {
- $user_info = Db::name('store_member')->where('id',$this->uid)->find();
- $user_name = $user_info['name'];
- if(!$user_info['is_auth']) throw new Exception('请实名认证后兑换作品!');
- $code_info = Db::table('goods_code')->where(['code'=>$code,'status'=>1])->find();
- if(empty($code_info)) throw new Exception('兑换码有误!');
- $goods_id = $code_info['goods_id'];
- $goods_info = Db::table('store_goods')->where(['id'=>$goods_id,'status'=>1,'type'=>2,'is_deleted'=>0])->find();
- if(empty($goods_info)) throw new Exception('商品已下架,嘤嘤嘤~~~');
- if(strtotime($goods_info['sell_time']) > time()) throw new Exception('商品还未开始兑换!请耐心等待');
- if($goods_info['stock'] <= 0) throw new Exception('晚来一步,商品已兑换完~');
- $collect_num = Db::table('goods_collect')->where(['goods_id'=>$goods_info['id']])
- ->where('status','in',[1,3])->count();
- $serial = intval($collect_num) + 1;
- // 作品详情
- $collect_info = [
- 'user_id' => $this->uid,
- 'goods_id' => $goods_info['id'],
- 'goods_cover'=> $goods_info['cover'],
- 'goods_name'=> $goods_info['name'],
- 'serial' => $serial,
- 'source' => 2,
- 'from_id' => $code_info['id'],
- 'sc_name' =>$user_name,//收藏者
- 'issuer' =>$goods_info['issuer'],//发行方
- 'framer' =>$goods_info['framer'],//framer
- 'coll_intro'=>$goods_info['coll_intro'],//作品介绍
- ];
- $collect_info = array_merge($collect_info,get_goods_hash($this->uid,$goods_info['id'],$serial));
- Db::table('goods_collect')->insert($collect_info);
- // 扣减库存
- Db::table('store_goods')->where('id',$goods_id)->setDec('stock',1);
- //修改兑换码状态
- 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")]);
- goods_sell_info($goods_info,$this->uid,$code_info['id']);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $is_commit =false;
- $msg = $e->getMessage();
- }
- $is_commit ? $this->success($msg):$this->error($msg);
- }
- /**
- * @title 作品转赠
- * @desc 作品转赠
- * @author qc
- * @url /api/Collect/goodsGiveAway
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:cid type:int : default:1 desc:收藏记录id
- * @param name:to_user type:int : default:1 desc:会员手机号
- */
- public function goodsGiveAway()
- {
- $cid = input('post.cid',0);
- $to_user = input('post.to_user',0);
- Db::startTrans();
- $is_commit = true;$msg= '恭喜!转赠成功!';
- try {
- $user_crystal = Db::table('store_member')->where('id',$this->uid)->value('crystal');
- $collect_info = Db::table('goods_collect')->alias('c')
- ->field('c.*,g.goods_auth,g.buy_notice,g.is_gift,g.gift_day,g.crystal,g.type')
- ->join('store_goods g','c.goods_id = g.id','LEFT')
- ->where('c.id',$cid)
- ->where('c.status',1)
- ->find();
- if(empty($collect_info)) throw new Exception('作品信息有误!');
- if($collect_info['is_gift'] == 0) throw new Exception('作品禁止转赠!');
- $gift_time = strtotime($collect_info['create_at']) + 86400 * $collect_info['gift_day'];
- if($gift_time > time()) throw new Exception('您的作品在收藏满'.$collect_info['gift_day'].'天后可以转赠');
- $send_user = Db::table('store_member')->where('status',1)
- ->where('phone',$to_user)->where('is_deleted',0)->find();
- if($send_user['id'] == $this->uid) throw new Exception('无法转赠给自己!');
- if(empty($send_user)) throw new Exception('赠送会员不存在!');
- if(!$send_user['is_auth']) throw new Exception('赠送会员未实名认证!');
- if($collect_info['crystal'] > $user_crystal) throw new Exception('元石不足,无法转赠!');
- $get_hash = get_goods_hash($send_user['id'],$collect_info['goods_id'],$collect_info['serial']);
- $send_info = [
- 'user_id' => $send_user['id'],
- 'goods_id' => $collect_info['goods_id'],
- 'goods_cover' => $collect_info['goods_cover'],
- 'goods_name' => $collect_info['goods_name'],
- 'serial' => $collect_info['serial'],
- 'from_id' => $collect_info['id'],
- 'hash_str' => $get_hash['hash_str'],
- 'issuer' => $collect_info['issuer'],
- 'coll_intro' => $collect_info['coll_intro'],
- 'framer' => $collect_info['framer'],
- 'sc_name' => $send_user['name'],
- 'original_id' => $collect_info['original_id'] ? $collect_info['original_id'] :$collect_info['id'] ,
- 'source' => 4 ,
- ];
- // 转赠收藏记录
- Db::table('goods_collect')->insert($send_info);
- $new_cid = Db::getLastInsID();
- // 更改收藏记录
- Db::table('goods_collect')->where('id',$collect_info['id'])->update(['status'=>2]);
- // 收藏日志
- Db::table('goods_present_info')->insert([
- 'goods_id' => $collect_info['goods_id'],
- 'goods_type' => $collect_info['type'],
- 'from_user' => $this->uid,
- 'to_user' => $send_user['id'],
- 'from_cid' => $collect_info['id'],
- 'to_cid' => $new_cid,
- 'original_id' => $send_info['original_id'],
- ]);
- // 扣除转让费用
- if($collect_info['crystal'] > 0){
- Db::table('store_member')->where('id',$this->uid)->update(['crystal'=>bcsub($user_crystal,$collect_info['crystal'],2)]);
- crystal_log($this->uid,$collect_info['crystal']*-1,'转赠作品',2,$collect_info['id']);
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $is_commit = false;
- $msg =$e->getMessage();
- }
- $is_commit ? $this->success($msg) : $this->error($msg);
- }
- /**
- * @title 获取转赠记录
- * @desc 获取转赠记录
- * @author qc
- * @url /api/Collect/getGiftLog
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int : default:20 desc:每页数
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:headimg type:string default:-- desc:头像
- * @return name:user_name type:string default:-- desc:昵称
- * @return name:goods_cover type:string default:-- desc:封面
- * @return name:goods_name type:string default:-- desc:商品
- * @return name:adv_type type:int default:-- desc:广告类型(1图片,2mp3,3视频)
- * @return name:goods_price type:float default:-- desc:价格
- * @return name:g_type type:int default:-- desc:(1售卖,2兑换,3邀请)
- */
- public function getGiftLog()
- {
- $sel_where = [];
- $sel_where[] = ['l.from_user','=',$this->uid];
- $list = Db::table('goods_present_info')->alias('l')
- ->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')
- ->join('store_member m','m.id = l.to_user','LEFT')
- ->join('goods_collect c','c.id = l.from_cid','LEFT')
- ->join('store_goods g','g.id = l.goods_id','LEFT')
- ->where($sel_where)
- ->order('l.id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('ok',['list'=>$list]);
- }
- }
|