Gift.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * api获取礼包控制器
  4. */
  5. namespace app\api\controller\v1;
  6. use app\common\model\Gift as GiftModel;
  7. use think\Db;
  8. class Gift extends UserCenter
  9. {
  10. const SIZE = 50;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. }
  15. /**
  16. * 游戏礼包列表
  17. */
  18. public function index()
  19. {
  20. $page = input('page', 0, 'intval'); //页码
  21. if ($this->request->isAjax()) {
  22. if ($page > 1) {
  23. $gift_list = $this->getGiftData($this->game_id);
  24. $gift_list = $gift_list->toArray()['data'];
  25. if (!empty($gift_list)) {
  26. return $this->json($gift_list, 1);
  27. }
  28. return $this->json([], 0, '没有了!');
  29. }
  30. return $this->json([], 0, '没有了!');
  31. }
  32. $giftList = $this->getGiftData($this->game_id);
  33. $giftBox = $this->getBoxData($this->userInfo['id'], $this->game_id);
  34. $this->assign('username', $this->username);
  35. $this->assign('gameid', $this->game_id);
  36. $this->assign('gift_list', $giftList); //礼包列表
  37. $this->assign('gift_box', $giftBox); //存号箱
  38. return $this->fetch('list');
  39. }
  40. /*
  41. * 获取礼包数据
  42. */
  43. public function getGiftData($game_id)
  44. {
  45. $nowtime = NOW_TIMESTAMP;
  46. //$yearAgo = strtotime("-1 year");
  47. $GiftModel = new GiftModel();
  48. $giftList = $GiftModel->alias('gift')
  49. ->field('gift.id,gift.title,gift.consume,gift.content,gift.total,gift.used,gift.endtime,info.mobileicon')
  50. ->join('cy_gameinfo info', 'info.game_id = gift.gameid','left')
  51. ->where(['gift.isdelete'=>0])
  52. ->where(function ($query) use ($game_id) {
  53. if (isset($game_id) && !empty($game_id)) {
  54. $query->whereRaw(sprintf('FIND_IN_SET(%s,gameid)', $game_id));
  55. }
  56. })
  57. ->where('gift.starttime', '<=', $nowtime)
  58. ->where('gift.endtime', '>=', $nowtime)
  59. ->order('gift.create_time desc')
  60. ->paginate(self::SIZE);
  61. if (!empty($giftList)) { // 标识玩家已领取的礼包
  62. foreach ($giftList as &$gift) {
  63. // 剩余可领的数量占比
  64. $gift['percent'] = round(($gift['total'] - $gift['used']) / $gift['total'], 2) * 100;
  65. //$gift['image'] = STATIC_DOMAIN.$gift['image'];
  66. }
  67. }
  68. return $giftList;
  69. }
  70. /**
  71. * 获取存号箱数据
  72. */
  73. public function getBoxData($userid, $game_id)
  74. {
  75. // $list = Db::table('cy_libaolog log,cy_libaoinfo info,cy_game game')
  76. // ->join('cy_gameinfo gameinfo', 'gameinfo.game_id = game.id','left')
  77. // ->field('log.code,info.title,info.endtime,game.`name`,gameinfo.mobileicon as image')
  78. // ->where('log.`libaoid`=info.`id` and info.`gameid`=game.`id`')
  79. // ->where(['log.user_id' => $userid, 'info.gameid' => $game_id])
  80. // ->order('info.create_time desc')
  81. // ->paginate(self::SIZE)
  82. // ->each(function($item, $key){
  83. // $item['endtime'] = date('Y-m-d', $item['endtime']);
  84. // return $item;
  85. // });
  86. $where = [
  87. 'll.user_id' => $userid,
  88. 'a.isdelete' => 0,
  89. ];
  90. $gameInfo = model('game')->alias('b')->join('cy_gameinfo info', 'b.id=info.game_id', 'left')
  91. ->where('b.id',$game_id)
  92. ->field('info.mobileicon,b.name')->find();
  93. $list = model('libao')->alias('ll')
  94. ->join('cy_libaoinfo a', 'll.infoid=a.id and ll.user_id="' . $userid . '"')
  95. ->field('a.id,a.gameid,a.title,a.content,a.total,a.used,a.starttime,a.endtime,a.description,a.consume,ll.code')
  96. ->where($where)
  97. ->where(function ($query) use ($game_id) {
  98. if (isset($game_id) && !empty($game_id)) {
  99. $query->whereRaw(sprintf('FIND_IN_SET(%s,gameid)', $game_id));
  100. }
  101. })
  102. ->orderRaw('a.create_time desc')
  103. ->paginate(self::SIZE)
  104. ->each(function($item, $key) use($gameInfo){
  105. $item['endtime'] = date('Y-m-d', $item['endtime']);
  106. $item['name'] = $gameInfo['name'];
  107. $item['image'] = $gameInfo['mobileicon'];
  108. return $item;
  109. });
  110. // Db 查询分页,foreach修改无效 使用each
  111. return $list;
  112. }
  113. public function getBox()
  114. {
  115. if ($this->request->isAjax()) {
  116. $page = input('page', 0, 'intval');
  117. if ($page > 1) {
  118. $gift_box = $this->getBoxData($this->username, $this->game_id);
  119. $gift_box = $gift_box->toArray()['data'];
  120. if (!empty($gift_box)) {
  121. return $this->json($gift_box, 1);
  122. }
  123. return $this->json([], 0, '没有了!');
  124. }
  125. return $this->json([], 0, '没有了!');
  126. }
  127. }
  128. /**
  129. * 领取礼包
  130. */
  131. public function getGift()
  132. {
  133. $username = $this->username; //用户名
  134. $gift_id = input('giftid'); //礼包ID
  135. if (empty($gift_id)) return $this->json('', 0, '礼包ID不能为空');
  136. if (empty($username)) return $this->json('', 0, '用户名不能为空');
  137. // 1 判断是否领取过
  138. $hasReceive = Db::table('cy_libao')->alias('a')
  139. ->field('a.code ,a.id as logid')
  140. ->where(['a.infoid' => $gift_id, 'a.user_id' => $this->userInfo['id']])
  141. ->find();
  142. if (!empty($hasReceive)) {
  143. return $this->json('', 0, '您已领取过该礼包');
  144. }
  145. $info = Db::table('cy_libaoinfo')->alias('a')
  146. ->join('cy_libao b', 'a.id = b.infoid', 'left')
  147. ->field('b.code,a.endtime,a.used,a.total,b.id as lid,a.consume')
  148. ->where(['a.id' => $gift_id, 'b.status' => 0])->find();
  149. if ($info['used'] >= $info['total'] || empty($info['code'])) {
  150. return $this->json('', 0, '该礼包已被领取完');
  151. }
  152. if ($info['endtime'] <= NOW_TIMESTAMP - 86400) {
  153. return $this->json('', 0, '该礼包已过期');
  154. }
  155. if ($info['consume'] > 0) {
  156. $pay = model('pay')->cache('v2:gift:giftList:pay:' . $this->userInfo['id'] . ':' . $this->game_id, 30)
  157. ->where(['userid' => $this->userInfo['id'], 'gameid' => $this->game_id, 'status' => 1])->sum('amount');
  158. if ($pay < $info['consume']) {
  159. return $this->json('', 0, '暂无资格领取');
  160. }
  161. }
  162. Db::startTrans();
  163. try {
  164. Db::table('cy_libao')->where(array('id' => $info['lid']))->update(array('status' => 1, 'update_time' => NOW_TIMESTAMP, 'user_id' => $this->userInfo['id']));
  165. Db::table('cy_libaoinfo')->where(array('id' =>$gift_id))->setInc('used');
  166. // 提交事务
  167. Db::commit();
  168. } catch (\Exception $e) {
  169. // 回滚事务
  170. Db::rollback();
  171. return $this->json('', 0, '该礼包该礼包暂时无法领取或已被领取完'. $e);
  172. }
  173. return $this->json('', 1, '领取成功,请返回存号箱查看。');
  174. // if (!$gift_info = Db::table('cy_libaoinfo')->where(['id' => $gift_id])->find()) {
  175. // return $this->json('', 0, '礼包不存在');
  176. // }
  177. //
  178. // $nowtime = time();
  179. //
  180. // //时间判断
  181. // if ($gift_info['starttime'] > $nowtime) {
  182. // return $this->json('', 0, '领取条件未满足');
  183. // }
  184. // if ($gift_info['endtime'] < $nowtime) {
  185. // return $this->json('', 0, '领取条件未满足');
  186. // }
  187. //
  188. // //是否领取判断
  189. // $res = Db::table('cy_libaolog')->where(['username' => $username, 'libaoid' => $gift_id])->find();
  190. // if ($res) {
  191. // return $this->json('', 0, '已领取过礼包');
  192. // }
  193. // // 领取金额限制: 累计成功消费金额
  194. // $userPay = Db::table('cy_pay')->field(['sum(amount)' => 'amount'])->where(['username' => $username])
  195. // ->where('status', '=', 1)->find();
  196. // $user_consume = ! empty($userPay) ? $userPay['amount'] : 0;
  197. //
  198. // //判断消费
  199. // if ($gift_info['consume'] != 0 && $user_consume < $gift_info['consume']) {
  200. // return $this->json('', 0, '领取条件未满足');
  201. // }
  202. // //指定时间内,禁止重复领取
  203. // if(!requestDuplicateCheck('gift_duplicate_'.$username.'_'.$gift_id,10)){
  204. //
  205. // return $this->json('', 0, '相同的礼包领取请求过多,请于10s以后,再次对该礼包的进行领取操作');
  206. // }
  207. //
  208. // // todo 并发情况下 重复领取情况 是否加上lock
  209. // $info = Db::table('cy_libaoinfo info,cy_libao libao')->field('libao.id,libao.code')
  210. // ->where('libao.`infoid`=info.id')->where([
  211. // 'info.id' => $gift_id,
  212. // 'libao.status' => 0,
  213. // ])->where(['info.isdelete'=>0])->order('info.starttime ASC')->find();
  214. //
  215. // if ($info) {
  216. // // 启动事务
  217. // Db::startTrans();
  218. //
  219. // try {
  220. // $code = $info['code']; // 激活码
  221. // // 更新激活码状态
  222. // Db::table('cy_libao')->where(['infoid' => $gift_id, 'id' => $info['id']])->update([
  223. // 'status' => 1,
  224. // 'update_time' => $nowtime,
  225. // ]);
  226. // // 更新礼包表领取个数
  227. // Db::table('cy_libaoinfo')->where(['id' => $gift_id])->update(['used' => Db::raw('used+1')]);
  228. // // 插入领取记录
  229. // Db::table('cy_libaolog')->insert([
  230. // 'username' => $username,
  231. // 'libaoid' => $gift_id,
  232. // 'code' => $code,
  233. // 'create_time' => $nowtime,
  234. // ]);
  235. // // 提交事务
  236. // Db::commit();
  237. //
  238. // } catch (\Exception $e) {
  239. // // 回滚事务
  240. // Db::rollback();
  241. //
  242. // return $this->json('', 0, '领取失败');
  243. // }
  244. //
  245. // return $this->json('', 1, '领取成功');
  246. // } else {
  247. // return $this->json('', 0, '领取失败');
  248. // }
  249. }
  250. /**
  251. * 礼包详情页面
  252. */
  253. public function giftDetail()
  254. {
  255. //session('gift_list', true);
  256. //$game_id = input('game_id'); //游戏ID
  257. $username = $this->username; //用户名
  258. $gift_id = input('gift_id');
  259. //参数校验
  260. if (empty($username) || empty($gift_id)) {
  261. $this->jsonResult('', 0, '礼包参数不能为空');
  262. }
  263. $giftInfo = Db::table('cy_libaoinfo')
  264. ->alias('gift')
  265. ->join('cy_gameinfo gameinfo', 'gameinfo.game_id = gift.gameid','left')
  266. ->field('gift.id,gift.title,gift.consume,gift.content,gift.total,gift.used,gift.starttime,gift.endtime,
  267. gift.gameid,gift.description,gameinfo.mobileicon')
  268. ->where(['gift.id' => $gift_id])
  269. ->find();
  270. //判断用户是否已经领取了该利好
  271. //已领取礼包记录
  272. $isReceived = Db::table('cy_libao')->field('code')->where([
  273. 'infoid' => $gift_id,
  274. 'user_id' => $this->userInfo['id'],
  275. ])->find();
  276. $giftInfo['percent']= round(($giftInfo['total'] - $giftInfo['used']) / $giftInfo['total'], 2) * 100;
  277. $giftInfo['mobileicon'] = $giftInfo['mobileicon'];
  278. if (!empty($isReceived)) {
  279. $giftInfo['received'] = true;
  280. } else {
  281. $giftInfo['received'] = false;
  282. }
  283. $this->assign('username', $username);
  284. $this->assign('gift_id', $gift_id);
  285. $this->assign('gameid', $giftInfo['gameid']);
  286. $this->assign('gift_info', $giftInfo);
  287. return $this->fetch('detail');
  288. }
  289. }