Files
liche/api/controllers/wxapp/bobing/Bobing.php
T
2021-09-01 09:49:40 +08:00

267 lines
11 KiB
PHP

<?php
defined('WXAPP_ITEMS') OR exit('No direct script access allowed');
ini_set('display_errors', 'On');
error_reporting(E_ERROR);
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
/**
* Notes:博饼页
* Created on: 2021/8/10 15:16
* Created by: dengbw
*/
class Bobing extends Wxapp
{
private $uid;
private $appConfig;
function __construct($inputs, $app_key)
{
parent::__construct($inputs, $app_key);
$this->login_white = '';//
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg = array();//授权微信信息
$this->majia_white = array('get');//超级管理员披上马甲可操作权限
$this->uid = $this->session['uid'];
//$this->uid = 4;
$this->load->model('bobing/bobing_user_model', 'mdBobingUser');
$this->load->model('bobing/bobing_user_credit_model', 'mdBobingUserCredit');
$this->load->model('bobing/bobing_logs_model', 'mdBobingLogs');
$this->appConfig = $this->mdBobingUser->appConfig();
}
/**
* Notes:首页
* Created on: 2020/8/10 11:47
* Created by: dengbw
* @return array
* @throws Exception
*/
protected function get()
{
$cf_uid = intval($this->input_param('cf_uid'));//要助力id
$this->uid == $cf_uid && $cf_uid = 0;
//$cf_uid = 14;
$where = array('act_key' => $this->appConfig['act_key'], 'app_id' => $this->app_id, 'uid' => $this->uid);
$re_u = $this->mdBobingUser->get($where);
if ($re_u) {
if (!$cf_uid && $re_u['if_kz'] == 0) {//更新开桌
$this->mdBobingUser->update(array("if_kz" => 1), $where);
}
} else {
$if_kz = $cf_uid ? 0 : 1;
$addUser = array_merge($where, array('if_kz' => $if_kz, 'c_time' => time()));
$idu = $this->mdBobingUser->add($addUser);
if (!$idu && $if_kz == 1) {
throw new Hd_exception('开桌失败,请重试', API_CODE_FAIL);
}
}
$share = array('title' => '狸车分享标题', 'content' => '<div>目前累计***幸运分,打败全闽南<span style="color:#fff10a">97.77%</span>的用户,</div>
<div>继续邀请好友助力博饼,</div><div>冲刺购物卡、苹果手机、汽车大奖~</div><div>添加小狸微信,还可立即获得<span style="color:#fff10a">288</span>幸运分!</div>'
, 'img' => 'https://qs.haodian.cn/wechat_app/liche/bobing/2021/share-tip.jpg', 'posters' => 'https://qs.haodian.cn/wechat_app/liche/bobing/2021/posterbg.jpg');
$result['valid_nums'] = $this->get_valid_nums($cf_uid);
$result['top_title'] = $this->get_top_title($cf_uid);
$result['users'] = $this->pr_users(true, array('cf_uid' => $cf_uid));
$result['logs'] = $this->pr_logs($cf_uid);
$result['share'] = $share;
return $result;
}
/**
* Notes:博一把
* Created on: 2021/8/10 15:18
* Created by: dengbw
*/
protected function put_bo()
{
$this->check_game_date();
$cf_uid = intval($this->input_param('cf_uid'));//要助力id
$this->uid == $cf_uid && $cf_uid = 0;
//$cf_uid = 14;
$this->load->library('bobing/bo');
$this->bo->uid = $this->uid;
$valid_nums = $this->get_valid_nums($cf_uid);//获取博饼剩余数
$this->bo->valid_nums = $valid_nums['value'];
$nickname = '';
if ($cf_uid) {//助力博
$this->bo->cf_uid = $cf_uid;
$result = $this->bo->zl_bo();
$reU = $this->app_user_model->get(array('id' => $this->uid));
$reU['nickname'] && $nickname = $reU['nickname'];
} else {//开桌博
$result = $this->bo->kz_bo();
}
//status 1开桌博饼次数用完2助力数用完
if ($result['status']) {//返回错误信息
if ($result['status'] == API_CODE_FAIL) {
throw new Hd_exception($result['content'], $result['status']);
}
return $result;
}
$status = 0;
$data['title'] = $result['title'];
$data['content'] = $result['content'];
//status 3抽到购车金4抽到红包
$log = $cf_uid ? $nickname . "为桌长博到{$result['credit']}幸运分" : "桌长博到{$result['credit']}幸运分";
if ($result['car_gold'] > 0) {
$status = 3;
$data['car_gold'] = $result['car_gold'];
$log .= $cf_uid ? "{$result['car_gold']}元购车金" : ",额外幸运地博到{$result['car_gold']}元购车金";
} else if ($result['hong_bao'] > 0) {
$status = 4;
$data['hong_bao_url'] = http_host_com('home') . "/h5/hongbao?id={$result['lid']}&uid={$this->uid}";
$log .= ",额外幸运地博到一个现金红包";
}
$data['log'] = $log;
$data['status'] = $status;
$valid_nums['value'] = $this->bo->valid_nums <= 0 ? 0 : $this->bo->valid_nums - 1;
$data['dices'] = $result['dices'];
$data['level'] = $result['level'];
$data['bo_title'] = $result['level_name'] . ' + ' . $result['credit'];
$data['valid_nums'] = $valid_nums;
$data['top_title'] = $this->get_top_title($cf_uid);
return $data;
}
protected function get_users()
{
$params = $this->input->get();
$this->data['users'] = $this->pr_users(false, $params);
return $this->data;
}
/**
* Notes:获取博饼剩余数
* Created on: 2021/8/17 10:24
* Created by: dengbw
* @param $cf_uid
* @return array
*/
private function get_valid_nums($cf_uid)
{
if ($cf_uid) {
$zl_nums = $this->mdBobingLogs->count(array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key']
, 'cf_uid' => $this->uid, 'type' => 1));
$zl_nums = intval($this->appConfig['zl_nums'] - $zl_nums);//剩下助力博饼数
$valid_nums = array('title' => '剩余助力次数:', 'value' => $zl_nums <= 0 ? 0 : $zl_nums);
} else {
$kz_nums = $this->mdBobingLogs->count(array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key']
, 'uid' => $this->uid, 'type' => 0));
$kz_nums = intval($this->appConfig['kz_nums'] - $kz_nums);//剩下开桌博饼数
$valid_nums = array('title' => '剩余博饼次数:', 'value' => $kz_nums <= 0 ? 0 : $kz_nums);
}
return $valid_nums;
}
/**
* Notes:博饼顶部标题
* Created on: 2021/8/17 10:14
* Created by: dengbw
* @return array
*/
private function get_top_title($cf_uid)
{
if ($cf_uid) {
$reU = $this->app_user_model->get(array('id' => $cf_uid));
$top_title = $reU['nickname'] ? $reU['nickname'] . '的桌子' : '未知用户的桌子';
} else {
$credit = $ranking = 0;
$where = array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'bo_date' => date('Y-m-d'));
$re_c = $this->mdBobingUserCredit->get(array_merge($where, array('uid' => $this->uid)));
$re_c && $credit = $re_c['credit'];
$ranking = $this->mdBobingUserCredit->count(array_merge($where, array('credit>=' => $credit, 'uid<>' => $this->uid))) + 1;//排名
$ranking > 100 && $ranking = '100名外';
$top_title = "今日博饼分:{$credit} | 今日排名:{$ranking}";
}
return $top_title;
}
private function pr_users($return_array = false, $params = array())
{
$page = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] ? intval($params['size']) : 7;
$cf_uid = intval($params['cf_uid']);
//$cf_uid = 14;
$list = array();
if ($cf_uid) {
$where = array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'uid' => $cf_uid, 'type' => 1);
} else {
$where = array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'uid' => $this->uid, 'type' => 1);
}
$total = $this->mdBobingLogs->count($where, 'distinct(cf_uid)');
if ($total) {
$res_u = $this->mdBobingLogs->select($where, 'id DESC', $page, $size, 'distinct(cf_uid)');
if ($res_u) {
$uids = implode(',', array_column($res_u, 'cf_uid'));
$list = $this->app_user_model->select(array('id in (' . $uids . ')' => null), 'id DESC', 0, 0, 'nickname,headimg');
}
}
if ($return_array) {
return $list;
}
return array('list' => $list, 'total' => $total);
}
/**
* Notes:获取博饼记录
* Created on: 2021/8/19 11:16
* Created by: dengbw
* @param $cf_uid
* @return array
*/
private function pr_logs($cf_uid)
{
//$cf_uid = 14;
$logs = array();
if ($cf_uid) {
$where = array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'uid' => $cf_uid);
} else {
$where = array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'uid' => $this->uid);
}
$res_l = $this->mdBobingLogs->select($where, 'id DESC', 1, 7, 'credit,hong_bao,car_gold,type,uid,cf_uid');
if ($res_l) {
$uids = array_column($res_l, 'cf_uid');
array_unique($uids);
$users2 = $this->app_user_model->select(array('id in (' . implode(',', $uids) . ')' => null), 'id DESC', 0, 0, 'id,nickname');
$nicknames = array();//微信昵称
foreach ($users2 as $key => $value) {
$nicknames[$value['id']] = $value['nickname'] ? $value['nickname'] : '****';
}
foreach ($res_l as $key => $value) {
$content = '';
if ($value['type'] == 0) {
$content = "桌长博到{$value['credit']}幸运分";
$value['car_gold'] > 0 && $content .= "{$value['car_gold']}元购车金";
} else if ($value['type'] == 1) {
$content = $nicknames[$value['cf_uid']] . "为桌长博到{$value['credit']}幸运分";
$value['car_gold'] > 0 && $content .= "{$value['car_gold']}元购车金";
$value['hong_bao'] > 0 && $content .= ",额外博到现金红包";
} else if ($value['type'] == 2) {
$content = "桌长加企业微信得到{$value['credit']}幸运分";
}
$logs[] = $content;
}
}
return $logs;
}
/**
* Notes:检查博饼时间
* Created on: 2021/8/27 14:25
* Created by: dengbw
* @throws Hd_exception
*/
protected function check_game_date()
{
if ($this->appConfig['game_start_date'] > date('Y-m-d')) {
throw new Hd_exception('博饼未开始', API_CODE_FAIL);
}
if ($this->appConfig['game_end_date'] < date('Y-m-d')) {
throw new Hd_exception('博饼已结束', API_CODE_FAIL);
}
}
}