123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- <?php
- /**
- * MemIa.php UTF-8
- * 玩家积分活动
- *
- * @date : 2018/5/5 15:02
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\controller\integral;
- use huo\controller\common\Base;
- use huo\controller\common\CommonFunc;
- use huo\controller\game\GameCache;
- use huo\controller\member\MemCache;
- use huo\controller\pay\SdkOrderCache;
- use huo\controller\sign\Sign;
- use huo\logic\order\OrderLogic;
- use huo\model\integral\MemItgLogModel;
- use huolib\constant\IaConst;
- use huolib\constant\MemItgConst;
- use huolib\status\IntegralStatus;
- use huolib\tool\StrUtils;
- use huolib\tool\Time;
- use huomp\controller\agent\Income;
- use huomp\logic\member\MemInvitedLogLogic;
- class MemIa extends Base {
- private $mem_id = 0;
- private $mem_data = [];
- public function __construct($mem_id) {
- if (!empty($mem_id)) {
- $this->mem_id = $mem_id;
- $_mc_class = MemCache::ins();
- $this->mem_data = $_mc_class->getInfoById($mem_id);
- }
- }
- /**
- * 判断玩家是否有待完成的任务
- *
- * @return bool
- */
- public function getHasMemIa() {
- $_ias = IaCache::ins()->getIas();
- $_mia_class = MemIaCache::ins();
- $_has_task = false;
- foreach ($_ias as $_v) {
- $_mia = $_mia_class->getMemIaByMemIaId($_v['id'], $this->mem_id);
- $_is_done = $this->isHasDone($_v['id'], $_mia['last_time'], $_mia['total_cnt'], $_mia['today_cnt']);
- if (false == $_is_done) {
- $_has_task = true;
- }
- }
- return $_has_task;
- }
- /**
- * 获取玩家积分活动列表
- *
- * @return array
- */
- public function getMemIas() {
- $_ias = IaCache::ins()->getIas();
- $_mia_class = MemIaCache::ins();
- $_rdata = [];
- foreach ($_ias as $_v) {
- $_mia = $_mia_class->getMemIaByMemIaId($_v['id'], $this->mem_id);
- $_is_done = $this->isHasDone($_v['id'], $_mia['last_time'], $_mia['total_cnt'], $_mia['today_cnt']);
- $_data['ia_code'] = $_v['ia_code'];
- $_data['ia_name'] = $_v['ia_name'];
- $_data['ia_desc'] = $_v['ia_desc'];
- $_data['integral'] = $_v['integral'];
- $_data['start_time'] = $_v['start_time'];
- $_data['end_time'] = $_v['end_time'];
- $_data['type'] = IaConst::getIATypeMsg($_v['type']);
- $_data['finish_flag'] = MemItgConst::MEM_IA_NOT_DONE;
- if (true == $_is_done) {
- $_data['finish_flag'] = MemItgConst::MEM_IA_DONE;
- }
- $_rdata[] = $_data;
- }
- return $_rdata;
- }
- /**
- * 做积分活动
- *
- * @param int $ia_id
- * @param mixed $ext
- *
- * @return array|int
- */
- public function doItgAct($ia_id, $ext = null) {
- if (empty($this->mem_id)) {
- $_code = IntegralStatus::ITG_MEM_ID_EMPTY;
- return $this->huoError($_code, IntegralStatus::getMsg($_code));
- }
- if (empty($this->mem_data)) {
- $_code = IntegralStatus::ITG_MEM_NOT_EXISTS;
- return $this->huoError($_code, IntegralStatus::getMsg($_code));
- }
- if (empty($ia_id)) {
- $_code = IntegralStatus::ITG_IA_ID_EMPTY;
- return $this->huoError($_code, IntegralStatus::getMsg($_code));
- }
- $_ia_data = IaCache::ins()->getIa($ia_id);
- if (is_numeric($_ia_data)) {
- return $_ia_data;
- }
- switch ($ia_id) {
- case IaConst::IA_SIGN:
- {
- $_app_id = $ext;
- $_sign_class = new Sign();
- return $_sign_class->sign($this->mem_id, $_app_id);
- }
- case IaConst::IA_FIRST_CHARGE:
- {
- /* 首充 */
- $_code = $this->doFirstCharge();
- $this->doItgAct(IaConst::IA_DAY_FIRST_CHARGE, $ext);
- return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
- }
- case IaConst::IA_DAY_FIRST_CHARGE:
- {
- /* 每日首充 */
- $_code = $this->doDayFirstCharge();
- $this->doItgAct(IaConst::IA_CHARGE, $ext);
- return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
- }
- case IaConst::IA_CHARGE:
- {
- $_code = $this->doCharge($ext);
- return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
- }
- case IaConst::IA_TG_USER:
- {
- /* 推广员任务 */
- return false;
- }
- case IaConst::IA_UPLOAD_PORTRAIT:
- case IaConst::IA_START_APP:
- case IaConst::IA_BIND_MOBILE:
- case IaConst::IA_IDENTIFY:
- {
- /* 一次性任务 */
- /* 启动APP */
- /* 绑定手机 */
- /* 实名认证 */
- /* 收藏盒子 */
- $_code = $this->doAct($ia_id, $ext);
- return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
- }
- case IaConst::IA_FAVORITE:
- {
- /* 收藏盒子 */
- $_app_id = $ext['app_id'];
- $_game_id = $ext['game_id'];
- $_code = $this->doAct($ia_id, $_app_id, $_game_id);
- return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
- }
- case IaConst::IA_INVITE_3:
- case IaConst::IA_PLAY_3:
- {
- $_app_id = $ext['app_id'];
- $_game_id = $ext['game_id'];
- /* 日常任务 */
- $_code = $this->doAct($ia_id, $_app_id, $_game_id);
- return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
- }
- case IaConst::IA_OPEN_GAME:
- {
- $_app_id = $ext['app_id'];
- $_game_id = $ext['game_id'];
- /* 打开游戏 */
- $_code = $this->doOpen($_app_id, $_game_id);
- if (IntegralStatus::NO_ERROR != $_code) {
- return $this->huoError($_code, IntegralStatus::getMsg($_code));
- }
- return $this->doItgAct(IaConst::IA_SHARE, $ext);
- }
- case IaConst::IA_SHARE:
- {
- $_app_id = $ext['app_id'];
- $_game_id = $ext['game_id'];
- /* 分享游戏 */
- $_code = $this->doShare($_app_id, $_game_id);
- return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
- }
- default:
- $_code = IntegralStatus::ITG_IA_ERROR;
- return $this->huoError($_code, IntegralStatus::getMsg($_code));
- break;
- }
- }
- /**
- * 判断是否已经
- *
- * @param int $ia_id
- * @param int $last_time
- * @param int $total_cnt
- * @param int $today_cnt
- *
- * @return bool
- */
- public function isHasDone($ia_id, $last_time, $total_cnt, $today_cnt) {
- $_ia_data = IaCache::ins()->getIa($ia_id);
- switch ($_ia_data['type']) {
- case IaConst::IA_TYPE_ONE:
- {
- if ($total_cnt >= $_ia_data['limit_cnt']) {
- return true;
- }
- return false;
- }
- case IaConst::IA_TYPE_DAY:
- {
- list($_start_time, $_end_time) = Time::today();
- if ($last_time >= $_start_time && $last_time <= $_end_time
- && $today_cnt >= $_ia_data['limit_cnt']) {
- return true;
- }
- return false;
- }
- case IaConst::IA_TYPE_LONG:
- default:
- return false;
- }
- }
- /**
- * 一次性活动
- *
- * @param int $ia_id 积分活动ID
- *
- * @param int $app_id 应用ID
- * @param int $game_id 游戏ID
- *
- * @return int
- */
- public function doAct($ia_id, $app_id = 0, $game_id = 0) {
- $_ia_data = IaCache::ins()->getIa($ia_id);
- if (is_numeric($_ia_data)) {
- return $_ia_data;
- }
- $_integral = $_ia_data['integral'];
- $_type = $_ia_data['type'];
- switch ($_type) {
- case IaConst::IA_TYPE_ONE:
- /* 一次性任务不能超过限制次数 */
- $_rs = MemIaCache::ins()->getMemIaByMemIaId($ia_id, $this->mem_id);
- if (!empty($_rs) && $_rs['total_cnt'] >= $_ia_data['limit_cnt']) {
- return IntegralStatus::ITG_IA_DONE;
- }
- break;
- case IaConst::IA_TYPE_DAY:
- /* 每日任务不能超过限制次数 */
- $_rs = MemIaCache::ins()->getMemIaByMemIaId($ia_id, $this->mem_id);
- if (!empty($_rs) && $_rs['today_cnt'] >= $_ia_data['limit_cnt']) {
- return IntegralStatus::ITG_IA_DONE;
- }
- /* 防止任务重复 */
- $_rs = (new MemItgLogModel())->getMemIaTodayCnt($ia_id, $this->mem_id, $game_id);
- if (!empty($_rs)) {
- return IntegralStatus::ITG_IA_DONE;
- }
- break;
- default:
- ;
- }
- return (new MemIntegral())->addMemItg($this->mem_id, $_integral, $ia_id, $game_id, '', 0, '', $app_id);
- }
- /**
- * 做首充任务
- *
- * @return bool
- */
- public function doFirstCharge() {
- $_map['create_time'] = ['gt', 0];
- $_rs = (new OrderLogic())->isFirstCharge($_map);
- if (false == $_rs) {
- return IntegralStatus::ITG_IA_DONE;
- }
- return $this->doAct(IaConst::IA_FIRST_CHARGE);
- }
- /**
- * 每日首充任务
- *
- * @return int
- */
- public function doDayFirstCharge() {
- list($_start_time, $_end_time) = Time::today();
- $_map['create_time'] = ['between', [$_start_time, $_end_time]];
- $_map['mem_id'] = $this->mem_id;
- $_rs = (new OrderLogic())->isFirstCharge($_map);
- if (false == $_rs) {
- return IntegralStatus::ITG_IA_DONE;
- }
- return $this->doAct(IaConst::IA_DAY_FIRST_CHARGE);
- }
- public function doCharge($order_id = '') {
- $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
- $_integral = $this->getItgByAmount($_order_data['amount']);
- return (new MemIntegral())->addMemItg(
- $this->mem_id,
- $_integral,
- IaConst::IA_CHARGE,
- $_order_data['app_id'],
- 'pay',
- $_order_data['id'],
- '',
- $_order_data['app_id']
- );
- }
- /**
- * @param float $amount 充值金额
- *
- * @return float
- */
- public function getItgByAmount($amount) {
- return floor($amount * 10);
- }
- /**
- * 打开游戏获取收益
- *
- * @param int $app_id
- * @param int $game_id
- *
- * @return int
- */
- public function doOpen($app_id = 0, $game_id = 0) {
- $_ia_id = IaConst::IA_OPEN_GAME;
- $_integral = (new Income())->getOpenGain($this->mem_id, $game_id,$app_id);
- if (is_numeric($_integral)) {
- return $_integral;
- }
- if (empty($_integral['integral'])) {
- return IntegralStatus::NO_ERROR;
- }
- $_is_rp = false; //不转红包
- if ($app_id == $game_id) {
- $_is_rp = true; //转红包
- $_ia_name = '新用户奖励';
- } else {
- $_game_data = GameCache::ins()->getInfoByAppId($game_id);
- $_game_name = get_val($_game_data, 'name', '');
- $_ia_name = '打开'.$_game_name.'获得奖励';
- }
- return (new MemIntegral())->addMemItg(
- $this->mem_id, $_integral['integral'], $_ia_id, $game_id, '', 0, $_ia_name, $app_id, $_is_rp
- );
- }
- /**
- * 获取分享收益
- *
- * @param int $app_id
- * @param int $game_id
- *
- * @return array|int
- */
- public function doShare($app_id = 0, $game_id = 0) {
- $_ia_id = IaConst::IA_SHARE;
- $_integral = (new Income())->getShareGain($this->mem_id, $game_id);
- if (is_numeric($_integral)) {
- return $_integral;
- }
- if (empty($_integral['parent_id'])) {
- return IntegralStatus::NO_ERROR;
- }
- /* 添加邀请记录表 */
- $_gold_rmb_rate = CommonFunc::getGoldRmbRate();
- $_amount = StrUtils::formatNumber($_integral['integral'] / $_gold_rmb_rate);
- (new MemInvitedLogLogic())->upData($_integral['parent_id'], $this->mem_id, $_amount);
- if (empty($_integral['integral'])) {
- return IntegralStatus::NO_ERROR;
- }
- $_mem_data = MemCache::ins()->getInfoById($this->mem_id);
- $_game_data = GameCache::ins()->getInfoByAppId($game_id);
- $_game_name = get_val($_game_data, 'name', '');
- $_nick_name = get_val($_mem_data, 'nickname', '');
- if ($app_id == $game_id) {
- $_ia_name = $_nick_name.'授权成功获得奖励';
- } else {
- $_ia_name = $_nick_name.'试玩'.$_game_name;
- }
- return (new MemIntegral())->addMemItg(
- $_integral['parent_id'], $_integral['integral'], $_ia_id, $game_id, 'member', $this->mem_id, $_ia_name,
- $app_id
- );
- }
- }
|