Gift.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 礼物接口
  7. */
  8. class Gift extends Api
  9. {
  10. protected $noNeedLogin = ['getGiftList','getGiftType'];
  11. protected $noNeedRight = '*';
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->giftModel = new \app\common\model\Gift();
  16. $this->gifttypeModel = new \app\common\model\GiftType();
  17. }
  18. public function test2(){
  19. $user_id = $this->auth->id;
  20. $gift_id = input('gift_id');
  21. $pay_total = input('pay_total');
  22. Db::startTrans();
  23. $rs = $this->test($user_id,$gift_id,$pay_total);
  24. if($rs !== true){
  25. Db::rollback();
  26. }
  27. Db::commit();
  28. dump($rs);
  29. }
  30. //gift_id必须是爆币礼物才可以
  31. public function test($user_id,$gift_id,$pay_total){
  32. //奖项参数
  33. $conf_arr = Db::name('gift_baobi_config')->where('gift_id',$gift_id)->select();
  34. if(empty($conf_arr)){
  35. return true;
  36. }
  37. //用户今天的爆币情况,与消费情况,不用区分具体哪个爆币礼物
  38. $today_start = strtotime(date('Y-m-d'));
  39. $today_end = $today_start + 86399;
  40. $map = [
  41. 'user_id' => $user_id,
  42. 'createtime' => ['BETWEEN',[$today_start,$today_end]],
  43. ];
  44. $today_data = Db::name('gift_baobi_log')->field('IFNULL(sum(pay_total),0) as today_pay_total,IFNULL(sum(baobi_total),0) as today_baobi_total')->where($map)->find();
  45. //dump($today_data);
  46. if($today_data['today_baobi_total'] >= $today_data['today_pay_total']){
  47. //爆币比消费还高了,不用抽奖了,记录个日志,返回。这里的日志仅用来记录今日消费 sum(pay_total)
  48. $data = [];
  49. $data['user_id'] = $user_id;
  50. $data['gift_id'] = $gift_id;
  51. $data['baobi_id'] = 0;
  52. $data['beilv'] = 0;
  53. $data['rate'] = 0;
  54. $data['pay_total'] = $pay_total;
  55. $data['baobi_total'] = 0;
  56. $data['createtime'] = time();
  57. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  58. if(!$log_id){
  59. return false;
  60. }
  61. return true;
  62. }
  63. //概率新数组
  64. $gailv = [];
  65. $bei = 100; //小数变整数
  66. foreach ($conf_arr as $key=>$value)
  67. {
  68. $gailv[$value['id']] = $value['rate']*$bei;
  69. }
  70. //dump($gailv);
  71. //奖项新数组
  72. $conf_column = [];
  73. foreach ($conf_arr as $key=>$value)
  74. {
  75. $conf_column[$value['id']] = $value;
  76. }
  77. //dump($conf_column);
  78. //抽
  79. $rid = $this->getRand($gailv); //根据概率获取奖项id
  80. //dump($rid);
  81. //返回中奖结果
  82. $result = $conf_column[$rid];
  83. //爆币金额
  84. $baobi_total = bcdiv(bcmul($pay_total,$result['beilv'],0),100,0);
  85. //写入爆币记录,不论有没有钱。全都写入其实是为了方便查日志,比在money_log里找范围更小,而且可删除
  86. $data = [];
  87. $data['user_id'] = $user_id;
  88. $data['gift_id'] = $gift_id;
  89. $data['baobi_id'] = $result['id'];
  90. $data['beilv'] = $result['beilv'];
  91. $data['rate'] = $result['rate'];
  92. $data['pay_total'] = $pay_total;
  93. $data['baobi_total'] = $baobi_total;
  94. $data['createtime'] = time();
  95. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  96. if(!$log_id){
  97. return false;
  98. }
  99. //直接给用户钱
  100. if($baobi_total > 0){
  101. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$baobi_total,'+',0,'送礼物爆币'.$result['beilv'].'%',21,'jewel');
  102. if($rs_wallet['status'] === false){
  103. return false;
  104. }
  105. }
  106. //默认成功
  107. return true;
  108. }
  109. //概率获得算法
  110. private function getRand($proArr) {
  111. //概率数组的总概率精度
  112. $proSum = array_sum($proArr);
  113. $key = rand(1, $proSum);
  114. // echo $key;
  115. $result = 0;
  116. $now = 0;
  117. foreach ($proArr as $k=>$v)
  118. {
  119. $now = $now + $v;
  120. if($key<=$now)
  121. {
  122. $result = $k;
  123. break;
  124. }
  125. }
  126. unset ($proArr);
  127. return $result;
  128. }
  129. /**
  130. * 获取礼物列表
  131. */
  132. public function getGiftList() {
  133. $type = input('type',0);
  134. // 获取基本信息
  135. $where = ['is_show'=>1];
  136. if($type){
  137. $where['type'] = $type;
  138. }
  139. $giftList = Db::name('gift')->field('id,name,price,image,special')->where($where)->order("sort","asc")->select();
  140. $giftList = list_domain_image($giftList,['image','special']);
  141. $this->success("获取成功!",$giftList);
  142. }
  143. /**
  144. * 获取礼物类型
  145. */
  146. public function getGiftType() {
  147. // 获取基本信息
  148. $where = [];
  149. $where["is_show"] = 1;
  150. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  151. $this->success("获取成功!",$giftList);
  152. }
  153. /**
  154. * 获取我的背包礼物
  155. */
  156. public function getMyBackGift() {
  157. $userid = input("user_id", $this->auth->id);
  158. // 分页搜索构建
  159. $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
  160. ->where(["user_id"=>$userid,"is_use"=>0])
  161. ->group("name")
  162. ->select();
  163. $this->success("获取成功!",$list);
  164. }
  165. /**
  166. * 获取我的礼物墙
  167. */
  168. public function getMyGiftWall() {
  169. $user_id = input("user_id", 0);
  170. $userid = $user_id ? $user_id : $this->auth->id;
  171. $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")
  172. ->where(["user_to_id" => $userid,])
  173. ->group("gift_id")
  174. ->order('g.value desc')
  175. ->select();
  176. $this->success("获取成功!", $list);
  177. }
  178. /**
  179. * 获取我的礼物墙
  180. */
  181. public function getMyGiftWall_typing() {
  182. $user_id = input("user_id", 0);
  183. $userid = $user_id ? $user_id : $this->auth->id;
  184. $list = Db::name('gift_user_typing')->alias('log')
  185. ->join('gift', 'gift.id = log.gift_id', 'LEFT')->field('log.*,sum(number) as number,gift.name,gift.image,gift.special')
  186. ->where(['log.user_to_id' => $userid])
  187. ->group('log.gift_id')
  188. ->order('gift.price desc')
  189. ->select();
  190. $list = list_domain_image($list,['image','special']);
  191. $this->success("获取成功!", $list);
  192. }
  193. /**
  194. * 获取我的收送礼明细
  195. */
  196. public function my_gift_log(){
  197. $user_id = $this->auth->id;
  198. $type = input('type',1);
  199. $where = [];
  200. if($type == 1){
  201. $where['user_id'] = $user_id;//我送出
  202. $joinstr = 'gup.user_to_id = user.id';
  203. }else{
  204. $where['user_to_id'] = $user_id;//我收到
  205. $joinstr = 'gup.user_id = user.id';
  206. }
  207. $list = Db::name('gift_user_party')->alias('gup')
  208. ->join('user',$joinstr,'LEFT')->field('gup.*,user.nickname')
  209. ->where($where)->order('id desc')->autopage()->select();
  210. $list = list_domain_image($list,['gift_gif_image']);
  211. $rs = [];
  212. if(empty($list)){
  213. $this->success(1,$rs);
  214. }
  215. foreach($list as $key => $val){
  216. if($type == 1){
  217. $remark = '赠送'.$val['nickname'].','.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
  218. }else{
  219. $remark = $val['nickname'].'赠送,'.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
  220. }
  221. $rs[] = [
  222. 'id' => $val['id'],
  223. 'gift_image' => $val['gift_gif_image'],
  224. 'createtime' => $val['createtime'],
  225. 'remark' => $remark
  226. ];
  227. }
  228. $this->success(1,$rs);
  229. }
  230. //聊天送礼物
  231. public function givegift_typing() {
  232. // 接口防并发
  233. if (!$this->apiLimit(1, 1000)) {
  234. $this->error(__('Operation frequently'));
  235. }
  236. $user_id = input('user_id');// 赠送对象
  237. $gift_id = input('gift_id');// 礼物ID
  238. $number = input('number',1,'intval');//数量
  239. if (!$user_id || !$gift_id || $number < 1)
  240. {
  241. $this->error();
  242. }
  243. // 不可以赠送给自己
  244. if($this->auth->id == $user_id)
  245. {
  246. $this->error("不可以赠送给自己");
  247. }
  248. // 获取礼物信息
  249. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  250. if (!$giftinfo)
  251. {
  252. $this->error("请选择礼物");
  253. }
  254. $giftvalue = bcmul($giftinfo['price'],$number);
  255. //被赠送人信息
  256. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  257. if (!$touserinfo)
  258. {
  259. $this->error("不存在的用户");
  260. }
  261. // 判断当前用户余额
  262. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  263. if($user_gold < $giftvalue)
  264. {
  265. $this->error("您的金币余额不足");
  266. }
  267. Db::startTrans();
  268. // 添加礼物赠送记录表
  269. $data = [
  270. 'user_id' => $this->auth->id,
  271. 'user_to_id' => $user_id,
  272. 'gift_id' => $giftinfo['id'],
  273. 'gift_name' => $giftinfo['name'],
  274. 'number' => $number,
  275. 'price' => $giftinfo['price'],
  276. 'total_price' => $giftvalue,
  277. 'createtime' => time(),
  278. ];
  279. $log_id = Db::name('gift_user_typing')->insertGetId($data);
  280. if(!$log_id){
  281. Db::rollback();
  282. $this->error('赠送失败');
  283. }
  284. if($giftvalue > 0){
  285. // 扣除当前用户余额
  286. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  287. if($wallet_rs['status'] === false){
  288. Db::rollback();
  289. $this->error($wallet_rs['msg']);
  290. }
  291. // 添加赠送用户余额
  292. $money_to_gold = config('site.money_to_gold');
  293. $gift_plat_scale = config('site.gift_plat_scale');
  294. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  295. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  296. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,54,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  297. if($wallet_rs['status'] === false){
  298. Db::rollback();
  299. $this->error($wallet_rs['msg']);
  300. }
  301. }
  302. Db::commit();
  303. $this->success('赠送成功');
  304. }
  305. }