Lottery.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Goods.php UTF-8
  4. * 商品处理
  5. *
  6. * @date : 2018/5/7 22:43
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\lottery;
  13. use huo\controller\common\Base;
  14. use huo\controller\member\MemCache;
  15. use huo\controller\posts\ShareAct;
  16. use huo\controller\shop\Award;
  17. use huo\controller\shop\GoodsCache;
  18. use huo\controller\shop\ItgOrder;
  19. use huo\logic\posts\PostsLogic;
  20. use huo\logic\shop\ItgOrderLogic;
  21. use huo\model\member\MemActivityModel;
  22. use huolib\constant\CacheConst;
  23. use huolib\constant\MemItgConst;
  24. use huolib\constant\NewsConst;
  25. use huolib\status\CommonStatus;
  26. use huolib\status\ShopStatus;
  27. use huolib\tool\Rand;
  28. use think\Cache;
  29. class Lottery extends Base {
  30. /**
  31. * 获取抽奖活动详情
  32. *
  33. * @param int $_mem_id
  34. * @param $_act_id int 活动id
  35. *
  36. * @return array
  37. */
  38. public function getDetail($_mem_id = 0, $_act_id) {
  39. if (!is_numeric($_act_id) || $_act_id < 0) {
  40. return $this->retErrMsg(CommonStatus::INVALID_PARAMS);
  41. }
  42. if (!empty($_mem_id)) {
  43. //当天首次进入,重置活动抽奖次数
  44. (new ShareAct())->resetLotteryCnt($_mem_id, $_act_id);
  45. }
  46. $_posts_data = (new PostsLogic())->getDetail($_act_id, NewsConst::NEWS_TYPE_AWARD);
  47. $_mem_act_model = new MemActivityModel();
  48. $_mem_act_data = $_mem_act_model->getMemAct($_mem_id, $_act_id);
  49. if (empty($_mem_act_data)) {
  50. if ($_mem_id > 0) {
  51. $_mem_act_data['mem_id'] = $_mem_id;
  52. $_mem_act_data['act_id'] = $_act_id;
  53. $_mem_act_id = $_mem_act_model->addMemAct($_mem_act_data);
  54. $_mem_act_data = $_mem_act_model->getMemActById($_mem_act_id);
  55. }
  56. }
  57. //获取最新的获奖记录20条
  58. $_where = array();
  59. $_where['act_id'] = $_act_id;
  60. $_itg_order_logic = new ItgOrderLogic();
  61. $_award_ad = $_itg_order_logic->getAwardOrderList($_where, "1,20");
  62. if ($_mem_id > 0) {//已登录
  63. $_me_data = MemCache::ins()->getMeInfoById($_mem_id);
  64. //获取我的抽奖记录
  65. $_where['mem_id'] = $_mem_id;
  66. $_rdata['my_integral'] = $_me_data['my_integral'];
  67. } else {
  68. $_rdata['my_integral'] = 0;
  69. }
  70. //我的奖品
  71. $_where['mem_id'] = $_mem_id;
  72. $_my_award = $_itg_order_logic->getAwardOrderList($_where, "1,20");;
  73. $_rdata['act_id'] = $_act_id;
  74. $_rdata['start_time'] = $_posts_data['start_time'];
  75. $_rdata['end_time'] = $_posts_data['end_time'];
  76. $_rdata['cost_integral'] = $_posts_data['cost_integral'];
  77. $_rdata['free_cnt'] = $_mem_act_data['free_cnt'] + $_mem_act_data['today_cnt'];
  78. $_rdata['roulette_img'] = $_posts_data['turntable'];
  79. $_rdata['pointer_img'] = $_posts_data['pointer'];
  80. $_rdata['award'] = Award::ins()->getAwards($_act_id);
  81. $_rdata['award_ad'] = $_award_ad;
  82. $_rdata['my_award'] = $_my_award;
  83. $_desc_list = explode("\n", $_posts_data['excerpt']);
  84. $_rdata['desc_list'] = $_desc_list;
  85. $_code = ShopStatus::NO_ERROR;
  86. return $this->huoSuccess($_code, ShopStatus::getMsg($_code), $_rdata);
  87. }
  88. /**
  89. * 抽奖
  90. *
  91. * @param $_mem_id
  92. * @param int $_act_id 活动id
  93. *
  94. * @return array
  95. */
  96. public function draw($_mem_id, $_act_id) {
  97. $_posts_data = (new PostsLogic())->getDetail($_act_id, NewsConst::NEWS_TYPE_AWARD);
  98. if (NewsConst::NEWS_PUBLISHED != $_posts_data['status']) { //未发布
  99. return $this->_getShopStatusReturn(ShopStatus::LOTTERY_HAS_NOT_STARTED);
  100. }
  101. $_time = time();
  102. if ($_time < $_posts_data['start_time']) { //活动未开始
  103. return $this->_getShopStatusReturn(ShopStatus::LOTTERY_HAS_NOT_STARTED);
  104. }
  105. if ($_time > $_posts_data['end_time']) { //活动已结束
  106. return $this->_getShopStatusReturn(ShopStatus::LOTTERY_HAS_ENDED);
  107. }
  108. $_award_datas = Award::ins()->getAwards($_act_id);
  109. $_arr = [];
  110. if (!empty($_award_datas)) {
  111. foreach ($_award_datas as $_k => $_v) {
  112. $_arr[$_k] = $_v['rate'];
  113. }
  114. }
  115. if (empty($_arr)) {
  116. return $this->_getShopStatusReturn(ShopStatus::LOTTERY_ERROR);
  117. }
  118. // /* 玩家重复点击抽奖临时处理 */
  119. // $_key = CacheConst::CACHE_MEM_LOTTERY_PREFIX.$_mem_id;
  120. // if (Cache::get($_key)) {
  121. // $_code = CommonStatus::TOO_MANY_REQUESTS;
  122. //
  123. // return $this->huoError($_code, CommonStatus::getMsg($_code));
  124. // } else {
  125. // Cache::set($_key, 1, MemItgConst::LOTTERY_LIMIT_TIME);
  126. // }
  127. $_key = Rand::getRand($_arr);
  128. $_award_data = $_award_datas[$_key];
  129. $_goods_id = $_award_data['goods_id'];
  130. if (empty($_goods_id)) {
  131. $_goods_id = MemItgConst::AWARD_NO_GOODS;
  132. }
  133. $_cost_integral = $_posts_data['cost_integral'];
  134. $_rs = (new ItgOrder())->drawExchange($_mem_id, $_goods_id, [], $_act_id, $_cost_integral);
  135. if (ShopStatus::NO_ERROR != $_rs['code']) {
  136. return $this->huoError($_rs['code'], $_rs['msg']);
  137. }
  138. $_goods_data = GoodsCache::ins()->getInfoByGoodsId($_goods_id);
  139. if ($_goods_data['object_name'] == MemItgConst::GOODS_IS_INTEGRAL
  140. || $_goods_data['object_name'] == MemItgConst::GOODS_IS_REDPACKET) {
  141. $shipping_status = MemItgConst::SHIP_STATUS_SUC;
  142. } else {
  143. $shipping_status = MemItgConst::SHIP_STATUS_WAIT_GET;
  144. }
  145. $_mem_act_model = new MemActivityModel();
  146. $_mem_act_data = $_mem_act_model->getMemAct($_mem_id, $_act_id);
  147. $_me_data = MemCache::ins()->getMeInfoById($_mem_id);
  148. //获取最新的获奖记录20条
  149. $_where = array();
  150. $_where['act_id'] = $_act_id;
  151. $_itg_order_logic = new ItgOrderLogic();
  152. $_award_ad = $_itg_order_logic->getAwardOrderList($_where, "1,20");
  153. //获取我的抽奖记录
  154. $_where['mem_id'] = $_mem_id;
  155. $_my_award = $_itg_order_logic->getAwardOrderList($_where, "1,20");;
  156. $_rdata['act_id'] = $_act_id;
  157. $_rdata['my_integral'] = $_me_data['my_integral'];
  158. $_rdata['cost_integral'] = $_cost_integral;
  159. $_rdata['free_cnt'] = $_mem_act_data['free_cnt'] + $_mem_act_data['today_cnt'];
  160. $_rdata['award_id'] = $_award_data['id'];
  161. $_rdata['order_id'] = $_rs['data']['order_id'];
  162. $_rdata['shipping_status'] = $shipping_status;
  163. $_rdata['list_order'] = $_award_data['list_order'];
  164. $_rdata['goods_id'] = $_goods_id;
  165. $_rdata['is_real'] = $_goods_data['is_real'];
  166. $_rdata['object_type'] = get_val($_goods_data, 'object_name', '');
  167. $_rdata['award_name'] = $_award_data['award_name'];
  168. $_rdata['original_img'] = $_goods_data['original_img'];
  169. if ($_goods_id == MemItgConst::AWARD_LOSE_GOODS_ID) {
  170. $_rdata['has_award'] = 1;
  171. } else {
  172. $_rdata['has_award'] = 2;
  173. }
  174. $_rdata['award_ad'] = $_award_ad;
  175. $_rdata['my_award'] = $_my_award;
  176. $_code = ShopStatus::NO_ERROR;
  177. return $this->huoSuccess($_code, ShopStatus::getMsg($_code), $_rdata);
  178. }
  179. protected function _getShopStatusReturn($code) {
  180. return $this->huoError($code, ShopStatus::getMsg($code));
  181. }
  182. }