Files
liche/api/controllers/wxapp/bobing/Home.php
T
2021-09-01 16:57:09 +08:00

382 lines
15 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 Home 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->load->model('bobing/bobing_user_model', 'mdBobingUser');
$this->load->model('bobing/bobing_user_credit_model', 'mdBobingUserCredit');
$this->load->model('apporder/order_purchase_model', 'mdOrderPurchase');
$this->appConfig = $this->mdBobingUser->appConfig();
}
/**
* Notes:首页
* Created on: 2020/8/10 11:47
* Created by: dengbw
* @return array
* @throws Exception
*/
protected function get()
{
//$bo_nums = $this->mdBobingUser->boNums($this->appConfig['act_key']);
$bo_nums = 0;
$this->data['bodata'] = array('title' => "累计博饼次数", 'content' => $this->appConfig['content'], 'bo_nums' => $bo_nums);
$lucky_car[] = array('title' => '雷丁芒果开回家', 'img' => 'https://qs.haodian.cn/wechat_app/liche/bobing/2021/index-tip.jpg'
, 'url' => '/pages/buyCar/detail/index?id=13');
$this->data['lucky_car'] = $lucky_car;
$this->data['group'] = $this->appConfig['group'];
$this->data['title'] = $this->appConfig['title'];
return $this->data;
}
/**
* Notes:排名
* Created on: 2021/8/20 15:23
* Created by: dengbw
* @return array
*/
protected function get_ranking()
{
//日期列表-博饼
$this->data['dates'] = $this->pr_dates(false);
$this->data['rank'] = $this->pr_ranking_date(true);//今日幸运分
$this->data['winners'] = $this->pr_winners(true);
$this->data['title'] = $this->appConfig['title'];
return $this->data;
}
/**
* Notes:今日幸运分排名
* Created on: 2021/8/20 15:23
* Created by: dengbw
* @return array
*/
public function get_ranking_date()
{
$params = $this->input->get();
$this->data['rank'] = $this->pr_ranking_date(false, $params);
return $this->data;
}
/**
* Notes:总幸运分排名
* Created on: 2021/8/20 15:24
* Created by: dengbw
* @return array
*/
public function get_ranking_all()
{
$params = $this->input->get();
$this->data['rank'] = $this->pr_ranking_all(false, $params);
return $this->data;
}
/**
* Notes:中奖用户
* Created on: 2021/8/20 15:38
* Created by: dengbw
* @return array
*/
public function get_winners()
{
$params = $this->input->get();
$this->data['winners'] = $this->pr_winners(false, $params);
return $this->data;
}
private function pr_ranking_date($return_array = false, $params = array())
{
$page = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] ? intval($params['size']) : 100;
$bo_date = date('Y-m-d');
$list = array();
$where = array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'bo_date' => $bo_date, 'credit >' => 0);
$total = $this->mdBobingUserCredit->count($where);
if ($total) {
$res_c = $this->mdBobingUserCredit->select($where, 'credit desc,u_time asc', $page, $size, 'uid,credit');
if ($res_c) {
$users = array();//微信昵称
$uids = array_column($res_c, 'uid');
$re_u = $this->app_user_model->select(array('id in (' . implode(',', $uids) . ')' => null), 'id DESC', 0, 0, 'id,nickname,headimg');
foreach ($re_u as $key => $value) {
$users[$value['id']] = array('nickname' => $value['nickname'], 'headimg' => $value['headimg']);
}
foreach ($res_c as $key => $value) {
$user = $users[$value['uid']];
$list[] = array('id' => $key + 1, 'nickname' => $user['nickname'], 'headimg' => $user['headimg']
, 'credit' => $value['credit'] . '分');
}
}
}
if ($return_array) {
return $list;
}
return array('list' => $list, 'total' => $total);
}
private function pr_ranking_all($return_array = false, $params = array())
{
$page = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] ? intval($params['size']) : 100;
$list = array();
$where = array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'credit >' => 0);
$total = $this->mdBobingUser->count($where);
if ($total) {
$res_c = $this->mdBobingUser->select($where, 'credit desc,u_time asc', $page, $size, 'uid,credit');
if ($res_c) {
$users = array();//微信昵称
$uids = array_column($res_c, 'uid');
$re_u = $this->app_user_model->select(array('id in (' . implode(',', $uids) . ')' => null), 'id DESC', 0, 0, 'id,nickname,headimg');
foreach ($re_u as $key => $value) {
$users[$value['id']] = array('nickname' => $value['nickname'], 'headimg' => $value['headimg']);
}
foreach ($res_c as $key => $value) {
$user = $users[$value['uid']];
$list[] = array('id' => $key + 1, 'nickname' => $user['nickname'], 'headimg' => $user['headimg']
, 'credit' => $value['credit'] . '分');
}
}
}
if ($return_array) {
return $list;
}
return array('list' => $list, 'total' => $total);
}
private function pr_winners($return_array = false, $params = array())
{
$dates = $this->pr_dates(false);
$date_default = $dates ? $dates[0] : date('Y-m-d');
$bo_date = $params['date'] ? $params['date'] : $date_default;
//$bo_date = '2021-08-19';
$list = array();
$where = array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'bo_date' => $bo_date, 'lottery' => 1);
$total = $this->mdBobingUserCredit->count($where);
if ($total) {
$res_c = $this->mdBobingUserCredit->select($where, 'credit desc,u_time asc', 1, 10, 'uid,credit');
if ($res_c) {
$users = array();//微信昵称
$uids = array_column($res_c, 'uid');
$re_u = $this->app_user_model->select(array('id in (' . implode(',', $uids) . ')' => null), 'id DESC', 0, 0, 'id,nickname,headimg');
foreach ($re_u as $key => $value) {
$users[$value['id']] = array('nickname' => $value['nickname'], 'headimg' => $value['headimg']);
}
foreach ($res_c as $key => $value) {
$user = $users[$value['uid']];
$list[] = array('id' => $key + 1, 'nickname' => $user['nickname'], 'headimg' => $user['headimg']
, 'credit' => $value['credit'] . '分');
}
}
}
if ($return_array) {
return $list;
}
return array('list' => $list, 'total' => $total);
}
/**
* Notes:规则
* Created on: 2021/8/19 17:08
* Created by: dengbw
* @return array
*/
protected function get_rule()
{
$this->data['rule'] = $this->appConfig['rule'];
$this->data['title'] = $this->appConfig['title'];
return $this->data;
}
/**
* Notes:礼品
* Created on: 2021/8/19 17:45
* Created by: dengbw
* @return array
*/
protected function get_gift()
{
$this->data['gift'] = $this->appConfig['gift'];
$this->data['title'] = $this->appConfig['title'];
return $this->data;
}
/**
* Notes:我的
* Created on: 2021/8/27 10:29
* Created by: dengbw
* @return array
*/
protected function get_mine()
{
$credit = $car_gold = 0;
$re_u = $this->mdBobingUser->get(array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'uid' => $this->uid));
$tips = '';
if ($re_u) {
$credit = $re_u['credit'];
$car_gold = $re_u['car_gold'] + $re_u['buy_car_gold'];
$sum = $this->mdBobingUser->count(array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key']));
$row = $this->mdBobingUser->count(array('app_id' => $this->app_id, 'act_key' => $this->appConfig['act_key'], 'credit<=' => $credit));
$percentage = round($row / $sum * 100, 2);
$tips = '<div>您已打败全闽南<span style="color:#dd4223;">' . $percentage . '</span>的用户</div><div>继续邀请好友助力博饼,冲刺大奖吧~</div>';
}
$img_url = 'https://qs.haodian.cn/wechat_app/liche';
$menulist[] = array('title' => '9.9抢500购车金', 'url' => 'buy_car_gold', 'img' => $img_url . '/bobing/2021/icon-mine-1.png');
$menulist[] = array('title' => '预约试驾', 'url' => '/pages/buyCar/detail/index?id=13', 'img' => $img_url . '/bobing/2021/icon-mine-4.png');
$menulist[] = array('title' => '我的海报', 'url' => '/bobing/pages/game/invite/index', 'img' => $img_url . '/bobing/2021/icon-mine-2.png');
$menulist[] = array('title' => '联系客服', 'url' => '/pages/', 'img' => $img_url . '/bobing/2021/icon-mine-3.png');
$menulist[] = array('title' => '关于狸车', 'url' => 'lc://switchTab/pages/index/index', 'img' => $img_url . '/bobing/2021/icon-mine-5.png');
$data = array(
"title" => $this->appConfig['title'],
"credit" => array('title' => '幸运分', 'value' => $credit),
"car_gold" => array('title' => '购车金', 'value' => $car_gold),
"tips" => $tips,
"menulist" => $menulist
);
return $data;
}
/**
* Notes:购买购车金
* Created on: 2021/8/31 9:57
* Created by: dengbw
* @return array
*/
protected function get_buy_car_gold()
{
$where = array('app_id' => $this->appConfig['act_key'], 'app_uid' => $this->uid, 'item_id' => 1);
$re_p = $this->mdOrderPurchase->get($where);
if (!$re_p) {
$sid = create_order_no(350200);
$add_data = [
'app_id' => $this->appConfig['act_key'],
'app_uid' => $this->uid,
'sid' => $sid,
'item_id' => 1,
'item_title' => '9.9抢500购车金',
'item_num' => 1,
'type' => 3,
'item_price' => 9.9,
'total_price' => 0.01,//9.9
'uname' => $this->session['nickname'],
'mobile' => $this->session['mobile'],
'payway' => 1,
'status' => 1,
'status_detail' => 11,
'c_time' => time()
];
$order_id = $this->mdOrderPurchase->add($add_data);
$date = array('sid' => $sid, 'tips' => $order_id ? '' : '购买失败请重试');
} else {
$date = array('sid' => $re_p['sid'], 'tips' => $re_p['status'] == 2 ? '每人限购一次' : '');
}
return $date;
}
/**
* Notes:获取二维码
* Created on: 2021/8/27 16:34
* Created by: dengbw
* @return array
* @throws Hd_Exception
*/
protected function get_qrcode()
{
$scene = $this->input_param('scene');
$page = $this->input_param('page');
$width = $this->input_param('width');
$fig = $this->input_param('fig');
if (0 == strlen($scene)) {
throw new Hd_Exception('二维码参数必填', API_CODE_INVILD_PARAM);
}
return $this->qrcode($scene, $page, $width, $fig);
}
/**
* Notes:获取图片二维码
* Created on: 2021/8/27 16:33
* Created by: dengbw
* @param $scene
* @param string $page
* @param int $width
* @param string $fig
* @return array
* @throws Hd_Exception
*/
private function qrcode($scene, $page = '', $width = 0, $fig = 'liche')
{
$path = "{$page}?{$scene}";
$width && $path .= "{$width}";
if ($fig) {
$this->config->load('app', true, true);
$configs = $this->config->item('app');
$config = $configs[$fig];
if (!$config) {
debug_log("[error] " . __FUNCTION__ . ": config not exist;fig={$fig}", $this->log_file);
throw new Hd_Exception('请求的小程序不存在', API_CODE_INVILD_PARAM);
}
$wxconfig = $config['wx'];
} else {
$wxconfig = $this->wx_config;
$fig = $this->app_key;
}
$filename = "{$fig}/" . substr(md5($path), 8, 16);
$this->load->library('Hdwechat');
$hdwechat = new Hdwechat($wxconfig);
$ret = $hdwechat->qrcode($filename, $scene, $page, $width);
if (!$ret) {
throw new Hd_Exception('生成失败,稍后重试', API_CODE_FAIL);
}
$data = array('url' => $ret['url']);
return $data;
}
//日期列表
private function pr_dates($include_today = true)
{
$ttl = 60;
$mc = &load_cache();
$key_dates = 'dates' . $this->appConfig['act_key'] . '_' . date('Y-m-d') . '_' . $include_today;
$dates = $mc->get($key_dates);
if (!$dates) {
$date_end = $include_today ? strtotime(date('Y-m-d')) : strtotime(date('Y-m-d', strtotime('-1 day')));
$game_start_date = strtotime($this->appConfig['game_start_date']);
$game_start_end = strtotime($this->appConfig['game_end_date']);
$dates = array();
$do = true;
while ($do) {
if ($game_start_date <= $date_end && $game_start_date <= $game_start_end) {
$dates[] = date('Y-m-d', $game_start_date);
$game_start_date = strtotime('+1 day', $game_start_date);
} else {
$do = false;
}
}
$dates = array_reverse($dates);
$mc->save($key_dates, $dates, $ttl);
}
return $dates;
}
}