Files
liche/home/controllers/h5/syt/Live.php
T
dengbw 7db088250d stylive_519
stylive_519_2
2022-05-19 16:21:19 +08:00

735 lines
33 KiB
PHP

<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);
require_once APPPATH . 'controllers/h5/Wxapp.php';
class Live extends Wxapp
{
private $session;
private $secret;
private $uid = 0;
private $of_id = 5;//自媒体
private $of2_id = 55;//私域直播
public function __construct()
{
parent::__construct(2);
$this->secret = "syt_live_2022505";
$this->load->model('app/licheb/syt_live_model', 'mdSytLive');
$this->load->model('app/licheb/syt_live_biz_model', 'mdSytLiveBiz');
$this->load->model('app/licheb/syt_live_data_model', 'mdSytLiveData');
$this->load->model('app/app_weixin_users_model', 'mdWeixinUsers');
$this->load->model('app/licheb/app_licheb_users_model', 'mdLichebUsers');
$ukey = get_cookie('ukey');
if ($ukey) {
$this->session = json_decode(liche_authcode($ukey, 'DECODE', $this->secret), true);
$this->uid = $this->session['uid'];
}
//重置cookie
$reset = $this->input_param('reset');
if ($reset) {
set_cookie("ukey", "", 86400 * 1);
$this->session = array();
$this->uid = 0;
}
}
/**
* Notes:
* https://liche-dev.xiaoyu.com/h5/syt/live?reset=1
* https://www.liche.cn/h5/syt/live
* Created on: 2021/8/26 11:19
* Created by: dengbw
*/
function get()
{
$skey = $this->input->get('skey');
$this->load->library('MyEncryption');
if (!$skey) {//测试专用
$skey = "id=1";
$skey = $this->myencryption->base64url_encode($skey);
}
if (!$skey) {
throw new Hd_exception("参数错误", 400);
}
$params = $this->myencryption->base64url_decode($skey);
$id = intval($params['id']);//活动id
if (!$id) {
throw new Hd_exception("参数错误", 400);
}
$cf_uid = intval($params['cf_uid']);//来源狸车宝用户id
$biz_id = intval($params['biz_id']);//门店id
//echo json_encode($params, JSON_UNESCAPED_UNICODE);
$re = $this->mdSytLive->get(array("id" => $id, "status" => 1));
if (!$re) {
throw new Hd_exception("直播不存在", 400);
}
$checkua = checkua();
$sign_package = [];
$url = http_host_com('home') . "/h5/syt/live?skey={$skey}";
if ($checkua == 'wxwork') {//企业微信
$this->load->library('wx_qyapi_agent', ['app' => 'lichene_1000024']);
$this->load->library('wx_qyapi', ['app' => 'lichene']);
$this->set_auth_wxwork($url);
$sign_package = $this->wx_qyapi->getSignPackage();
} else if ($checkua == 'wx') {//微信
$this->set_auth($url);
$this->load->library('Jssdk');
$jssdk = new Jssdk('liche');
$sign_package = $jssdk->getSignPackage();
}
$live_user = ['userId' => 888888888, 'userName' => '私域直播', 'pic' => '//livestatic.videocc.net/assets/wimages/missing_face.png'];
if ($this->uid) {
if ($checkua == 'wxwork') {
$re_wu = $this->mdLichebUsers->get(['id' => $this->uid]);
if ($re_wu) {
$re_wu['unionid'] && $live_user['userId'] = $re_wu['unionid'];
$live_user['userName'] = $re_wu['nickname'] ? $re_wu['nickname'] : $re_wu['uname'];
$re_wu['headimg'] && $live_user['pic'] = $re_wu['headimg'];
}
} else if ($checkua == 'wx') {
$re_wu = $this->mdWeixinUsers->get(['id' => $this->uid]);
if ($re_wu) {
$re_wu['unionid'] && $live_user['userId'] = $re_wu['unionid'];
$re_wu['nickname'] && $live_user['userName'] = $re_wu['nickname'];
$re_wu['headimg'] && $live_user['pic'] = $re_wu['headimg'];
////更新参数
// $jsondata = $re_wu['jsondata'] ? json_decode($re_wu['jsondata'], true) : '';
// $jsondata['live'] = $params;
// $this->mdWeixinUsers->update(['jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE)], ['id' => $this->uid]);
}
}
if ($this->session['biz_id']) {//门店人员
$biz_id = $this->session['biz_id'];
$cf_uid = $this->session['biz_uid'];
}
}
$share_skey = "id=" . $id . "&cf_uid=" . $cf_uid . '&biz_id=' . $biz_id;
//echo '<br>share_skey:' . $share_skey;
$share_url = http_host_com('home') . "/h5/syt/live?skey=" . $this->myencryption->base64url_encode($share_skey);
//echo '<br>share_url:' . $share_url;
$title = $re['title'];
$share_img = $share_desc = $rule_content = '';
if ($re['jsondata']) {
$jsondata = json_decode($re['jsondata'], true);
$jsondata['share_img'] && $share_img = build_qiniu_image_url($jsondata['share_img']);
$jsondata['share_desc'] && $share_desc = $jsondata['share_desc'];
$jsondata['rule'] && $rule_content = str_replace("\n", "<br>", $jsondata['rule']);
}
$winInfo = $prizeList = [];
$prizes = $re['prizes'] ? json_decode($re['prizes'], true) : [];
foreach ($prizes['list'] as $key => $value) {
$prizeList[$value['id']] = $value;
}
$res_data = $this->mdSytLiveData->select(['l_id' => $id, 'type' => 2], 'id DESC', 1, 15, 'jsondata');
foreach ($res_data as $key => $value) {
if ($value['jsondata']) {
$jsondata_l = json_decode($value['jsondata'], true);
$prize_name = $prizeList[$jsondata_l['prize_id']]['title'];
$mobile = mobile_asterisk($jsondata_l['mobile']);
$winInfo[] = ['title' => $mobile . '领取了盲盒,开出了' . $prize_name];
}
}
$info['id'] = $id;
$info['session_id'] = $re['session_id'];
$info['l_status'] = $re['l_status'];
$info['l_status_name'] = $this->mdSytLive->lStatusAry($re['l_status']);
$info['cf_uid'] = $cf_uid;
$info['biz_id'] = $biz_id;
$info['biz_uid'] = intval($this->session['biz_uid']);
$info['checkua'] = $checkua;
$info['winInfo'] = $winInfo;
$info['banner'] = $re['img'] ? build_qiniu_image_url($re['img']) : '';
$info['rule'] = ['title' => '活动规则', 'content' => $rule_content];
$info['isOpen'] = false;//是否已开奖
$info['lottery'] = false;//是否已中奖
$info['e_time'] = 15;//距离开奖时间
$info['serviceCode'] = 'http://www.xmfish.com/images/chanel/www/index2/b_mobile.jpg';
$info['msgMyPrize'] = ['title' => '', 'lottery' => ['title' => '', 'img' => '', 'name' => '', 'mobile' => '',
'time' => ['title' => '', 'content' => ''], 'other' => ['title' => '注意事项', 'content' => '您还未登记信息!']]];
//检查是否试中奖
$re_data = $this->mdSytLiveData->get(['l_id' => $id, 'type' => 2, 'if_biz' => $this->session['biz_id'] ? 1 : 0
, 'uid' => $this->session['biz_uid'] ? $this->session['biz_uid'] : $this->uid]);
if ($re_data) {
$jsondata = $re_data['jsondata'] ? json_decode($re_data['jsondata'], true) : '';
$prize_id = intval($jsondata['prize_id']);
if ($prize_id) {
$prizes_item = $prizeList[$prize_id];
if ($prizes_item) {
$info['isOpen'] = true;//已开奖
$info['lottery'] = true;//已中奖
$info['e_time'] = 0;
$info['msgMyPrize'] = ['title' => '我的试驾礼', 'lottery' => ['title' => $prizes_item['title']
, 'img' => build_qiniu_image_url($prizes_item['img_value']), 'price' => $prizes_item['price']
, 'name' => $jsondata['name'], 'mobile' => $jsondata['mobile'],
'time' => ['title' => '活动时间', 'content' => $prizes['note']],
'other' => ['title' => '注意事项', 'content' => '']]];
}
}
}
//直播参数
$this->load->library('PolyvApi');
$polyv = new PolyvApi();
$liveSdk = $polyv->getLiveSdk();
$liveSdk['user'] = $live_user;
$info['liveSdk'] = $liveSdk;
//分享
$share = [
'title' => $title,
"img" => $share_img,
"desc" => $share_desc,
"url" => $share_url
];
if ($re['l_status'] == 1) {//加进入直播统计
$this->addLiveData(['type' => 1, 'l_id' => $id, 'cf_uid' => $cf_uid, 'biz_id' => $biz_id]);
}
$data = [
"info" => $info,
'_title' => $title,
"sign_package" => $sign_package,
"share" => $share,
'view' => 'h5/syt/live',
"checkua" => $checkua,
];
return $data;
}
function get_statistics()
{
$params = $this->input->get();
$id = intval($params['id']);
if (!$id) {
throw new Hd_exception('参数错误', 400);
}
if (!$this->session['biz_id']) {
throw new Hd_exception('无权限查看', 400);
}
$re = $this->mdSytLive->get(array("id" => $id, "status" => 1));
if (!$re) {
throw new Hd_exception("直播不存在", 400);
}
$this->load->model('live/Live_polyv_viewlog_model', 'mdPolyvViewlog');
$this->load->model("biz/biz_model", 'mdBiz');
$where = ['l_id' => $id, 'biz_id' => $this->session['biz_id']];
$re_biz = $this->mdSytLiveBiz->get($where);
//$watchs_num = $this->mdSytLiveData->count(['l_id' => $id, 'biz_id' => $this->session['biz_id'], 'type' => 1]);
$watchs = $enrolls = $playDuration = $watchs_num = 0;
if ($re_biz) {
$watchs = $re_biz['watchs'];
$enrolls = $re_biz['enrolls'];
}
$dynamic = $watchsList = $enrollsList = [];
$res = $this->mdSytLiveData->select($where, 'id DESC', 1, 15, 'uid,if_biz,type,c_time');
if ($res) {
$users_wx = $users_cb = $map_wx = $map_cb = [];
foreach ($res as $key => $value) {
if ($value['if_biz']) {
$users_cb[] = $value['uid'];
} else {
$users_wx[] = $value['uid'];
}
}
if ($users_wx) {
$str_ids = implode(',', array_unique($users_wx));
$map_wx = $this->mdWeixinUsers->map('id', 'nickname,uname', ["id in({$str_ids})" => null]);
}
if ($users_cb) {
$str_ids = implode(',', array_unique($users_cb));
$map_cb = $this->mdLichebUsers->map('id', 'nickname,uname', ["id in({$str_ids})" => null]);
}
foreach ($res as $key => $value) {
$type_name = $value['type'] == 2 ? ' 领取了试驾盒' : ' 进入了直播间';
if ($value['if_biz']) {
$name = $map_cb[$value['uid']]['nickname'] ? $map_cb[$value['uid']]['nickname'] : $map_cb[$value['uid']]['uname'];
} else {
$name = $map_wx[$value['uid']]['nickname'] ? $map_wx[$value['uid']]['nickname'] : $map_wx[$value['uid']]['uname'];
}
!$name && $name = '无用户名';
$dynamic[] = ['title' => $name . $type_name, 'time' => friendly_date($value['c_time'], 'mohu')];
}
}
if ($re['session_id']) {
$this->load->library('PolyvApi');
$polyv = new PolyvApi();
$liveParam = $polyv->getLiveParam();
$where_live = ['param3' => 'live', 'channelId' => $liveParam['channelId'], 'sessionId' => $re['session_id'],
"param1 in (select distinct unionid from lc_syt_live_data where l_id={$id} AND biz_id={$this->session['biz_id']} AND type=1)" => null];
$re_sum = $this->mdPolyvViewlog->sum('playDuration', $where_live);
$playDuration = ceil($re_sum['playDuration'] / $watchs);
$watchs_num = $this->mdPolyvViewlog->count($where_live);
}
$res_watchs = $this->mdSytLiveBiz->select(['l_id' => $id], 'watchs DESC,id ASC', 1, 10, 'biz_id,watchs');
if ($res_watchs) {
$str_ids = implode(',', array_unique(array_column($res_watchs, 'biz_id')));
$map_biz = $this->mdBiz->map('id', 'biz_name', ["id in({$str_ids})" => null]);
foreach ($res_watchs as $key => $value) {
$watchsList[] = ['ranking' => ($key + 1), 'name' => $map_biz[$value['biz_id']], 'num' => $value['watchs'] . '人'];
}
}
$res_enrolls = $this->mdSytLiveBiz->select(['l_id' => $id], 'enrolls DESC,id ASC', 1, 10, 'biz_id,enrolls');
if ($res_enrolls) {
$str_ids = implode(',', array_unique(array_column($res_enrolls, 'biz_id')));
$map_biz = $this->mdBiz->map('id', 'biz_name', ["id in({$str_ids})" => null]);
foreach ($res_enrolls as $key => $value) {
$enrollsList[] = ['ranking' => ($key + 1), 'name' => $map_biz[$value['biz_id']], 'num' => $value['enrolls'] . '人'];
}
}
$survey = [['title' => '累计观看(次)', 'num' => $watchs_num, 'url' => ''], ['title' => '人均观看(秒)', 'num' => $playDuration, 'url' => '']
, ['title' => '观看用户', 'num' => $watchs, 'url' => '/h5/syt/live/statistics_view?id=' . $id], ['title' => '报名用户(人)', 'num' => $enrolls, 'url' => '']];
$info = ['tabid' => 1, 'tab' => [['id' => 1, 'title' => '活动数据'], ['id' => 2, 'title' => '活动排名']],
'survey' => $survey, 'dynamic' => $dynamic, 'watchsList' => $watchsList, 'enrollsList' => $enrollsList];
$data = [
"info" => $info,
'_title' => '活动数据',
'view' => 'h5/syt/live_statistics',
];
return $data;
}
function get_statistics_view()
{
$params = $this->input->get();
$id = intval($params['id']);
if (!$id) {
throw new Hd_exception('参数错误', 400);
}
if (!$this->session['biz_id']) {
throw new Hd_exception('无权限查看', 400);
}
$re = $this->mdSytLive->get(array("id" => $id, "status" => 1));
if (!$re) {
throw new Hd_exception("直播不存在", 400);
}
$info = ['id' => $id, 'thead' => ['观看昵称', '观看次数', '停留时长'], 'isDataEnd' => false, 'isNoData' => false, 'list' => []];
$res = $this->statistics_view_list(['id' => $id, 'session_id' => $re['session_id']]);
$info['session_id'] = $re['session_id'];
$info['isNoData'] = $res['total'] == 0 ? true : false;
$info['isDataEnd'] = count($res['list']) == $res['total'] ? true : false;
$info['list'] = $res['list'];
$data = [
"info" => $info,
'_title' => '活动数据_观看用户',
'view' => 'h5/syt/live_statistics_view',
];
return $data;
}
function get_statistics_view_list()
{
$params = $this->input->get();
$data = $this->statistics_view_list($params);
$data['code'] = 200;
return $data;
}
function statistics_view_list($params = [])
{
$this->load->model('live/Live_polyv_viewlog_model', 'mdPolyvViewlog');
$page = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] ? intval($params['size']) : 10;
$where = ['l_id' => $params['id'], 'biz_id' => $this->session['biz_id']];
$total = $this->mdSytLiveData->count($where, 'unionid');
$list = [];
if ($total) {
$res = $this->mdSytLiveData->select($where, 'id DESC', $page, $size, 'distinct(unionid),uid,if_biz,unionid');
$users_wx = $users_cb = $map_wx = $map_cb = [];
foreach ($res as $key => $value) {
if ($value['if_biz']) {
$users_cb[] = $value['uid'];
} else {
$users_wx[] = $value['uid'];
}
}
if ($users_wx) {
$str_ids = implode(',', array_unique($users_wx));
$map_wx = $this->mdWeixinUsers->map('id', 'nickname,uname,headimg', ["id in({$str_ids})" => null]);
}
if ($users_cb) {
$str_ids = implode(',', array_unique($users_cb));
$map_cb = $this->mdLichebUsers->map('id', 'nickname,uname,headimg', ["id in({$str_ids})" => null]);
}
$this->load->library('PolyvApi');
$polyv = new PolyvApi();
$liveParam = $polyv->getLiveParam();
foreach ($res as $key => $value) {
if ($value['if_biz']) {
$name = $map_cb[$value['uid']]['nickname'] ? $map_cb[$value['uid']]['nickname'] : $map_cb[$value['uid']]['uname'];
$headimg = $map_cb[$value['uid']]['headimg'];
} else {
$name = $map_wx[$value['uid']]['nickname'] ? $map_wx[$value['uid']]['nickname'] : $map_wx[$value['uid']]['uname'];
$headimg = $map_wx[$value['uid']]['headimg'];
}
$where_live = ['param3' => 'live', 'channelId' => $liveParam['channelId'], 'sessionId' => $params['session_id']
, 'param1' => $value['unionid']];
$num = $this->mdPolyvViewlog->count($where_live);
$re_sum = $this->mdPolyvViewlog->sum('playDuration', $where_live);
$time = 0;
if ($re_sum['playDuration']) {
$format = $re_sum['playDuration'] >= 3600 ? 'H时i分s秒' : 'i分s秒';
$time = gmdate($format, $re_sum['playDuration']);
}
$tip = '';
$re = $this->mdSytLiveData->count(array_merge($where, ['type' => 2, 'unionid' => $value['unionid']]));
$re && $tip = '报名';
!$name && $name = '无用户名';
!$headimg && $headimg = '/img/h5/avatar.png';
$list[] = ['headimg' => $headimg, 'name' => $name, 'tip' => $tip, 'num' => $num, 'time' => $time];
}
}
return ['total' => $total, 'list' => $list];
}
/**
* Notes:修改直播状态
* Created on: 2022/5/19 11:57
* Created by: dengbw
* @return array
* @throws Hd_exception
*/
function post_l_status()
{
$params = $this->input->post();
$id = intval($params['id']);
$l_status = intval($params['l_status']);
$session_id = $params['session_id'];
if (!$id || !$session_id) {
throw new Hd_exception('参数错误', 400);
}
$re = $this->mdSytLive->get(array("id" => $id, "status" => 1));
if (!$re) {
throw new Hd_exception("直播不存在", 400);
}
$upData = ['l_status' => $l_status];
if (!$re['session_id']) {
$upData['session_id'] = $session_id;
} else {
$session_id = $re['session_id'];
}
$this->mdSytLive->update($upData, ["id" => $re['id']]);
$data = [
"code" => 200,
"msg" => '状态更新成功',
'l_status' => $l_status,
'l_status_name' => $this->mdSytLive->lStatusAry($l_status),
'session_id' => $session_id
];
return $data;
}
/**
* Notes:抽盲盒
* Created on: 2022/5/11 17:35
* Created by: dengbw
*/
function post_lottery()
{
$params = $this->input->post();
$id = intval($params['id']);
if (!$id) {
throw new Hd_exception('参数错误', 400);
}
$re = $this->mdSytLive->get(array("id" => $id, "status" => 1));
if (!$re) {
throw new Hd_exception("直播不存在", 400);
}
if ($re['l_status'] != 1) {
throw new Hd_exception($this->mdSytLive->lStatusAry($re['l_status']), 400);
}
$msgWin = ['title1' => '恭喜您', 'title2' => '获得试驾好礼啦'];
$msgMyPrize = [];
$prizes = $re['prizes'] ? json_decode($re['prizes'], true) : [];
if ($prizes && $prizes['list']) {
$msgWin['content'] = $prizes['note'];
$count = count($prizes['list']) - 1;
$num = rand(0, $count);
$prizes_item = $prizes['list'][$num];
if ($prizes_item) {
$msgWin['id'] = $prizes_item['id'];
$msgWin['name'] = $prizes_item['title'];
$msgWin['price'] = $prizes_item['price'];
$msgWin['img'] = $prizes_item['img_value'] ? build_qiniu_image_url($prizes_item['img_value']) : '';
$msgMyPrize = ['title' => '我的试驾礼', 'lottery' => ['title' => $msgWin['name']
, 'img' => $msgWin['img'], 'price' => $msgWin['price'], 'name' => '', 'mobile' => '',
'time' => ['title' => '活动时间', 'content' => $prizes['note']],
'other' => ['title' => '注意事项', 'content' => '您还未登记信息!']]];
}
} else {
throw new Hd_exception("暂无盲盒奖品", 400);
}
$data = [
"code" => 200,
"msg" => '抽奖成功',
'msgWin' => $msgWin,
'msgMyPrize' => $msgMyPrize,
];
return $data;
}
/**
* Notes:登记信息
* Created on: 2022/5/16 10:32
* Created by: dengbw
* @throws Hd_exception
*/
function post_enroll()
{
$params = $this->input->post();
$mobile = $params['mobile'];
$name = $params['name'];
if (!$name) {
throw new Hd_exception('请输入您的姓名', 400);
}
if (!mobile_valid($mobile)) {
throw new Hd_exception('请输入正确的手机号', 400);
}
$id = intval($params['id']);
$biz_id = intval($params['biz_id']);
$cf_uid = intval($params['cf_uid']);
$prize_id = intval($params['prize_id']);
$re = $this->mdSytLive->get(array("id" => $id, "status" => 1));
if (!$re) {
throw new Hd_exception("直播不存在", 400);
}
if ($re['l_status'] != 1) {
throw new Hd_exception($this->mdSytLive->lStatusAry($re['l_status']), 400);
}
$uid = $this->session['biz_uid'] ? $this->session['biz_uid'] : $this->uid;
$this->load->model("biz/biz_model", 'mdBiz');
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
$re_data = $this->mdSytLiveData->get(['type' => 2, 'l_id' => $id, 'if_biz' => $this->session['biz_id'] ? 1 : 0, 'uid' => $uid]);
if ($re_data) {
throw new Hd_exception('您已经登记过了', 400);
}
if (!$biz_id) {//无门店加入线索池
$cf_id = 35;
$this->load->model('receiver/receiver_clues_model', 'mdClues');
$re_clue = $this->mdClues->get(['out_id' => $id, 'cf_id' => $cf_id, 'mobile' => $mobile]);
if ($re_clue) {
throw new Hd_exception('您已经登记过', 400);
}
$add_data = [
'name' => $name,
'mobile' => $mobile,
'cf_id' => $cf_id,
'out_id' => $id,
'cf_platform' => 'h5',
'en_time' => date('Y-m-d H:i:s'),
'c_time' => time()
];
$this->mdClues->db->trans_begin();
$enroll_id = $this->mdClues->add($add_data);
$jsondata = ['name' => $name, 'mobile' => $mobile, 'enroll_id' => $enroll_id, 'enroll_type' => 2, 'prize_id' => $prize_id];
$id_d = $this->addLiveData(['type' => 2, 'l_id' => $id, 'cf_uid' => $cf_uid, 'biz_id' => $biz_id, 'jsondata' => $jsondata]);
if ($enroll_id && $id_d) {
$this->mdClues->db->trans_commit();
throw new Hd_exception('登记成功', 200);
} else {
$this->mdClues->db->trans_rollback();
throw new Hd_exception('登记失败,请重试', 400);
}
}
$re_cus = $this->mdCustomers->get(['biz_id' => $biz_id, 'mobile' => $mobile, 'of_id' => $this->of_id
, 'of2_id' => $this->of2_id, 't_id' => $id]);
if ($re_cus) {
throw new Hd_exception('您已经登记过', 400);
}
$city_id = $county_id = 0;
$re_biz = $this->mdBiz->get(['id' => $biz_id, 'status' => 1]);
if ($re_biz) {
$city_id = $re_biz['city_id'];
$county_id = $re_biz['county_id'];
}
$add_data = [
'name' => $name,
'mobile' => $mobile,
'biz_id' => $biz_id,
'city_id' => $city_id,
'county_id' => $county_id,
'cf_title' => '自有资源',
'of_id' => $this->of_id,
'of2_id' => $this->of2_id,
'admin_id' => $cf_uid,
't_id' => $id,
'p_time' => date('Y-m-d H:i:s'),
'c_time' => time()
];
$this->mdCustomers->db->trans_begin();
$enroll_id = $this->mdCustomers->add($add_data);
$jsondata = ['name' => $name, 'mobile' => $mobile, 'enroll_id' => $enroll_id, 'enroll_type' => 1, 'prize_id' => $prize_id];
$id_d = $this->addLiveData(['type' => 2, 'l_id' => $id, 'cf_uid' => $cf_uid, 'biz_id' => $biz_id, 'jsondata' => $jsondata]);
if ($enroll_id && $id_d) {
$this->mdCustomers->db->trans_commit();
throw new Hd_exception('登记成功', 200);
} else {
$this->mdCustomers->db->trans_rollback();
throw new Hd_exception('登记失败,请重试', 400);
}
}
/**
* Notes:直播数据
* Created on: 2022/5/16 10:31
* Created by: dengbw
* @param $params
* @return mixed
*/
private function addLiveData($params)
{
$add_data = [
'l_id' => $params['l_id'],
'unionid' => $this->session['unionid'],
'uid' => $this->session['biz_uid'] ? $this->session['biz_uid'] : $this->uid,
'cf_uid' => $params['cf_uid'],
'biz_id' => $params['biz_id'],
'type' => $params['type'],
'if_biz' => $this->session['biz_uid'] ? 1 : 0,
'c_time' => time()
];
$params['jsondata'] && $add_data['jsondata'] = json_encode($params['jsondata'], JSON_UNESCAPED_UNICODE);
$id = $this->mdSytLiveData->add($add_data);
if ($id && $params['biz_id']) {//更新门店数据
$re_biz = $this->mdSytLiveBiz->get(['l_id' => $params['l_id'], 'biz_id' => $params['biz_id']]);
if ($re_biz) {
if ($params['type'] == 2) {//试驾盲盒人
$this->mdSytLiveBiz->update(['enrolls = enrolls+1' => null], ['id' => $re_biz['id']]);
} else {//进直播间人
$re_count = $this->mdSytLiveData->count(['l_id' => $add_data['l_id'], 'biz_id' => $add_data['biz_id']
, 'uid' => $add_data['uid'], 'type' => $add_data['type'], 'if_biz' => $add_data['if_biz']]);
if ($re_count == 1) {//人只统计一次
$this->mdSytLiveBiz->update(['watchs = watchs+1' => null], ['id' => $re_biz['id']]);
}
}
} else {
$add_data = [
'l_id' => $params['l_id'],
'biz_id' => $params['biz_id'],
'c_time' => time()
];
if ($params['type'] == 2) {
$add_data['enrolls'] = 1;
} else {
$add_data['watchs'] = 1;
}
$this->mdSytLiveBiz->add($add_data);
}
}
return $id;
}
/**
* Notes:微信未登录授权登录
* Created on: 2020/10/28 21:07
* Created by: dengbw
* @param $url
*/
protected function set_auth($url)
{
if ($this->uid) {
return;
}
$this->load->helper('url');
$this->load->config('wechat');
$config = $this->config->item('liche');
$code = $this->input->get('code');
$auth = 1;//是否信息授权
$auth && $url .= "&auth={$auth}";
if ($code) {//授权码获取微信信息
$auth_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$config['appid']}&secret={$config['appSecret']}&code={$code}&grant_type=authorization_code";
$res = file_get_contents($auth_url);
$ret = json_decode($res, true);
$openid = $ret['openid'];
$unionid = $ret['unionid'];
if ($openid) {
$row_wechat = $this->mdWeixinUsers->get(array('openid' => $openid));
if (!$row_wechat) {//用户授权信息未记录到wechat
$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$ret['access_token']}&openid={$openid}&lang=zh_CN";
$res = file_get_contents($info_url);
$ret = json_decode($res, true);
$unionid = $ret['unionid'];
$add = array(
'nickname' => strval($ret['nickname']),
"headimg" => strval($ret['headimgurl']),
"unionid" => $unionid,
"openid" => $openid,
"app_id" => $this->app_id,
"c_time" => time()
);
$this->uid = $this->mdWeixinUsers->add($add);
if (!$this->uid) {
debug_log("[error]# " . $this->mdWeixinUsers->db->last_query(), __FUNCTION__, $this->log_dir);
}
} else {
!$unionid && $unionid = $row_wechat['unionid'];
$this->uid = $row_wechat['id'];
}
}
if ($this->uid) {
$biz_id = $biz_uid = 0;
if ($unionid) {
$re_licheb_users = $this->mdLichebUsers->get(array('unionid' => $unionid));
if ($re_licheb_users) {
$biz_id = intval($re_licheb_users['biz_id']);
$biz_uid = $re_licheb_users['id'];
}
}
$session = ['uid' => $this->uid, "unionid" => $unionid, "biz_id" => $biz_id, "biz_uid" => $biz_uid];
$ukey = liche_authcode(json_encode($session, JSON_UNESCAPED_UNICODE), 'ENCODE', $this->secret);
set_cookie("ukey", $ukey, 86400 * 1);
$this->session = $session;
}
} elseif ($auth) {//信息授权获取用户微信昵称/头像
$redirect_uri = urlencode($url);
$auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['appid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
redirect($auth_url);
} elseif (!$this->session) {//静默授权获取用户openid
$redirect_uri = urlencode($url);
$auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['appid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
redirect($auth_url);
}
}
/**
* Notes:授权获取企业员工UserId
* Created on: 2022/3/29 17:03
* Created by: dengbw
* @param string $url
* @return mixed
*/
private function set_auth_wxwork($url = '')
{
if ($this->uid) {
return;
}
$config = $this->wx_qyapi_agent->getConfig();
$code = $this->input->get('code');
if ($code) {//授权码获取微信信息
$this->load->library('mycurl');
$access_token = $this->wx_qyapi_agent->access_token();
$auth_url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$access_token}&code={$code}";
$result = $this->mycurl->httpGet($auth_url);
$result = json_decode($result, true);
if ($result['errcode'] == 0 && $result['UserId']) {
$re_wx = $this->wx_qyapi->get_external_contact(['url' => 'user_get', 'userid' => $result['UserId']]);
if ($re_wx['errcode'] == 0 && $re_wx['mobile']) {
$re_u = $this->mdLichebUsers->get(array('mobile' => $re_wx['mobile']));
if ($re_u) {
$this->uid = $biz_uid = $re_u['id'];
$biz_id = intval($re_u['biz_id']);
$unionid = $re_u['unionid'] ? $re_u['unionid'] : '';
$session = ['uid' => $this->uid, "unionid" => $unionid, "biz_id" => $biz_id, "biz_uid" => $biz_uid];
$ukey = liche_authcode(json_encode($session, JSON_UNESCAPED_UNICODE), 'ENCODE', $this->secret);
set_cookie("ukey", $ukey, 86400 * 1);
$this->session = $session;
}
}
}
} else {//静默授权获取用户openid
$this->load->helper('url');
$redirect_uri = urlencode($url);
$auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['corpid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
redirect($auth_url);
}
}
}