123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 礼物接口
- */
- class Gift extends Api
- {
- protected $noNeedLogin = ['getGiftList','getGiftType'];
- protected $noNeedRight = '*';
- public $giftModel;
- public $gifttypeModel;
- public function _initialize()
- {
- parent::_initialize();
- $this->giftModel = new \app\common\model\Gift();
- $this->gifttypeModel = new \app\common\model\GiftType();
- }
- /**
- * 获取礼物列表
- */
- public function getGiftList() {
- $type = $this->request->request("type");
- // 获取基本信息
- $where = [];
- $type != '' && $where["type"] = $type;
- $giftList = Db::name('gift')->where($where)->order("value","asc")->select();
- $giftList = list_domain_image($giftList,['image','special']);
- $this->success("获取成功!",$giftList);
- }
- /**
- * 获取礼物类型
- */
- public function getGiftType() {
- // 获取基本信息
- $where = [];
- $where["is_show"] = 1;
- $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
- $this->success("获取成功!",$giftList);
- }
- /**
- * 获取我的背包礼物
- */
- public function getMyBackGift() {
- $userid = $this->auth->id;
- $page = $this->request->request('page',1); // 分页
- $pageNum = $this->request->request('pageNum',10); // 分页
- // 分页搜索构建
- $pageStart = ($page-1)*$pageNum;
- $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
- ->where(["user_id"=>$userid,"is_use"=>0])
- ->limit($pageStart,$pageNum)
- ->order('value', 'asc')
- ->group("name")
- ->select();
- $this->success("获取成功!",$list);
- }
- /**
- * 获取我的礼物墙
- */
- public function getMyGiftWall() {
- $user_id = $this->request->request("user_id", 0);
- $userid = $user_id ? $user_id : $this->auth->id;
- $list = \app\common\model\GiftUserParty::alias('a')->join("hx_gift g", "g.id = a.gift_id", "inner")->field("gift_id,g.name,g.image,sum(number) as number")
- ->where(["user_to_id" => $userid, 'g.type' => ['<>', 6]])
- ->group("gift_id")
- ->order('g.value desc')
- ->select();
- $this->success("获取成功!", $list);
- }
- //聊天送礼物
- public function givegift_typing() {
- // 接口防并发
- if (!$this->apiLimit(1, 1000)) {
- $this->error(__('Operation frequently'));
- }
- $user_id = input('user_id');// 赠送对象
- $gift_id = input('gift_id');// 礼物ID
- $number = input('number',1,'intval');//数量
- if (!$user_id || !$gift_id || $number < 1)
- {
- $this->error();
- }
- // 不可以赠送给自己
- if($this->auth->id == $user_id)
- {
- $this->error("不可以赠送给自己");
- }
- // 获取礼物信息
- $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
- if (!$giftinfo)
- {
- $this->error("请选择礼物");
- }
- $giftvalue = bcmul($giftinfo['value'],$number);
- //被赠送人信息
- $touserinfo = Db::name('user')->where('id',$user_id)->find();
- if (!$touserinfo)
- {
- $this->error("不存在的用户");
- }
- // 判断当前用户余额
- $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
- if($user_gold < $giftvalue)
- {
- $this->error("您的金币余额不足");
- }
- Db::startTrans();
- // 添加礼物赠送记录表
- $data = [
- 'user_id' => $this->auth->id,
- 'user_to_id' => $user_id,
- 'gift_id' => $giftinfo['id'],
- 'gift_name' => $giftinfo['name'],
- 'number' => $number,
- 'price' => $giftvalue,
- 'createtime' => time(),
- ];
- $log_id = Db::name('gift_user_typing')->insertGetId($data);
- if(!$log_id){
- Db::rollback();
- $this->error('赠送失败');
- }
- if($giftvalue > 0){
- // 扣除当前用户余额
- $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- // 添加赠送用户余额
- $money_to_gold = config('site.money_to_gold');
- $gift_plat_scale = config('site.gift_plat_scale');
- $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
- $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
- $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,54,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- }
- //tag任务赠送金币
- //搭讪奖励
- $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
- if($task_rs === false){
- Db::rollback();
- $this->error('完成任务赠送奖励失败');
- }
- Db::commit();
- $this->success('赠送成功');
- }
- }
|