activitesmodel.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018-10-11
  6. * Time: 下午 06:19
  7. */
  8. class ActivitesModel
  9. {
  10. public $sid = null;
  11. public $appid = null;
  12. public $openid = null;
  13. public $channel = null;
  14. public $channel_id = null;
  15. public $act_type = null;
  16. public $act_rule = null;
  17. protected $act_start_at = null;
  18. protected $act_end_at = null;
  19. public $pact = null;
  20. public $config_data = null;
  21. private $redis = null;
  22. private $time = null;
  23. private $time_day = null;
  24. private $utils = null;
  25. private $cache_key = null;
  26. private $file_name = null;
  27. private $service = null;
  28. private $server_prefix = null;
  29. public $ranking_type = [1 => 'power', 2 => 'level'];
  30. public $role_ranking_type = null;
  31. protected $user_info = null;
  32. protected $request = null;
  33. protected static $role_prize_uplimit = ZERO;
  34. //--
  35. public $uid = null;
  36. public $gameid = null;
  37. protected static $role_lotteres_cont_url = "http://api.djsh5.com/wxgameapi/wxluckycount/";
  38. const ROLE_LOTTERES_KEY = '3sat9s828s6sgank';
  39. public function __construct($act_type = null)
  40. {
  41. $this->utils = new Utils();
  42. $this->httpData(); // 初始化基础数据
  43. $this->service = new ActivitySer();
  44. // 统计页面点击
  45. $this->statwebClickLog();
  46. }
  47. /***
  48. * 初始基础配置数据
  49. * @return bool
  50. */
  51. public function httpDataVerif($act_type = null)
  52. {
  53. $request = file_get_contents("php://input");
  54. $this->request = $request;
  55. $this->uid = self::isParame($_GET, 'uid');
  56. $this->gameid = self::isParame($_GET, 'gameid');; //self::isParame($this->request,'appid');
  57. $this->act_type = $act_type; //self::isParame($this->request,'appid');//你自己定义的要
  58. $this->role_ranking_type = ONE; //self::isParame($this->request,'appid');
  59. $this->file_name = $this->getWechatFileName();
  60. log_message::info('file name ', $this->file_name);
  61. $this->config_data = $this->utils->getFileConfig($this->file_name);
  62. $this->config_data = $this->isActivitesData($this->config_data);
  63. $this->time = date(DATE_FORMAT_S, time());
  64. return true;
  65. }
  66. public function initializeHttpData($act_type)
  67. {
  68. $this->httpDataVerif($act_type);
  69. if (!$this->isActivites($act_type)) {
  70. log_message::info("活动规则异常");
  71. exit(Utils::sendResults('活动规则异常'));
  72. }
  73. }
  74. public function httpData()
  75. {
  76. $request = file_get_contents("php://input");
  77. log_message::info("http POST DATA ...", json_encode($_POST));
  78. log_message::info("http php://input DATA ", json_encode($this->request));
  79. log_message::info("http GET DATA conttents php input ...", json_encode($_GET));
  80. $this->request = $request;
  81. $this->uid = self::isParame($_GET, 'uid');
  82. if (empty($this->uid)) {
  83. exit(Utils::sendResults('网络失败'));
  84. }
  85. $this->gameid = self::isParame($_GET, 'gameid');
  86. $this->gameid = self::isParame($_GET, 'gameid');
  87. if (empty($this->gameid)) {
  88. exit(Utils::sendResults('网络失败'));
  89. }
  90. $this->time = date(DATE_FORMAT_S, time());
  91. $this->time_day = date(DATE_FORMAT_D, time());
  92. return true;
  93. }
  94. /***
  95. * 抽奖日志统计
  96. */
  97. public function statLotteyLog($prize_id)
  98. {
  99. $data = [
  100. 'uid'=>$this->uid,
  101. 'gameid'=>$this->gameid,
  102. 'prize_id'=>$prize_id,
  103. 'date'=>date('Ymd',time()),
  104. ];
  105. $res = $this->service->setLotteryLog($data);
  106. if (!$res){
  107. log_message::info("存储日志失败");
  108. }
  109. }
  110. /***
  111. * 页面点击统计日志
  112. * @param $prize_id
  113. */
  114. public function statwebClickLog()
  115. {
  116. $data = [
  117. 'uid'=>$this->uid,
  118. 'gameid'=>$this->gameid,
  119. 'date'=>date('Ymd',time()),
  120. ];
  121. $res = $this->service->setWebClickLog($data);
  122. if (!$res){
  123. log_message::info("存储抽奖日志失败");
  124. }
  125. }
  126. /***
  127. * 参数是否为空
  128. * @param $data
  129. * @return bool
  130. */
  131. public static function isParame($data, $key)
  132. {
  133. if ($red = isDatas($data, $key)) {
  134. return $red;
  135. }
  136. log_message::info("$key is null ");
  137. //exit(Utils::errorCode("$key is null ", FAILURE));
  138. return null;
  139. }
  140. //
  141. public function getCacheKey()
  142. {
  143. return 'global_config';
  144. }
  145. public function getFileName($appid = null)
  146. {
  147. return $this->appid . '.json';
  148. }
  149. public function getWechatFileName($appid = null)
  150. {
  151. return 'wechat.json';
  152. }
  153. /***
  154. * @param $role_sid
  155. * @param $min_sid
  156. * @param $max_sid
  157. */
  158. public function isActivites($act_type = null)
  159. {
  160. // 游戏是否存在
  161. /* if (!$this->isApp()) {
  162. log_message::info('appid游戏不存在');
  163. return false;
  164. }*/
  165. // 活动是否开启
  166. if (!$this->isActivitesStatus()) {
  167. log_message::info('活动不存在');
  168. return false;
  169. }
  170. // 活动类型是否存在 && $act_type!=-1 act_rule true set
  171. if (!$this->isActivityType()) {
  172. log_message::info('活动类型不存在');
  173. return false;
  174. }
  175. // 渠道活动的区服范围是否正常
  176. /*if (!$this->isServer()) {
  177. log_message::info('渠道活动区服不存在或范围有误');
  178. return false;
  179. }*/
  180. // 渠道是否在有效时间范围内 max time && $act_type!=-1
  181. if (!$this->isActivitesTime()) {
  182. log_message::info('渠道活动时间已经失效');
  183. return false;
  184. }
  185. return true;
  186. }
  187. /***
  188. * 活动时间校验是否为有效期
  189. * @param $start_at
  190. * @param $end_at
  191. * @return bool
  192. */
  193. public function isActivitesTime($start_at = null, $end_at = null)
  194. {
  195. $act_rule = $this->act_rule;
  196. log_message::info("+++++++++++++++++", json_encode($act_rule, JSON_UNESCAPED_UNICODE));
  197. $start_at = strtotime($act_rule[$this->act_type]['start_at']);
  198. $end_at = strtotime($act_rule[$this->act_type]['end_at']);
  199. log_message::info("当前时间", $this->time, "活动开始时间" . $start_at, "活动结束时间" . $end_at);
  200. // 如果活动是用户 就需要初始化配置数据抽奖的
  201. // 首先判断抽奖活动的配置时间与用的更新的配置时间是否一样的
  202. // 并且获得的时间在当前时间的有效期范围之内才可以否则 是失败的
  203. // 就是不通过的
  204. log_message::info("^^^^^^^^^^^^^^", json_encode($this->user_info, JSON_UNESCAPED_UNICODE));
  205. if ((strtotime($this->time) >= $start_at) && (strtotime($this->time) <= $end_at)) {
  206. $this->act_start_at = $start_at;
  207. $this->act_end_at = $end_at;
  208. // 重置活动开始截止时间 活动实效之前依然存在活动次数
  209. // 活动实效之后所属的活动时间 抽取次数必须要进行重置
  210. // $this->editRoleLotteryEndAt();
  211. return true;
  212. }
  213. // 否则如果不在有效期有可能活动已经实效了
  214. // $this->editRoleLotteryEndAt();
  215. return false;
  216. }
  217. /***
  218. * 重置用户抽奖活动的截止时间,判断截止时间是否失效如果已经失效重置抽奖次数
  219. * 必须要进行初始化不然等玩家下一次抽奖的时候数据就不准确了
  220. * 但是又一点 以为每次抽奖的时候用户的活动数据都会根据玩家对应的数据更新到表中
  221. * 这样做有什么意义呢???
  222. * @return bool
  223. */
  224. public function editRoleLotteryEndAt()
  225. {
  226. $act_lottery_end_at = isDatas($this->user_info, 'act_lottery_end_at')
  227. ?
  228. strtotime($this->user_info['act_lottery_end_at'])
  229. :
  230. ZERO;
  231. $lotteryinfo = isDatas($this->act_rule, 'lottery') ? $this->act_rule['lottery'] : null;
  232. if ($lotteryinfo) {
  233. $start_at = strtotime($lotteryinfo['start_at']);
  234. $end_at = strtotime($lotteryinfo['end_at']);
  235. // 只有等活动快要过期的时候更新最新的活动
  236. if ($act_lottery_end_at > ZERO && $act_lottery_end_at > $end_at) {
  237. // 到时候可以按照需求自定义初始的值
  238. $data = [
  239. 'act_lottery_end_at' => $end_at,
  240. 'surplus_lottery_num' => ZERO,
  241. 'login_frequency ' => ZERO,
  242. 'login_successive_day' => ZERO,
  243. 'login_total_day ' => ZERO,
  244. 'pay_frequency ' => ZERO,
  245. 'pay_amount ' => ZERO,
  246. 'lottery_total ' => ZERO,
  247. ];
  248. $get_prepare = [
  249. 'appid' => $this->appid,
  250. 'openid' => $this->openid,
  251. 'channel_id' => $this->channel_id,
  252. 'sid' => $this->sid,
  253. ];
  254. $ret = $this->service->updateUserInfo($data, $get_prepare);
  255. if ($ret) {
  256. return true;
  257. }
  258. log_message::info("editRoleLotteryEndAt up false");
  259. }
  260. }
  261. log_message::info("editRoleLotteryEndAt data false");
  262. return false;
  263. }
  264. /***
  265. * 是否存在游戏
  266. * @return bool
  267. */
  268. public function isApp()
  269. {
  270. if ($this->appid == isDatas($this->config_data, 'appid')) {
  271. return true;
  272. }
  273. return false;
  274. }
  275. /***
  276. * 是否存在渠道
  277. * @return bool
  278. */
  279. public function isChannel()
  280. {
  281. if (strripos($this->config_data['channel_code'], $this->channel)) {
  282. return true;
  283. }
  284. log_message::info('channel is null false !!!');
  285. return false;
  286. }
  287. /***
  288. * 活动状态是否已经开启
  289. * @param $status
  290. * @return bool
  291. */
  292. public function isActivitesStatus($status = null)
  293. {
  294. $status = $this->config_data['act_status'];
  295. if (isset($status)) {
  296. if ($status == ACTIVITES_STATUS) {
  297. return true;
  298. }
  299. log_message::info(Utils::sendResults('status is 0 '));
  300. return false;
  301. }
  302. log_message::info(Utils::sendResults('status is null '));
  303. return false;
  304. }
  305. /***
  306. * 开启活动列表
  307. */
  308. public function activiti_menu_list()
  309. {
  310. //echo '{"result":1,"msg":"成功","val":null,"list":
  311. //{"inviting":1,"invited":1,"newgift":1,"lottery":1,"backuser":1,"ranking":1}}';
  312. // $messige, $result_dat = null, $status = null
  313. $data = array('inviting' => ZERO, 'invited' => ZERO, 'newgift' => ZERO, 'backuser' => ZERO);
  314. if (isDatas($this->act_rule)) {
  315. foreach ($this->act_rule as $act_key => $act_val) {
  316. $start_at = strtotime($act_val['start_at']);
  317. $end_at = strtotime($act_val['end_at']);
  318. if ((strtotime($this->time) >= $start_at) && (strtotime($this->time) <= $end_at)) {
  319. $data[$act_key] = ONE;
  320. } else {
  321. $data[$act_key] = ZERO;
  322. }
  323. }
  324. return Utils::sendResults("活动信息回调成功", ["list" => $data], SUCCESS);
  325. }
  326. log_message::info("activiti menu list is null");
  327. return Utils::sendResults("活动不存在");
  328. }
  329. /***
  330. * 是否存在活动类型
  331. * @param null $act_type
  332. * @return bool
  333. */
  334. public function isActivityType($act_type = null)
  335. {
  336. $this->act_rule = json_decode($this->config_data['act_rule'], true);
  337. if (isset($this->act_rule) && count($this->act_rule) > ZERO) {
  338. if (isset($this->act_rule[$this->act_type])) {
  339. return $this->act_rule[$this->act_type];
  340. }
  341. log_message::info('不存在的活动类型...' . $this->act_rule[$this->act_type]);
  342. return false;
  343. }
  344. log_message::info('不存在的活动类型2');
  345. return false;
  346. }
  347. /***
  348. * 是否在正常的区服范围
  349. * @param $role_sid
  350. * @param $min_sid
  351. * @param $max_sid
  352. * @return bool
  353. */
  354. public function isServer($role_sid = null)
  355. {
  356. $min_sid = $this->config_data['server_min'];
  357. $max_sid = $this->config_data['server_max'];
  358. $role_sid = $this->sid;
  359. if (isset($min_sid) && isset($max_sid)) {
  360. if ($role_sid >= $min_sid && $role_sid <= $max_sid) {
  361. return true;
  362. }
  363. log_message::info('不存在的区服');
  364. return false;
  365. }
  366. log_message::info('区服空');
  367. return false;
  368. }
  369. /**=================================
  370. * 活动去活动配置
  371. * @param $file_name
  372. * @return bool|mixed
  373. * =================================
  374. */
  375. public function isActivitesData()
  376. {
  377. if (!isBlank($this->config_data)) {
  378. return $this->config_data;
  379. }
  380. return false;
  381. }
  382. public function setConfig($data, $cache_key = null)
  383. {
  384. $cache_key = isBlank($cache_key) ? $this->cache_key : $cache_key;
  385. $ret = $this->service->setConfigCache($cache_key, $this->appid, $data);
  386. if ($ret) {
  387. return $ret;
  388. }
  389. return false;
  390. }
  391. /***
  392. * @param array $userdata
  393. * @return bool
  394. */
  395. public function initializeUserinfo($userdata = [])
  396. {
  397. $data = [
  398. 'uid' => $this->uid,
  399. 'gameid' => $this->gameid,
  400. 'login_at' => date('Ymd', time()),
  401. ];
  402. $data = isDatas($userdata) ? $userdata : $data;
  403. $this->user_info = $this->service->saveUserInfo($data);
  404. if ($this->user_info) {
  405. return $this->user_info;
  406. }
  407. return Utils::sendResults("初始化信息失败");
  408. }
  409. /**
  410. *初始用户抽奖基础数据if (strtotime($this->time) >= strtotime($act_endtime)) {
  411. * // 初始数据 set cache db
  412. */
  413. public function initializeLotteryInfo()
  414. {
  415. // 重置用户每日的实物上限次数
  416. self::$role_prize_uplimit = isDatas($this->user_info, 'lottery_up_limit')
  417. ?
  418. isDatas($this->user_info, 'lottery_up_limit')
  419. :
  420. ZERO;
  421. $this->respaceRoleUplimitdata();
  422. $file_name = $this->getWechatFileName();
  423. $config_data = $this->utils->getFileConfig($file_name);
  424. $config_rule = Utils::decodeDate(isDatas($config_data, 'act_rule'));
  425. $prize_info = ActivitySer::$prize_info;//$this->service->getCachePrizeInfo();
  426. $prize_respace_at = isDatas($prize_info)
  427. ?
  428. isDatas($prize_info[ONE], 'create_at')
  429. ?
  430. isDatas($prize_info[ONE], 'create_at') : null
  431. :
  432. null;
  433. $lottery = isDatas($config_rule, 'lottery');
  434. if ($lottery) {
  435. //$this->initializeHttpData('lottery');
  436. // 初始用户抽奖基础数据
  437. $start_at = isDatas($lottery, 'start_at');
  438. $end_at = isDatas($lottery, 'end_at');
  439. // 活动有效期
  440. if (strtotime($this->time) >= strtotime($start_at) && strtotime($this->time) <= strtotime($end_at)) {
  441. //加载抽奖数据重置玩家自身的配置数据 BUG
  442. $this->replaceLotteryData($start_at, $end_at, true);
  443. $this->isRespaceLottery($prize_respace_at);
  444. log_message::info("initializeLotteryInfo false");
  445. return true;
  446. }
  447. //Utils::sendResults("初始化信息失败");
  448. log_message::info("initializeLotteryInfo lottery times is false");
  449. return false;
  450. }
  451. log_message::info("initializeLotteryInfo lottery is null");
  452. return false;
  453. }
  454. public function isRespaceLottery($prize_respace_at)
  455. {
  456. $now_at = date(DATE_FORMAT_D, time());
  457. $prize_respace_at = date(DATE_FORMAT_D, strtotime($prize_respace_at));
  458. if (isDatas($prize_respace_at)) {
  459. if ($prize_respace_at != $now_at) {
  460. // get hgetAll prize info
  461. $datas = $this->service->byPrizeidInfo(null, true);
  462. $lotter_list = [];
  463. foreach ($datas as $key => $var) {
  464. $prizeOut = null;
  465. $prizeOut = json_decode($var, true);
  466. $prizeOut['frequency'] = ZERO;
  467. $prizeOut['create_at'] = $this->time;
  468. $lotter_list[] = $prizeOut;
  469. }
  470. //$this->service->replaceUpperLimit(); // 重置 LOTTERY DB
  471. //$prizedata = $this->service->getdbPrizeInfo(); // GET DB PRIZE INFO
  472. //$prizedata = ActivitySer::$prize_info;
  473. // $this->service->setCachePrizeInfo($prizedata); // respace db cache info
  474. if (isDatas($lotter_list)) {
  475. $this->service->setCachePrizeInfo($lotter_list);
  476. }
  477. }
  478. }
  479. log_message::info("RespaceLottery prize_respace_at is null ");
  480. return false;
  481. }
  482. /**
  483. * 排行榜数据获取
  484. */
  485. public function getRanking()
  486. {
  487. $data = null;
  488. $rank_url = Utils::config('rank_api');
  489. log_message::info($rank_url);
  490. $data = array(
  491. 'g' => 'WCNKH3c3TLdlo',
  492. 'noip' => ONE,
  493. 's' => $this->server_prefix . $this->sid,
  494. 'c' => $this->channel,
  495. 'type' => $this->ranking_type[$this->role_ranking_type]
  496. );
  497. log_message::info($rank_url['host'] . $this->channel);
  498. $url = $rank_url['host'] . $this->channel;
  499. $ret = Utils::decodeDate(Utils::send_request($url, $data));
  500. //$ret = json_decode($ret, true);
  501. if ($ret) {
  502. return Utils::sendResults("排行榜", ["list" => $ret], SUCCESS);
  503. }
  504. return Utils::sendResults("ranking is null");
  505. }
  506. /***
  507. * @return bool|false|string
  508. */
  509. public function getdbRankingInfo()
  510. {
  511. $data = [
  512. "appid" => 'cHQMG8RUm0vrA',
  513. "channel_code" => 'aiweiyou',
  514. ];
  515. $ret = $this->service->globalConfig($data);
  516. if ($ret) {
  517. $ranking_data = Utils::decodeDate(isDatas($ret, 'ranking_rule'));
  518. $limit_data = array_slice($ranking_data, ZERO, 20);
  519. return Utils::sendResults("排行榜", ["list" => $limit_data], SUCCESS);
  520. }
  521. return false;
  522. }
  523. /***
  524. *
  525. */
  526. public function ranKingVerif()
  527. {
  528. return;
  529. }
  530. /***
  531. * @return bool|string
  532. */
  533. public function rankingBack()
  534. {
  535. $this->service->redis->set('ttt', '哈哈');
  536. $data = $this->service->redis->get('ttt');
  537. return $data;
  538. }
  539. /***
  540. * @return mixed 废弃
  541. */
  542. public function prizeInfo()
  543. {
  544. $ret = $this->service->getPrizeInfo($this->appid);
  545. if (!$ret) {
  546. log_message::info('getPrizeInfo is false');
  547. return $ret;
  548. }
  549. log_message::info('getPrizeInfo is ok');
  550. return $ret;
  551. }
  552. /***
  553. * 重置抽奖配置信息
  554. * @return false|string
  555. */
  556. public function setLottryInfo()
  557. {
  558. $data = Utils::config('game_prize');
  559. $ret = $this->service->setCachePrizeInfo($data);
  560. if ($ret) {
  561. return Utils::sendResults('lottery config set ok', [], SUCCESS);
  562. }
  563. return Utils::sendResults('lottery config set false', FAILURE);
  564. }
  565. /***
  566. * @return array|bool|null
  567. */
  568. public function respaceLotteryInfo()
  569. {
  570. $ret = $this->service->getCachePrizeInfo();
  571. if ($ret) {
  572. return Utils::sendResults('lottery config set ok', $ret, SUCCESS);
  573. }
  574. return Utils::sendResults('lottery config set false');
  575. }
  576. /**
  577. * 抽奖配置
  578. */
  579. public function getLotteryConfig()
  580. {
  581. $pirze_lottery = null;
  582. $user_lotteres_num = isDatas($this->user_info, 'surplus_lottery_num')
  583. ?
  584. (int)isDatas($this->user_info, 'surplus_lottery_num')
  585. :
  586. ZERO;
  587. if ($user_lotteres_num <= ZERO) {
  588. return Utils::sendResults('抽取次数已用完!');
  589. }
  590. // 获取玩家次数
  591. // 返回抽取奖励信息 - 如果带有需要发货的实物需要返回信息提示玩家填写收货地址信息
  592. // 并每次抽完减去次数
  593. // 获取一个订单详情信息发货同事最好是能够通知给玩家一个订单号 后面可以追加物流信息
  594. // respace data ActivitySer::$prize_info
  595. $prize_arr = ActivitySer::$prize_info;
  596. // 每次前端页面的请求,PHP循环奖项设置数组,
  597. // 通过概率计算函数get_rand获取抽中的奖项id。
  598. // 将中奖奖品保存在数组$res['yes']中,
  599. // 而剩下的未中奖的信息保存在$res['no']中。中
  600. foreach ($prize_arr as $key => $val) {
  601. $arr[$val['prize_id']] = $val['rate'];
  602. }
  603. $rid = Utils::getPrizeRand($arr); //根据概率获取奖项id
  604. $pirze_lottery = $prize_arr[$rid];
  605. $prize_id = self::isParame($pirze_lottery, 'prize_id');
  606. $pirze_lottery = $this->service->setLotteryFrequency($prize_id);
  607. if (isDatas($pirze_lottery)) {
  608. if (is_string($pirze_lottery)) {
  609. $pirze_lottery = json_decode($pirze_lottery, true);
  610. }
  611. //if (isset($pirze_lottery['prize_type']) && $pirze_lottery['prize_type'] == ONE) {
  612. // 如果实物需要用户填写收货地址的,提前保存记录 备份等待用户填写收货地址防止对方篡改
  613. $prize_order = ZERO;
  614. $pirze_lottery = self::roleLotteryIntervention($prize_arr, $pirze_lottery);
  615. //$pirze_lottery = self::$role_prize_uplimit >= THREE ? $prize_arr[SIX] : $pirze_lottery;
  616. // 也可以作为用户的抽奖记录 再接再厉除外
  617. if (isDatas($pirze_lottery, 'upper_limit')) {
  618. $indata = $this->setpirzeConsigneeLog($pirze_lottery);
  619. $prize_order = isDatas($indata, 'prize_order')
  620. ?
  621. isDatas($indata, 'prize_order')
  622. :
  623. ZERO;
  624. }
  625. //}
  626. // 设置活动期间抽取次数
  627. $prize_id = $pirze_lottery['prize_id'];
  628. $pirze_name = $pirze_lottery['prize'];
  629. $prize_type = $pirze_lottery['prize_type'];
  630. $role_lottery_uplimit = $prize_type > ZERO ? ONE : ZERO;
  631. $this->lotteryDeduction($role_lottery_uplimit);
  632. $prize_desc = isDatas($pirze_lottery, 'prize_desc')
  633. ?
  634. isDatas($pirze_lottery, 'prize_desc')
  635. :
  636. null;
  637. $datalist = ["list" => [
  638. "result" => [[
  639. "LOGIN_ACCOUNT" => (int)$this->uid,
  640. "GIFT_NAME" => $pirze_name,
  641. "GIFT_INDEX" => intval($prize_id),
  642. "prize_type" => intval($prize_type),
  643. "prize_order" => $prize_order,
  644. "prize_desc" => $prize_desc
  645. ]]
  646. ]];
  647. // 统计抽奖日志
  648. $this->statLotteyLog($prize_id);
  649. return Utils::sendResults("抽奖成功", $datalist, SUCCESS);
  650. }
  651. log_message::info("抽奖数据为空!");
  652. return Utils::sendResults("prize is null ");
  653. }
  654. /**
  655. * 用户抽奖干预 上限每日三次
  656. * @param $prizeData
  657. * @param $lotteresPrize
  658. * @return mixed
  659. */
  660. public static function roleLotteryIntervention($prizeData, $lotteresPrize)
  661. {
  662. // 如果玩家抽实物已经三次 则干预
  663. if (self::$role_prize_uplimit >= THREE && $lotteresPrize['prize_type'] == ONE) {
  664. log_message::info("抽中实物了");
  665. return $prizeData[SIX];
  666. }
  667. return $lotteresPrize;
  668. }
  669. /***
  670. * 抽奖剩余总次数
  671. * @return int
  672. *
  673. * public function lotteryTotal()
  674. * {
  675. * $userinfo = $this->backUserData();
  676. * $lotter_total = isDatas($userinfo, 'surplus_lottery_num')
  677. * ?
  678. * (int)isDatas($userinfo, 'surplus_lottery_num')
  679. * :
  680. * ZERO;
  681. * return $lotter_total;
  682. * }*/
  683. /***
  684. * Lottery 初始的时候加载
  685. * 备份登录 次数 登录天数 连续登录天数
  686. * @param null $start_at
  687. * @param null $end_at
  688. * @param bool $initial default false if true is initial lottery data
  689. * @return bool|null
  690. */
  691. public function replaceLotteryData($start_at = null, $end_at = null, $initial = false)
  692. {
  693. /* $online_num = $this->service->getOnlineNum($this->appid, $this->channel_id, $this->sid, $this->openid, $start_at, $end_at);
  694. $pay_num = $this->service->getUserPayInfo($this->appid, $this->channel_id, $this->sid, $this->openid, $start_at, $end_at);*/
  695. $data = [];
  696. $merge_data = null;
  697. // 登录次数(活动期间)
  698. /* $data['login_frequency'] = $online_num['login_frequency'];
  699. // 连续登录几天(活动期间)
  700. $data['login_successive_day'] = $online_num['login_successive_day'];
  701. // 累计登录天数包括中段没有连续登录的 (活动期间)
  702. $data['login_total_day'] = $online_num['login_day'];
  703. // 充值次数(活动期间)
  704. $data['pay_frequency'] = $pay_num['pay_frequency'];
  705. // 充值金额 (活动期间)
  706. $data['pay_amount'] = $pay_num['pay_amount'];*/
  707. // 对应uid get api [uid/gameid]$url, $data, $coding = 'gbk', $refererUrl = '',
  708. // $method = 'POST',
  709. //$data['respce_lottery_total'] = $this->getRoleLotteryCont();
  710. $data['respce_lottery_total'] = $this->getRoleLotteryCont();
  711. if($this->uid == '1762120937' && $this->gameid == '38'){
  712. // $data['respce_lottery_total'] = 100000;
  713. }
  714. log_message::info("back user data", json_encode($data));
  715. $merge_data = array_merge($this->user_info, $data);
  716. // 处理递增次数
  717. $user_info = $this->service->updateUserlotteryInfo($merge_data);
  718. if ($user_info) {
  719. log_message::info('更新抽奖成功');
  720. $this->user_info = $user_info;
  721. return $this->user_info;
  722. }
  723. log_message::info('更新抽奖失败');
  724. return false;
  725. }
  726. public function getRoleLotteryCont()
  727. {
  728. $url = self::$role_lotteres_cont_url;
  729. $uid = $this->uid;
  730. $gameid = $this->gameid;
  731. $retluckycount = ZERO;
  732. $sign = md5($uid . '&' . $gameid . '&' . self::ROLE_LOTTERES_KEY);
  733. $ret = file_get_contents($url . '?uid=' . $uid . '&gameid=' . $gameid . '&sign=' . $sign);
  734. if (isDatas($ret)) {
  735. $data = json_decode($ret, true);
  736. $luckycount = isDatas($data, 'luckycount') ? isDatas($data, 'luckycount') : ZERO;
  737. log_message::info("getRoleLotteryCont is true " . $retluckycount);
  738. return $luckycount;
  739. }
  740. log_message::info("getRoleLotteryCont is null ");
  741. return false;
  742. }
  743. /***
  744. * 抽奖次数奖励
  745. */
  746. public function lucky_draw_total()
  747. {
  748. $lucky_draw_total = isDatas($this->user_info, 'lucky_draw_total')
  749. ?
  750. isDatas($this->user_info, 'lucky_draw_total')
  751. :
  752. ZERO;
  753. return $lucky_draw_total;
  754. }
  755. /**
  756. *
  757. */
  758. public function lucky_draw_total_prize()
  759. {
  760. $class_key = isDatas($_GET, 'class_key')
  761. ?
  762. isDatas($_GET, 'class_key')
  763. :
  764. null;
  765. $lucky_draw_total = isDatas($this->user_info, 'lucky_draw_total')
  766. ?
  767. isDatas($this->user_info, 'lucky_draw_total')
  768. :
  769. ZERO;
  770. // 3次抽取奖励 gift_title
  771. //{"result":1,"msg":"\u62b1\u6b49\uff0c\u9080\u8bf7\u6b21\u6570\u4e0d\u8db3","val":null,"list":{"gift":{"GIFT_NAME":"超级称号"}}}
  772. if ($class_key == "gift_title" && ($lucky_draw_total >= THREE && $lucky_draw_total < SIX)) {
  773. // 3 的时候不初始化 只有等6的时候初始化 0
  774. }
  775. // 6次抽取奖励 "gift_year"
  776. if ($class_key == "gift_year" && $lucky_draw_total >= SIX) {
  777. }
  778. }
  779. /***
  780. * 抽奖剩余次数扣取 累计抽取次数增加
  781. * @return bool
  782. */
  783. public function lotteryDeduction($role_lottery_uplimit = ZERO)
  784. {
  785. $user_info = $this->service->updateUserlotteryInfo($this->user_info, true, $role_lottery_uplimit);
  786. if ($user_info) {
  787. $this->user_info = $user_info;
  788. log_message::info("抽取用户抽取次数成功", SUCCESS);
  789. return true;
  790. }
  791. log_message::info("抽取用户抽取次数失败", FAILURE);
  792. return false;
  793. }
  794. /***
  795. * 添加用户收货地址
  796. */
  797. public function setpirzeConsigneeLog($prize)
  798. {
  799. $sign = $this->uid.$this->gameid.time().microtime();
  800. $prize_order = strtoupper(md5(md5($sign)));
  801. $prize_desc = isDatas($prize, 'prize_desc') ? isDatas($prize, 'prize_desc') : null;
  802. $prize_type = $prize['status'];
  803. $data = [
  804. 'uid' => $this->uid,
  805. 'gameid' => $this->gameid,
  806. 'prize_id' => $prize['prize_id'],
  807. 'prize_name' => $prize['prize'],
  808. 'create_at' => $this->time,
  809. 'prize_order' => $prize_order,
  810. 'prize_desc' => $prize_desc,
  811. 'prize_type' => $prize_type,
  812. 'date' => date('Ymd', time()),
  813. ];
  814. $ret = $this->service->savePirzeConsignee($data);
  815. if ($ret) {
  816. log_message::info("添加奖品信息成功");
  817. return $data;
  818. }
  819. log_message::info("添加奖品信息失败");
  820. return false;
  821. }
  822. /*public function initialUserLottery($act_endtime)
  823. {
  824. if (strtotime($this->time) >= strtotime($act_endtime)) {
  825. // 初始数据 set cache db
  826. }
  827. }*/
  828. /***
  829. * 更新用户抽奖地址
  830. * @return bool
  831. */
  832. public function updateShippingAddress()
  833. {
  834. $address = isDatas($_GET, 'consignee_address');
  835. //$user_nick_name = isDatas($this->request, 'consignee_name');
  836. $user_phone = isDatas($_GET, 'consignee_phone');
  837. $prize_order = isDatas($_GET, 'prize_order');
  838. $type = isDatas($_GET, 'type'); // 1 - 0
  839. $data = $this->byShippingList($prize_order);
  840. log_message::info("updateShippingAddress get data ", json_encode($_GET, JSON_UNESCAPED_UNICODE));
  841. if (isset($_GET['type']) && $type==ZERO)
  842. {
  843. $data = [
  844. 'uid' => $this->uid,
  845. 'gameid' => $this->gameid,
  846. 'type' => ONE,
  847. 'consignee_address' => $address,
  848. 'consignee_phone' => $user_phone,
  849. 'prize_order' => $prize_order,
  850. ];
  851. $ret = $this->service->editPirzeConsignee($data);
  852. if ($ret) {
  853. log_message::info("礼物已被更改领取...");
  854. return Utils::sendResults("add Address", $data, SUCCESS);
  855. }
  856. }
  857. if (!isset($_GET['type']) && isDatas($data)){
  858. return Utils::sendResults('该礼物已经领取', $data, SUCCESS);
  859. }
  860. return Utils::sendResults("update Shipping Address false", FAILURE);
  861. }
  862. public function byShippingList($order_id)
  863. {
  864. $data = $this->service->byuserPrizeOrderInfo($this->uid,$this->gameid, $order_id);
  865. if ($data) {
  866. log_message::info("shpping list is true ");
  867. return $data;
  868. }
  869. log_message::info("shpping list is null ");
  870. return false;
  871. }
  872. /***
  873. * 用户抽奖剩余次数
  874. * @return bool|false|string
  875. */
  876. public function getLotterySurplusNum()
  877. {
  878. $data = [];
  879. if ($this->user_info && !empty($this->user_info)) {
  880. $surplus_lottery_num = isDatas($this->user_info, 'surplus_lottery_num')
  881. ?
  882. isDatas($this->user_info, 'surplus_lottery_num')
  883. :
  884. ZERO;
  885. $data = [
  886. 'val' => $surplus_lottery_num,
  887. 'list' => ['chance_times' => $surplus_lottery_num]
  888. ];
  889. log_message::info("被调用", $data, SUCCESS);
  890. return Utils::sendResults("接口调用成功", $data, SUCCESS);
  891. }
  892. return Utils::sendResults("初始化失败", $data, FAILURE);
  893. }
  894. /***
  895. * 用户已经抽取次数
  896. * @return bool|false|string
  897. */
  898. public function getUserLotteryTotal()
  899. {
  900. //$userInfo = $this->service->saveUserInfo(['openid' => $this->openid]);
  901. if ($this->user_info && !empty($this->user_info)) {
  902. $lottery_total = is_array($this->user_info)
  903. ?
  904. isDatas($this->user_info, 'lottery_total')
  905. :
  906. json_decode($this->user_info, true)['lottery_total'];
  907. $lottery_total = empty($lottery_total)
  908. ?
  909. ZERO
  910. :
  911. $lottery_total;
  912. $data = [
  913. 'result' => SUCCESS,
  914. 'msg' => "接口调用成功",
  915. 'val' => $lottery_total,
  916. ];
  917. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  918. log_message::info("getUserLotteryTotal data info ", $data);
  919. return $data;
  920. }
  921. log_message::info("user lottery num is null", FAILURE);
  922. return false;
  923. }
  924. /***
  925. * 干预重置用户日抽奖的上限次数
  926. * @param $data
  927. */
  928. public function respaceRoleUplimitdata()
  929. {
  930. $online_at = isDatas($this->user_info, 'login_at');
  931. if ($online_at && $online_at != date('Ymd', time())) {
  932. $data = $this->user_info;
  933. $data['login_at'] = date('Ymd', time());
  934. $data['lottery_up_limit'] = ZERO;
  935. $db_res = $this->service->updateUserInfo($data, ['uid' => $this->uid,'gameid'=>$this->gameid]);
  936. $cache_res = $this->service->setCacheUserInfo($data);
  937. if ($db_res && $cache_res) {
  938. $this->user_info = $data;
  939. log_message::info('干预重置的玩家上限抽奖次数成功');
  940. return true;
  941. }
  942. log_message::info('干预重置的玩家上限抽奖次数失败');
  943. return false;
  944. }
  945. log_message::info("respaceUserInfo is time is true");
  946. }
  947. /***
  948. * 用户获奖记录 || 后期存放redis
  949. */
  950. public function userWinningRecord()
  951. {
  952. //$appid, $openid, $channel_id, $sid
  953. $data = $this->service->userPrizeInfo($this->uid,$this->gameid);
  954. if (isDatas($data)) {
  955. if (is_string($data)) {
  956. $data = json_decode($data, true);
  957. }
  958. foreach ($data as $var) {
  959. $rewards[] = [
  960. 'ID' => intval($var['prize_id']),
  961. 'GIFT_NAME' => $var['prize_name'],
  962. 'TIME' => $var['create_at'],
  963. 'prize_desc' => $var['prize_desc'],
  964. 'prize_order' => $var['prize_order'],
  965. 'prize_type' => intval($var['prize_type']),
  966. 'consignee_address' => $var['consignee_address'],
  967. 'consignee_phone' => $var['consignee_phone'],
  968. 'type' => (int)$var['type'],
  969. ];
  970. }
  971. $user_prize_list = [
  972. "result" => SUCCESS,
  973. "msg" => "接口调用成功",
  974. "val" => null,
  975. "list" => [
  976. "rewards" => $rewards
  977. ]
  978. ];
  979. return json_encode($user_prize_list, JSON_UNESCAPED_UNICODE);
  980. }
  981. return Utils::sendResults("暂无奖品可领取");
  982. }
  983. /***
  984. * 充值用户抽奖活动截止时间
  985. */
  986. public function closeUserLotteryEndAt()
  987. {
  988. }
  989. public function setdbprize()
  990. {
  991. $this->service->getCachePrizeInfo();
  992. }
  993. //~
  994. }