Gift.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * Gift.php UTF-8
  4. * 礼包处理类
  5. *
  6. * @date : 2016年12月3日下午4:58:28
  7. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  8. * @author : wuyonghong <wyh@huosdk.com>
  9. * @version : HUOSDK 7.0
  10. * @modified: 2016年12月3日下午4:58:28
  11. */
  12. namespace huo\controller\gift;
  13. use huo\controller\common\Base;
  14. use huo\controller\game\GameCache;
  15. use huo\controller\help\QqCache;
  16. use huo\controller\integral\MemIntegral;
  17. use huo\controller\member\MemCache;
  18. use huo\logic\game\GiftLogic;
  19. use huolib\constant\MemItgConst;
  20. use huolib\status\GiftStatus;
  21. use huolib\status\IntegralStatus;
  22. use huolib\status\MemberStatus;
  23. class Gift extends Base {
  24. protected $gift_model;
  25. public function __construct(GiftLogic $gift_model = null) {
  26. if (null === $gift_model) {
  27. $this->gift_model = new GiftLogic();
  28. } else {
  29. $this->gift_model = $gift_model;
  30. }
  31. }
  32. /**
  33. * 获取积分礼包列表
  34. *
  35. * @param int $game_id
  36. * @param int $mem_id
  37. * @param string $page
  38. * @param bool $inc_me
  39. *
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public function getIntegralGifts($game_id = 0, $mem_id = 0, $page = '1,10', $inc_me = false) {
  46. $_map['condition'] = ['gt', 0];
  47. if (!empty($game_id)) {
  48. $_map['app_id'] = $game_id;
  49. }
  50. return $this->getGifts($mem_id, $_map, $page, $inc_me);
  51. }
  52. /**
  53. * 获取普通礼包列表
  54. *
  55. * @param int $game_id
  56. * @param int $mem_id
  57. * @param string $page
  58. * @param bool $inc_me
  59. *
  60. * @return array
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @throws \think\exception\DbException
  64. */
  65. public function getNormalGifts($game_id = 0, $mem_id = 0, $page = '1,10', $inc_me = false) {
  66. $_map['condition'] = 0;
  67. if (!empty($game_id)) {
  68. $_map['app_id'] = $game_id;
  69. }
  70. return $this->getGifts($mem_id, $_map, $page, $inc_me);
  71. }
  72. /**
  73. * 获取礼包列表
  74. *
  75. * @param int $mem_id
  76. * @param $map
  77. * @param string $page
  78. *
  79. * @param bool $inc_me
  80. *
  81. * @return array
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. */
  86. public function getGifts($mem_id, $map, $page, $inc_me = false) {
  87. $_map = $map;
  88. if (isset($_map['gift_type'])) {
  89. //礼包搜索类型条件处理
  90. switch ($map['gift_type']) {
  91. case 1:
  92. $_map['condition'] = 0;
  93. $_map['qq_id'] = 0;
  94. break;
  95. case 2:
  96. $_map['condition'] = ['>', 0];
  97. break;
  98. case 3:
  99. $_map['qq_id'] = ['>', 0];
  100. break;
  101. }
  102. unset($_map['gift_type']);
  103. }
  104. $_data = $this->getGiftListDetail($mem_id, $_map, $page, $inc_me);
  105. if (is_numeric($_data)) {
  106. $_code = $_data;
  107. return $this->huoError($_code, GiftStatus::getMsg($_code));
  108. }
  109. $_code = GiftStatus::NO_ERROR;
  110. return $this->huoSuccess($_code, GiftStatus::getMsg($_code), $_data);
  111. }
  112. /**
  113. * 获取礼包列表详情
  114. *
  115. * @param string $mem_id
  116. * @param array $map
  117. * @param string $page
  118. *
  119. * @param bool $inc_me
  120. *
  121. * @return mixed
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @throws \think\exception\DbException
  125. */
  126. public function getGiftListDetail($mem_id, $map, $page, $inc_me = false) {
  127. $_data = $this->gift_model->getGifts($mem_id, $map, $page, $inc_me);
  128. if (false == is_array($_data)) {
  129. return $_data;
  130. }
  131. $_list = $_data['list'];
  132. if (empty($_list)) {
  133. return $_data;
  134. }
  135. unset($_data['list']);
  136. $_data['list'] = $this->getGiftExtInfo($_list);
  137. return $_data;
  138. }
  139. /**
  140. * 获取礼包扩展信息
  141. *
  142. * @param array $data
  143. *
  144. * @return array
  145. */
  146. public function getGiftExtInfo($data = []) {
  147. if (empty($data)) {
  148. return $data;
  149. }
  150. $_qq_class = new QqCache();
  151. foreach ($data as $_k => $_v) {
  152. if (empty($_v['qq_id'])) {
  153. $data[$_k]['qq_group'] = null;
  154. } else {
  155. $data[$_k]['qq_group'] = $_qq_class->getInfoByQqId($_v['qq_id']);
  156. }
  157. $_game_info = GameCache::ins()->getInfoByAppId($_v['game_id']);
  158. $data[$_k]['icon'] = $_game_info['icon'];
  159. $data[$_k]['game_name'] = $_game_info['name'];
  160. }
  161. return $data;
  162. }
  163. /**
  164. * 获取礼包详情
  165. *
  166. * @param int $gift_id
  167. * @param $mem_id
  168. *
  169. * @return array
  170. */
  171. public function getGiftDetail($gift_id = 0, $mem_id) {
  172. if (empty($gift_id)) {
  173. $_code = GiftStatus::GAME_ID_EMPTY;
  174. $this->huoError($_code, GiftStatus::getMsg($_code));
  175. }
  176. $_gift_data = GiftCache::ins()->getInfoByGiftId($gift_id);
  177. if (empty($_gift_data)) {
  178. $_code = GiftStatus::GIFT_NOT_EXISTS;
  179. return $this->huoError($_code, GiftStatus::getMsg($_code));
  180. }
  181. $_gift_logic = new GiftLogic();
  182. $_base_field = $_gift_logic->getBaseField();
  183. foreach ($_base_field as $_k => $_v) {
  184. $_data[$_v] = $_gift_data[$_k];
  185. }
  186. $_gift_code = $_gift_logic->getGiftCodes($gift_id, $mem_id);
  187. if (!empty($_gift_code)) {
  188. $_data['code'] = $_gift_code[$gift_id];
  189. } else {
  190. $_data['code'] = null;
  191. }
  192. $_ext_data[] = $_data;
  193. $_rdata = $this->getGiftExtInfo($_ext_data);
  194. $_code = GiftStatus::NO_ERROR;
  195. return $this->huoSuccess($_code, GiftStatus::getMsg($_code), $_rdata[0]);
  196. }
  197. /**
  198. * 领取礼包
  199. *
  200. * @param int $mem_id
  201. * @param int $gift_id
  202. *
  203. * @return array
  204. */
  205. public function setGift($mem_id = 0, $gift_id = 0, $isAward = 0) {
  206. if (empty($mem_id)) {
  207. $_code = MemberStatus::LOGIN_IS_OUT;
  208. return $this->huoError($_code, MemberStatus::getMsg($_code));
  209. }
  210. if (empty($gift_id)) {
  211. $_code = GiftStatus::GIFT_ID_EMPTY;
  212. return $this->huoError($_code, GiftStatus::getMsg($_code));
  213. }
  214. $_gift_cache = GiftCache::ins();
  215. $_gift_data = $_gift_cache->getInfoByGiftId($gift_id);
  216. if (empty($_gift_data)) {
  217. $_code = GiftStatus::GIFT_NOT_EXISTS;
  218. return $this->huoError($_code, GiftStatus::getMsg($_code));
  219. }
  220. /* 判断余量 */
  221. if (empty($_gift_data['remain_cnt']) || $_gift_data['remain_cnt'] <= 0) {
  222. $_code = GiftStatus::GIFT_REMAIN_ZERO;
  223. return $this->huoError($_code, GiftStatus::getMsg($_code));
  224. }
  225. $_mc_class = MemCache::ins();
  226. $_me_data = $_mc_class->getMeInfoById($mem_id);
  227. if (!$isAward) {//不是奖品,扣除积分
  228. if ($_gift_data['condition'] > 0) {
  229. $_my_integral = $_me_data['my_integral'];
  230. if ($_my_integral < $_gift_data['condition']) {
  231. $_code = IntegralStatus::ITG_NOT_ENOUGH;
  232. return $this->huoError($_code, IntegralStatus::getMsg($_code));
  233. }
  234. $_me_data['my_integral'] -= $_gift_data['condition'];
  235. $_mc_class->saveMeCache($mem_id, $_me_data);
  236. }
  237. }
  238. $_gift_data['remain_cnt']--;
  239. $_gift_cache->updateGift($gift_id, $_gift_data);
  240. $_data = $this->gift_model->setGift($mem_id, $gift_id);
  241. if (false === $_data) {
  242. $_code = GiftStatus::GIFT_GET_ERROR;
  243. return $this->huoError($_code, GiftStatus::getMsg($_code));
  244. }
  245. $_mitg_class = new MemIntegral();
  246. /* 减少此次积分 */
  247. $_mitg_class->setMemItg($mem_id, $_gift_data['condition'], MemItgConst::MEM_ITG_DEDUCT, 0, 0, "", 0, "兑换礼包");
  248. $_me_data['gift_cnt']++;
  249. $_mc_class->updateMeCache($mem_id, $_me_data);
  250. $_code = GiftStatus::NO_ERROR;
  251. $_gift_data['code'] = $_data;
  252. return $this->huoSuccess($_code, GiftStatus::getMsg($_code), $_gift_data);
  253. }
  254. /**
  255. * 获取游戏礼包列表
  256. *
  257. * @param int $mem_id 玩家ID
  258. * @param $from
  259. * @param string $page 页码
  260. * @param $app_ids
  261. *
  262. * @return array
  263. * @throws \think\db\exception\DataNotFoundException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. * @throws \think\exception\DbException
  266. */
  267. public function getGameGifts($mem_id, $from, $page = '', $app_ids = null) {
  268. $_gift_logic = new GiftLogic();
  269. $_map['classify'] = $from;
  270. if (!empty($app_id)) {
  271. if (is_array($app_ids)) {
  272. $_map['id'] = ['in', $app_ids];
  273. } elseif (is_numeric($app_ids)) {
  274. $_map['id'] = $app_ids;
  275. }
  276. }
  277. $_map['classify'] = $from;
  278. $_app_rs = $_gift_logic->getHasGiftAppIds($_map, $page);
  279. if (is_numeric($_app_rs)) {
  280. $_data=['count'=>0,'list'=>[]];
  281. return $this->huoError($_app_rs, GiftStatus::getMsg($_app_rs),$_data);
  282. }
  283. $_rdata['count'] = $_app_rs['count'];
  284. $_app_ids = $_app_rs['list'];
  285. $_gc_class = GameCache::ins();
  286. $_list = [];
  287. foreach ($_app_ids as $_app_id) {
  288. $_game_data = $_gc_class->getInfoByAppId($_app_id);
  289. unset($_data);
  290. $_data['game_id'] = $_app_id;
  291. $_data['gamename'] = $_game_data['name'];
  292. $_data['icon'] = $_game_data['icon'];
  293. $_gift_map['app_id'] = $_app_id;
  294. $_gift_data = $this->getGiftListDetail($mem_id, $_gift_map, $page,true);
  295. $_data['gift_cnt'] = $_gift_data['count'];
  296. $_data['gift_list'] = $_gift_data['list'];
  297. $_list[] = $_data;
  298. }
  299. if (empty($_list)) {
  300. $_code = GiftStatus::UNKNOWN_ERROR;
  301. return $this->huoError($_code, GiftStatus::getMsg($_code));
  302. }
  303. $_rdata['list'] = $_list;
  304. $_code = GiftStatus::NO_ERROR;
  305. return $this->huoSuccess($_code, GiftStatus::getMsg($_code), $_rdata);
  306. }
  307. /**
  308. * 获取玩家礼包列表
  309. *
  310. * @param int $mem_id
  311. * @param string $page
  312. */
  313. public function getMemGifts($mem_id, $page = '') {
  314. if (empty($mem_id)) {
  315. $_code = MemberStatus::LOGIN_IS_OUT;
  316. return $this->huoError($_code, MemberStatus::getMsg($_code));
  317. }
  318. $_rs = $this->gift_model->getMemGifts($mem_id, $page);
  319. $_code = GiftStatus::NO_ERROR;
  320. if (GiftStatus::GIFT_CNT_ZERO == $_rs) {
  321. return $this->huoSuccess($_code, GiftStatus::getMsg($_code));
  322. }
  323. return $this->huoSuccess($_code, GiftStatus::getMsg($_code), $_rs);
  324. }
  325. }