MemIa.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * MemIa.php UTF-8
  4. * 玩家积分活动
  5. *
  6. * @date : 2018/5/5 15:02
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\integral;
  13. use huo\controller\common\Base;
  14. use huo\controller\common\CommonFunc;
  15. use huo\controller\game\GameCache;
  16. use huo\controller\member\MemCache;
  17. use huo\controller\pay\SdkOrderCache;
  18. use huo\controller\sign\Sign;
  19. use huo\logic\order\OrderLogic;
  20. use huo\model\integral\MemItgLogModel;
  21. use huolib\constant\IaConst;
  22. use huolib\constant\MemItgConst;
  23. use huolib\status\IntegralStatus;
  24. use huolib\tool\StrUtils;
  25. use huolib\tool\Time;
  26. use huomp\controller\agent\Income;
  27. use huomp\logic\member\MemInvitedLogLogic;
  28. class MemIa extends Base {
  29. private $mem_id = 0;
  30. private $mem_data = [];
  31. public function __construct($mem_id) {
  32. if (!empty($mem_id)) {
  33. $this->mem_id = $mem_id;
  34. $_mc_class = MemCache::ins();
  35. $this->mem_data = $_mc_class->getInfoById($mem_id);
  36. }
  37. }
  38. /**
  39. * 判断玩家是否有待完成的任务
  40. *
  41. * @return bool
  42. */
  43. public function getHasMemIa() {
  44. $_ias = IaCache::ins()->getIas();
  45. $_mia_class = MemIaCache::ins();
  46. $_has_task = false;
  47. foreach ($_ias as $_v) {
  48. $_mia = $_mia_class->getMemIaByMemIaId($_v['id'], $this->mem_id);
  49. $_is_done = $this->isHasDone($_v['id'], $_mia['last_time'], $_mia['total_cnt'], $_mia['today_cnt']);
  50. if (false == $_is_done) {
  51. $_has_task = true;
  52. }
  53. }
  54. return $_has_task;
  55. }
  56. /**
  57. * 获取玩家积分活动列表
  58. *
  59. * @return array
  60. */
  61. public function getMemIas() {
  62. $_ias = IaCache::ins()->getIas();
  63. $_mia_class = MemIaCache::ins();
  64. $_rdata = [];
  65. foreach ($_ias as $_v) {
  66. $_mia = $_mia_class->getMemIaByMemIaId($_v['id'], $this->mem_id);
  67. $_is_done = $this->isHasDone($_v['id'], $_mia['last_time'], $_mia['total_cnt'], $_mia['today_cnt']);
  68. $_data['ia_code'] = $_v['ia_code'];
  69. $_data['ia_name'] = $_v['ia_name'];
  70. $_data['ia_desc'] = $_v['ia_desc'];
  71. $_data['integral'] = $_v['integral'];
  72. $_data['start_time'] = $_v['start_time'];
  73. $_data['end_time'] = $_v['end_time'];
  74. $_data['type'] = IaConst::getIATypeMsg($_v['type']);
  75. $_data['finish_flag'] = MemItgConst::MEM_IA_NOT_DONE;
  76. if (true == $_is_done) {
  77. $_data['finish_flag'] = MemItgConst::MEM_IA_DONE;
  78. }
  79. $_rdata[] = $_data;
  80. }
  81. return $_rdata;
  82. }
  83. /**
  84. * 做积分活动
  85. *
  86. * @param int $ia_id
  87. * @param mixed $ext
  88. *
  89. * @return array|int
  90. */
  91. public function doItgAct($ia_id, $ext = null) {
  92. if (empty($this->mem_id)) {
  93. $_code = IntegralStatus::ITG_MEM_ID_EMPTY;
  94. return $this->huoError($_code, IntegralStatus::getMsg($_code));
  95. }
  96. if (empty($this->mem_data)) {
  97. $_code = IntegralStatus::ITG_MEM_NOT_EXISTS;
  98. return $this->huoError($_code, IntegralStatus::getMsg($_code));
  99. }
  100. if (empty($ia_id)) {
  101. $_code = IntegralStatus::ITG_IA_ID_EMPTY;
  102. return $this->huoError($_code, IntegralStatus::getMsg($_code));
  103. }
  104. $_ia_data = IaCache::ins()->getIa($ia_id);
  105. if (is_numeric($_ia_data)) {
  106. return $_ia_data;
  107. }
  108. switch ($ia_id) {
  109. case IaConst::IA_SIGN:
  110. {
  111. $_app_id = $ext;
  112. $_sign_class = new Sign();
  113. return $_sign_class->sign($this->mem_id, $_app_id);
  114. }
  115. case IaConst::IA_FIRST_CHARGE:
  116. {
  117. /* 首充 */
  118. $_code = $this->doFirstCharge();
  119. $this->doItgAct(IaConst::IA_DAY_FIRST_CHARGE, $ext);
  120. return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
  121. }
  122. case IaConst::IA_DAY_FIRST_CHARGE:
  123. {
  124. /* 每日首充 */
  125. $_code = $this->doDayFirstCharge();
  126. $this->doItgAct(IaConst::IA_CHARGE, $ext);
  127. return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
  128. }
  129. case IaConst::IA_CHARGE:
  130. {
  131. $_code = $this->doCharge($ext);
  132. return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
  133. }
  134. case IaConst::IA_TG_USER:
  135. {
  136. /* 推广员任务 */
  137. return false;
  138. }
  139. case IaConst::IA_UPLOAD_PORTRAIT:
  140. case IaConst::IA_START_APP:
  141. case IaConst::IA_BIND_MOBILE:
  142. case IaConst::IA_IDENTIFY:
  143. {
  144. /* 一次性任务 */
  145. /* 启动APP */
  146. /* 绑定手机 */
  147. /* 实名认证 */
  148. /* 收藏盒子 */
  149. $_code = $this->doAct($ia_id, $ext);
  150. return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
  151. }
  152. case IaConst::IA_FAVORITE:
  153. {
  154. /* 收藏盒子 */
  155. $_app_id = $ext['app_id'];
  156. $_game_id = $ext['game_id'];
  157. $_code = $this->doAct($ia_id, $_app_id, $_game_id);
  158. return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
  159. }
  160. case IaConst::IA_INVITE_3:
  161. case IaConst::IA_PLAY_3:
  162. {
  163. $_app_id = $ext['app_id'];
  164. $_game_id = $ext['game_id'];
  165. /* 日常任务 */
  166. $_code = $this->doAct($ia_id, $_app_id, $_game_id);
  167. return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
  168. }
  169. case IaConst::IA_OPEN_GAME:
  170. {
  171. $_app_id = $ext['app_id'];
  172. $_game_id = $ext['game_id'];
  173. /* 打开游戏 */
  174. $_code = $this->doOpen($_app_id, $_game_id);
  175. if (IntegralStatus::NO_ERROR != $_code) {
  176. return $this->huoError($_code, IntegralStatus::getMsg($_code));
  177. }
  178. return $this->doItgAct(IaConst::IA_SHARE, $ext);
  179. }
  180. case IaConst::IA_SHARE:
  181. {
  182. $_app_id = $ext['app_id'];
  183. $_game_id = $ext['game_id'];
  184. /* 分享游戏 */
  185. $_code = $this->doShare($_app_id, $_game_id);
  186. return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
  187. }
  188. default:
  189. $_code = IntegralStatus::ITG_IA_ERROR;
  190. return $this->huoError($_code, IntegralStatus::getMsg($_code));
  191. break;
  192. }
  193. }
  194. /**
  195. * 判断是否已经
  196. *
  197. * @param int $ia_id
  198. * @param int $last_time
  199. * @param int $total_cnt
  200. * @param int $today_cnt
  201. *
  202. * @return bool
  203. */
  204. public function isHasDone($ia_id, $last_time, $total_cnt, $today_cnt) {
  205. $_ia_data = IaCache::ins()->getIa($ia_id);
  206. switch ($_ia_data['type']) {
  207. case IaConst::IA_TYPE_ONE:
  208. {
  209. if ($total_cnt >= $_ia_data['limit_cnt']) {
  210. return true;
  211. }
  212. return false;
  213. }
  214. case IaConst::IA_TYPE_DAY:
  215. {
  216. list($_start_time, $_end_time) = Time::today();
  217. if ($last_time >= $_start_time && $last_time <= $_end_time
  218. && $today_cnt >= $_ia_data['limit_cnt']) {
  219. return true;
  220. }
  221. return false;
  222. }
  223. case IaConst::IA_TYPE_LONG:
  224. default:
  225. return false;
  226. }
  227. }
  228. /**
  229. * 一次性活动
  230. *
  231. * @param int $ia_id 积分活动ID
  232. *
  233. * @param int $app_id 应用ID
  234. * @param int $game_id 游戏ID
  235. *
  236. * @return int
  237. */
  238. public function doAct($ia_id, $app_id = 0, $game_id = 0) {
  239. $_ia_data = IaCache::ins()->getIa($ia_id);
  240. if (is_numeric($_ia_data)) {
  241. return $_ia_data;
  242. }
  243. $_integral = $_ia_data['integral'];
  244. $_type = $_ia_data['type'];
  245. switch ($_type) {
  246. case IaConst::IA_TYPE_ONE:
  247. /* 一次性任务不能超过限制次数 */
  248. $_rs = MemIaCache::ins()->getMemIaByMemIaId($ia_id, $this->mem_id);
  249. if (!empty($_rs) && $_rs['total_cnt'] >= $_ia_data['limit_cnt']) {
  250. return IntegralStatus::ITG_IA_DONE;
  251. }
  252. break;
  253. case IaConst::IA_TYPE_DAY:
  254. /* 每日任务不能超过限制次数 */
  255. $_rs = MemIaCache::ins()->getMemIaByMemIaId($ia_id, $this->mem_id);
  256. if (!empty($_rs) && $_rs['today_cnt'] >= $_ia_data['limit_cnt']) {
  257. return IntegralStatus::ITG_IA_DONE;
  258. }
  259. /* 防止任务重复 */
  260. $_rs = (new MemItgLogModel())->getMemIaTodayCnt($ia_id, $this->mem_id, $game_id);
  261. if (!empty($_rs)) {
  262. return IntegralStatus::ITG_IA_DONE;
  263. }
  264. break;
  265. default:
  266. ;
  267. }
  268. return (new MemIntegral())->addMemItg($this->mem_id, $_integral, $ia_id, $game_id, '', 0, '', $app_id);
  269. }
  270. /**
  271. * 做首充任务
  272. *
  273. * @return bool
  274. */
  275. public function doFirstCharge() {
  276. $_map['create_time'] = ['gt', 0];
  277. $_rs = (new OrderLogic())->isFirstCharge($_map);
  278. if (false == $_rs) {
  279. return IntegralStatus::ITG_IA_DONE;
  280. }
  281. return $this->doAct(IaConst::IA_FIRST_CHARGE);
  282. }
  283. /**
  284. * 每日首充任务
  285. *
  286. * @return int
  287. */
  288. public function doDayFirstCharge() {
  289. list($_start_time, $_end_time) = Time::today();
  290. $_map['create_time'] = ['between', [$_start_time, $_end_time]];
  291. $_map['mem_id'] = $this->mem_id;
  292. $_rs = (new OrderLogic())->isFirstCharge($_map);
  293. if (false == $_rs) {
  294. return IntegralStatus::ITG_IA_DONE;
  295. }
  296. return $this->doAct(IaConst::IA_DAY_FIRST_CHARGE);
  297. }
  298. public function doCharge($order_id = '') {
  299. $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
  300. $_integral = $this->getItgByAmount($_order_data['amount']);
  301. return (new MemIntegral())->addMemItg(
  302. $this->mem_id,
  303. $_integral,
  304. IaConst::IA_CHARGE,
  305. $_order_data['app_id'],
  306. 'pay',
  307. $_order_data['id'],
  308. '',
  309. $_order_data['app_id']
  310. );
  311. }
  312. /**
  313. * @param float $amount 充值金额
  314. *
  315. * @return float
  316. */
  317. public function getItgByAmount($amount) {
  318. return floor($amount * 10);
  319. }
  320. /**
  321. * 打开游戏获取收益
  322. *
  323. * @param int $app_id
  324. * @param int $game_id
  325. *
  326. * @return int
  327. */
  328. public function doOpen($app_id = 0, $game_id = 0) {
  329. $_ia_id = IaConst::IA_OPEN_GAME;
  330. $_integral = (new Income())->getOpenGain($this->mem_id, $game_id,$app_id);
  331. if (is_numeric($_integral)) {
  332. return $_integral;
  333. }
  334. if (empty($_integral['integral'])) {
  335. return IntegralStatus::NO_ERROR;
  336. }
  337. $_is_rp = false; //不转红包
  338. if ($app_id == $game_id) {
  339. $_is_rp = true; //转红包
  340. $_ia_name = '新用户奖励';
  341. } else {
  342. $_game_data = GameCache::ins()->getInfoByAppId($game_id);
  343. $_game_name = get_val($_game_data, 'name', '');
  344. $_ia_name = '打开'.$_game_name.'获得奖励';
  345. }
  346. return (new MemIntegral())->addMemItg(
  347. $this->mem_id, $_integral['integral'], $_ia_id, $game_id, '', 0, $_ia_name, $app_id, $_is_rp
  348. );
  349. }
  350. /**
  351. * 获取分享收益
  352. *
  353. * @param int $app_id
  354. * @param int $game_id
  355. *
  356. * @return array|int
  357. */
  358. public function doShare($app_id = 0, $game_id = 0) {
  359. $_ia_id = IaConst::IA_SHARE;
  360. $_integral = (new Income())->getShareGain($this->mem_id, $game_id);
  361. if (is_numeric($_integral)) {
  362. return $_integral;
  363. }
  364. if (empty($_integral['parent_id'])) {
  365. return IntegralStatus::NO_ERROR;
  366. }
  367. /* 添加邀请记录表 */
  368. $_gold_rmb_rate = CommonFunc::getGoldRmbRate();
  369. $_amount = StrUtils::formatNumber($_integral['integral'] / $_gold_rmb_rate);
  370. (new MemInvitedLogLogic())->upData($_integral['parent_id'], $this->mem_id, $_amount);
  371. if (empty($_integral['integral'])) {
  372. return IntegralStatus::NO_ERROR;
  373. }
  374. $_mem_data = MemCache::ins()->getInfoById($this->mem_id);
  375. $_game_data = GameCache::ins()->getInfoByAppId($game_id);
  376. $_game_name = get_val($_game_data, 'name', '');
  377. $_nick_name = get_val($_mem_data, 'nickname', '');
  378. if ($app_id == $game_id) {
  379. $_ia_name = $_nick_name.'授权成功获得奖励';
  380. } else {
  381. $_ia_name = $_nick_name.'试玩'.$_game_name;
  382. }
  383. return (new MemIntegral())->addMemItg(
  384. $_integral['parent_id'], $_integral['integral'], $_ia_id, $game_id, 'member', $this->mem_id, $_ia_name,
  385. $app_id
  386. );
  387. }
  388. }