Files
liche/home/controllers/h5/syt/Special.php
T
xiaoyu 6a9316dd64 0507
2025-05-07 12:39:07 +08:00

654 lines
33 KiB
PHP

<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);
require_once APPPATH . 'controllers/h5/Wxapp.php';
class Special extends Wxapp
{
private $session;
private $secret;
private $uid = 0;
private $cf_id = 35;//私域直播
public function __construct()
{
parent::__construct(2);
$this->secret = "2021_syt_h5_1119";
//$this->load->helper('cookie');
$this->load->model('app/licheb/syt_activity_model', 'mdSytActivity');
$this->load->model('app/licheb/syt_activity_kpidata_model', 'mdSytActivityKpiData');
$this->load->model('app/app_weixin_users_model', 'mdWeixinUsers');
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
$this->load->model("biz/biz_model", 'mdBiz');
$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 * 30);
$this->session = array();
$this->uid = 0;
}
}
/**
* Notes:
* https://liche-dev.xiaoyu.com/h5/syt/special
* https://liche-dev.xiaoyu.com/h5/syt/special?skey=YV9pZD03JmNmX3VpZD0yMSZiaXpfaWQ9MiZjZl9zaGFyZT1SNU5JS1c
* https://www.liche.cn/h5/syt/special
* Created on: 2021/8/26 11:19
* Created by: dengbw
*/
function get()
{
$skey = $this->input->get('skey');
//$skey = "YV9pZD03JmNmX3VpZD0yMSZiaXpfaWQ9MiZjZl9zaGFyZT1SNU5JS1c";
if (!$skey) {
throw new Hd_exception("参数错误", 400);
}
$this->load->library('MyEncryption');
$param = $this->myencryption->base64url_decode($skey);
//$skey = "a_id=7&cf_uid=24&biz_id=2&cf_share=" . $this->myencryption->random_string(6);
//echo $this->myencryption->base64url_encode($skey);
//print_r($param);
//exit;
$a_id = intval($param['a_id']);//活动id
$cf_uid = intval($param['cf_uid']);//来源用户id
$biz_id = intval($param['biz_id']);//门店id
$cf_share = $param['cf_share'];//分享
$re_a = $this->mdSytActivity->get(array("id" => $a_id, "status" => 1));
if (!$re_a) {
throw new Hd_exception("活动不存在", 400);
}
$url = http_host_com('home') . "/h5/syt/special?skey={$skey}";
$this->set_auth($url);
$topic_ary = array('id' => $re_a['z_id'], 'a_id' => $a_id, '_title' => $re_a['title']);
$topic = $this->get_topic($topic_ary);
if (!$topic) {
throw new Hd_exception("专题不存在", 400);
}
if ($this->uid) {
$re_wu = $this->mdWeixinUsers->get(['id' => $this->uid]);
if ($re_wu) {//更新参数
$jsondata = $re_wu['jsondata'] ? json_decode($re_wu['jsondata'], true) : '';
$jsondata['syt'] = $param;
$this->mdWeixinUsers->update(['jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE)], ['id' => $this->uid]);
}
}
//一次性订阅
// $re_s = $this->mdSytSubscribemsg->get(array('a_id' => $a_id, 'uid' => $this->uid));
// if ($re_s) {
// $subscribemsg = '已订阅直播';
// } else {
// $this->load->config('wechat');
// $config = $this->config->item('liche');
// $sub_redirect_url = urlencode(http_host_com('home') . '/h5/syt/special/subscribemsg?skey=' . $skey
// . '&s_time=' . $topic['mCountdown']['b_s_time']);
// $subscribemsg = "https://mp.weixin.qq.com/mp/subscribemsg?action=get_confirm&appid={$config['appid']}&scene=0}&template_id=DO0B9IYYub1d0oNvy9czzGbe6_1EU8PQmnLEoDOcmXA&redirect_url={$sub_redirect_url}&reserved=test#wechat_redirect";
// }
// $topic['subscribemsg'] = $subscribemsg;
$topic['subscribemsg'] = '';
$a_img = $re_a['img'] ? build_qiniu_image_url($re_a['img']) : '';
$a_title = $re_a['title'] ? $re_a['title'] : '';
//微信分享
$this->load->library('Jssdk');
$jssdk = new Jssdk('liche');
$sign_package = $jssdk->getSignPackage();
$t_skey = $c_skey = '';
if ($biz_id) {//门店
$skey = "a_id=" . $a_id . "&cf_uid=" . $cf_uid . "&biz_id=" . $biz_id . "&cf_share=" . $this->myencryption->random_string(6);
$c_skey = "a_id=" . $a_id . "&cf_uid=" . $cf_uid . '&biz_id=' . $biz_id;
} else {
$skey = "a_id=" . $a_id . "&cf_uid=" . $cf_uid . "&cf_share=" . $this->myencryption->random_string(6);
$c_skey = "a_id=" . $a_id . '&cf_uid=' . $cf_uid;
}
//浏览
$this->set_kpi_score(array('a_id' => $a_id, 'cf_uid' => $cf_uid, 'biz_id' => $biz_id, 'kpi' => 'browse'));
//分享
$cf_share && $this->set_kpi_score(array('a_id' => $a_id, 'cf_uid' => $cf_uid, 'biz_id' => $biz_id, 'cf_share' => $cf_share, 'kpi' => 'share'));
$share_url = http_host_com('home') . "/h5/syt/special?skey=" . $this->myencryption->base64url_encode($skey);
$jsondata = $re_a['jsondata'] ? json_decode($re_a['jsondata'], true) : '';
$share_title = $jsondata['share_title'] ? $jsondata['share_title'] : '';
$posters = $jsondata['posters'] ? $jsondata['posters'] : '';
$share = array(
'title' => $a_title,
"img" => $a_img,
"desc" => $share_title,
"url" => $share_url
);
//渠道获取统计代码
$stat_code = "";
// if ($t_id) {
// $where = array('id' => $t_id);
// $row = $this->mdSytActivityTeam->get($where);
// $row['declaration'] && $stat_code = $row['declaration'];
// }
$topic['a_skey'] = $this->myencryption->base64url_encode("a_id=" . $a_id);
$topic['c_skey'] = $this->myencryption->base64url_encode($c_skey);
$topic['t_skey'] = $t_skey;
$topic['a_id'] = $a_id;
$topic['cf_uid'] = $cf_uid;
$topic['biz_id'] = $biz_id;
$topic['uid'] = $this->uid;
$topic['posters'] = $posters;
$topic['stat_code'] = $stat_code;
$data = array(
'_title' => $a_title,
'view' => 'h5/syt/special',
'topic' => $topic,
"sign_package" => $sign_package,
"share" => $share,
);
return $data;
}
private function get_topic($param = array())
{
$data = $list = $mButton = array();
$mCountdown = array('b_e_time' => 0, 'b_s_time' => 0);
$this->load->model('app/App_model', 'mdApp');
$this->load->model('topics/topics_model', 'mdTopics');
$this->load->model('topics/topic_modules_model', 'mdTopicModules');
$this->load->model('topics/topic_module_text_model', 'mdModuleText');
$this->load->model('topics/topic_module_ad_model', 'mdModuleAd');
$this->load->model('topics/topic_module_enroll_model', 'mdModuleEnroll');
$this->load->model('topics/topic_module_button_model', 'mdModuleButton');
// $this->load->model('topics/topic_module_countdown_model', 'mdModuleCountdown');
$re = $this->mdTopics->get(array('id' => $param['id'], 'app_id' => $this->app_id));
if (!$re) {
return $data;
}
$appConfig = array();
$mp_app_id = $mp_app_name = '';
$ebiz = 0;
if ($re['jsondata']) {
$topics_jsondata = $re['jsondata'] ? json_decode($re['jsondata'], true) : array();
if ($topics_jsondata['mp_app_id']) {
$appConfig = $this->mdApp->appConfig()[$topics_jsondata['mp_app_id']];
$mp_app_id = $topics_jsondata['mp_app_id'];
$mp_app_name = $appConfig['wx_kzh']['appname'] ? $appConfig['wx_kzh']['appname'] : '星师傅';
}
$ebiz = intval($topics_jsondata['ebiz']);
}
$nickname = $mobile = $msgisReg = '';
$module_ids = $re['module_ids'];
if ($module_ids) {
$where = is_numeric($module_ids) ? array('id' => $module_ids) : array("id in ({$module_ids})" => null);
$where['status'] = 1;
$total = $this->mdTopicModules->count($where);
if ($total) {
$select = 'id, type,jsondata';
$rows = $this->mdTopicModules->select($where, 'sort DESC', 0, 0, $select);
foreach ($rows as $row_topic) {
$mid = $row_topic['id'];
$type = $row_topic['type'];
$json_topic = json_decode($row_topic['jsondata'], true);
$title_img = "";
$title_icon = "";
if ($json_topic['title_img']) {
$title_img = build_qiniu_image_url($json_topic['title_img']);
} else {
$title_icon = "https://qs.liche.cn/wechat_app/xingxuanka/special/tt-tip2.png";
}
switch ($type) {
case "countdown"://倒计时
$module = $this->mdModuleCountdown->get(array('module_id' => $mid));
$jsondata = $module['jsondata'] ? json_decode($module['jsondata'], true) : array();
$b_s_time = $jsondata['time'] ? $jsondata['time'] : 0;
$b_e_time = strtotime($b_s_time) - time();
$b_e_time < 0 && $b_e_time = 0;
$mCountdown = array(
'title' => $module['title'] ? $module['title'] : '',
'b_title' => $jsondata['title'] ? $jsondata['title'] : '',
'b_mp_title' => $jsondata['mp_title'] ? $jsondata['mp_title'] : '',
'b_ghid' => $appConfig['ghid'] ? $appConfig['ghid'] : '',
'b_s_time' => $b_s_time,
'b_e_time' => $b_e_time,
);
break;
case "button"://按钮类型
$module = $this->mdModuleButton->get(array('module_id' => $mid));
$jsondata = $module['jsondata'] ? json_decode($module['jsondata'], true) : array();
$ghid = $url = '';
$url = $jsondata['url'] ? $jsondata['url'] : '';
$url_mini = $jsondata['url_mini'] ? $jsondata['url_mini'] : '';//在小程序里连接
if ($appConfig['ghid'] && (strstr($url, 'pages'))) {//跳转小程序
$ghid = $appConfig['ghid'];
}
$mButton = array(
'title' => $module['title'] ? $module['title'] : '',
'type' => $module['type'],
'b_url' => $url,
'b_url_mini' => $url_mini,
'b_title' => $jsondata['title'] ? $jsondata['title'] : '',
'b_img' => $jsondata['img'] ? build_qiniu_image_url($jsondata['img']) : '',
'b_color' => $jsondata['color'] ? $jsondata['color'] : '',
'b_ghid' => $ghid,
);
break;
case "text"://文本类型
$module = $this->mdModuleText->get(array('module_id' => $mid));
$list[] = array(
"type" => "text",
'id' => $mid,
'title' => $module['title'] ? $module['title'] : '',
"title_img" => $title_img,
"title_icon" => $title_icon,
"title_color" => $json_topic["title_color"] ? $json_topic["title_color"] : "#fff",//背景色,
"title_bgcolor" => $json_topic["title_bgcolor"] ? $json_topic["title_bgcolor"] : "#ee5b15",//背景色
'bg_img' => $json_topic['bg_img'] ? build_qiniu_image_url($json_topic['bg_img']) : "",
'bg_color' => $json_topic["bg_color"] ? $json_topic["bg_color"] : '#ff0',
"border_color" => strval($json_topic['border_color']),
'descrip' => $module['descrip'] ? $module['descrip'] : '',
'content' => $module['content'] ? $module['content'] : '',
'style' => 0,
'ads' => array(),
);
break;
case 'ad'://广告
$module = $this->mdModuleAd->get(array('module_id' => $mid));
$module_json = $module['img_json'] ? json_decode($module['img_json'], true) : array();
$ads = array();
foreach ($module_json as $ad) {
$url = strval($ad['link']);
$miniProgram = '';
$img = $ad['img'] ? build_qiniu_image_url($ad['img']) : '';
if ($appConfig['ghid'] && strstr($url, 'pages')) {//跳转小程序
$img_class = $module['style'] == 2 ? 'wp100 block bds-f3' : 'wp100 block';
$miniProgram = '<div class="relative">
<div class="absolute top-0 left-0 right-0 bottom-0 opacity-0 overflowhidden">
<wx-open-launch-weapp username="' . $appConfig['ghid'] . '" path="' . $url . '">
<template><button style="width:655px;height:681px;opacity:0"></button></template>
</wx-open-launch-weapp></div><img class="' . $img_class . '" src="' . $img . '" /></div>';
}
$is_item = false;
if (strpos($url, "wxapp/syt/item") !== false) {
$url .= "&__skey=" . $this->input_param('skey');
$is_item = true;
}
$ads[] = array(
'type' => 'ad',
'img' => $img,
'url' => $url ? $url : '',
'miniProgram' => $miniProgram,
'jump_type' => intval($ad['jump_type']),
'is_item' => $is_item,
'title' => $ad['title']
);
}
$title_img = "";
$title_icon = "";
if ($json_topic['title_img']) {
$title_img = build_qiniu_image_url($json_topic['title_img']);
} else {
$title_icon = "https://qs.liche.cn/wechat_app/xingxuanka/special/tt-tip2.png";
}
$list[] = array(
"type" => "ad",
'id' => $mid,
'title' => $module['title'] ? $module['title'] : '',
"title_img" => $title_img,
"title_icon" => $title_icon,
"title_color" => $json_topic["title_color"] ? $json_topic["title_color"] : "#fff",//背景色,
"title_bgcolor" => $json_topic["title_bgcolor"] ? $json_topic["title_bgcolor"] : "#fe7c35",//背景色
'bg_img' => $json_topic['bg_img'] ? build_qiniu_image_url($json_topic['bg_img']) : "",
'bg_color' => $json_topic["bg_color"] ? $json_topic["bg_color"] : (2 == $module['style'] ? '#FFF' : ''),
"border_color" => strval($json_topic['border_color']),
'descrip' => $module['descrip'] ? $module['descrip'] : '',
'content' => '',
'style' => $module['style'],
'ads' => $ads,
);
break;
case 'enroll'://报名活动
$module = $this->mdModuleEnroll->get(array('module_id' => $mid));
//处理状态字段
$status = array("title" => "我要报名", "value" => 0);
//是否开始
if (1 == $module['type']) {
$s_time = strtotime($module['as_time']);
$e_time = strtotime($module['ae_time']);
} else {
$s_time = strtotime($module['es_time']);
$e_time = strtotime($module['ee_time']);
}
if ($s_time > time()) {
$status = array("title" => "报名尚未开始", "value" => 2);
} elseif ($e_time < time()) {
$status = array("title" => "报名已截止", "value" => -2);
}
//判断是否还能参加报名
if (0 == $status['value'] && $module['limit_num'] > 0) {
$where = array('a_id' => $param['a_id']);
$count = $this->mdSytActivityEnroll->count($where);
if ($count >= $module['limit_num']) {
$status = array("title" => "报名人数已满", "value" => 3);
}
}
if ($this->uid) {//是否报名
$re_enroll = [];
$this->session['mobile'] && $re_enroll = $this->mdCustomers->get(array('cf_id' => $this->cf_id, 't_id' => $param['a_id'], 'mobile' => $this->session['mobile']));
if ($re_enroll) {
$status = array("title" => "已报名", "value" => 1);
} else {
$reUsers = $this->mdWeixinUsers->get(array('id' => $this->uid));
$nickname = $reUsers['nickname'] ? $reUsers['nickname'] : '';
$mobile = $reUsers['mobile'] ? $reUsers['mobile'] : '';
}
}
$jsondata = $module['jsondata'] ? json_decode($module['jsondata'], true) : array();
$if_code = intval($jsondata['if_code']);//是否验证手机
if ($jsondata["show_type"] == 1) {
$msgisReg = array(
'title' => $module['title'] ? $module['title'] : '报名',
'if_code' => $if_code,
'status' => $status);
break;
}
$info = array(
// array("title" => "姓名", "field" => "input", "key" => "uname"),
// array("title" => "手机号", "field" => "mobile", "key" => "mobile"),
// array("title" => "备注", "field" => "input", "key" => "remark"),
);
$record = array();
$date_arr = array(
date('Y.m.d'),
date('Y.m.d', strtotime("-1 day")),
date('Y.m.d', strtotime("-2 day")),
date('Y.m.d', strtotime("-3 day"))
);
$num = 10;
$res = $this->mdCustomers->select(array('cf_id' => $this->cf_id, 't_id' => $param['a_id']), 'c_time desc', 1, $num);
if ($res) {
foreach ($res as $key2 => $val2) {
$nick_name = $val2['nickname'];
$len = mb_strlen($nick_name, 'UTF-8');
if ($len >= 3) {
$nick_name = mb_substr($nick_name, 0, 1, 'UTF-8') . '*' . mb_substr($nick_name, -1, 1, 'UTF-8');
} else {
$nick_name = mb_substr($nick_name, 0, 1, 'UTF-8') . '*';
}
$record[] = date('Y.m.d', $val2['c_time']) . ' ' . $nick_name . ' 报名了 ' . $param['_title'];
}
}
$num = $num - count($record);
if ($num) {
for ($i = 0; $i < $num; $i++) {
$record[] = $date_arr[array_rand($date_arr)] . ' ' . rand_name() . ' 报名了 ' . $param['_title'];
}
}
$list[] = array(
"type" => "enroll",
'id' => $mid,
'title' => $module['title'] ? $module['title'] : '',
"title_img" => $title_img,
"title_icon" => $title_icon,
"title_color" => $json_topic["title_color"] ? $json_topic["title_color"] : "#fff",//背景色,
"title_bgcolor" => $json_topic["title_bgcolor"] ? $json_topic["title_bgcolor"] : "#ee5b15",//背景色
'bg_img' => $json_topic['bg_img'] ? build_qiniu_image_url($json_topic['bg_img']) : "",
'bg_color' => $json_topic["bg_color"] ? $json_topic["bg_color"] : '#fff',
"border_color" => strval($json_topic['border_color']),
'descrip' => $module['descrip'] ? $module['descrip'] : '',
'content' => $module['content'] ? $module['content'] : '',
'style' => 0,
'if_code' => $if_code,
'ads' => array(),
'status' => $status,
'info' => $info,
'record' => $record
);
break;
default:
}
}
}
}
$data = array(
'id' => $param['id'],
'title' => $re['title'],
'bg_img' => build_qiniu_image_url($re['bg_img']),
'bg_color' => $re['bg_color'],
'ebiz' => $ebiz,
'list' => $list,
'name' => $nickname,
'telPhone' => $mobile,
'vCode' => '',
'codeState' => true,
'codeTx' => '获取验证码',
'isSubmiting' => false,
'mButton' => $mButton,
'mCountdown' => $mCountdown,
"bundleIntervalEvent" => '',
'msgisShowCode' => false,
'msgisRegShow' => false,
"qrcode" => '',
"logintelPhone" => '',
"loginvCode" => '',
"logincodeState" => true,
"logincodeTx" => '获取验证码',
"loginBundleIntervalEvent" => '',
'loginBackurl' => '',
"msgisLoginShowCode" => false,
"needLogin" => $reUsers['mobile'] ? false : true,
'mp_app_id' => $mp_app_id,
'mp_app_name' => $mp_app_name,
'msgisReg' => $msgisReg,
);
return $data;
}
/**
* Notes:生成海报
* Created on: 2022/1/20 16:35
* Created by: dengbw
* @return array
* @throws Hd_exception
*/
protected function get_poster()
{
$skey = $this->input_param('skey');
if (!$skey) {
throw new Hd_exception("参数错误", 400);
}
$this->load->library('MyEncryption');
$param = $this->myencryption->base64url_decode($skey);
$a_id = intval($param['a_id']);//活动id
$cf_uid = intval($param['cf_uid']);
$biz_id = intval($param['biz_id']);//门店id
$re_a = $this->mdSytActivity->get(array("id" => $a_id, "status" => 1));
if (!$re_a) {
throw new Hd_exception("活动不存在", 400);
}
$url = http_host_com('home') . "/h5/wxapp/syt/special/poster?skey={$skey}";
$this->set_auth($url);
if ($biz_id) {//门店
$skey = "a_id=" . $a_id . "&cf_uid=" . $cf_uid . "&biz_id=" . $biz_id . "&cf_share=" . $this->myencryption->random_string(6);
} else {
$skey = "a_id=" . $a_id . "&cf_uid=" . $cf_uid . "&cf_share=" . $this->myencryption->random_string(6);
}
$share_url = http_host_com('home') . "/h5/syt/special?skey=" . $this->myencryption->base64url_encode($skey);
$qr_cod = '/h5/syt/myqrcode/get?url=' . $share_url;
$posters = array();
if ($re_a['jsondata']) {
$jsondata = json_decode($re_a['jsondata'], true);
if ($jsondata['posters']) {//海报
foreach ($jsondata['posters'] as $key => $value) {
$posters[] = array("img" => build_qiniu_image_url($value['url']), "code" => $qr_cod,
"width" => $value['width'], "height" => $value['height']);
}
}
}
$this->load->library('Jssdk');
$jssdk = new Jssdk('liche');
$sign_package = $jssdk->getSignPackage();
$data = array(
'_title' => '生成海报',
'posters' => $posters,
'sign_package' => $sign_package,
'view' => 'h5/syt/special_poster',
);
return $data;
}
/**
* Notes:活动报名
* Created on: 2020/11/3 14:36
* Created by: dengbw
* @throws Hd_exception
*/
function post_enroll()
{
$params = $this->input->post();
$mobile = $params['mobile'];
$code = $params['code'];
$nickname = $params['nickname'];
$if_code = $params['if_code'];
if (!$nickname) {
throw new Hd_exception('请输入昵称', 400);
}
if (!mobile_valid($mobile)) {
throw new Hd_exception('手机号码不正确', 400);
}
$key = '';
if (!$if_code) {//0开启,1关闭
if (!$code) {
throw new Hd_exception('请输入验证码', 400);
}
$key = $this->redis_mcode . $mobile;
if (!in_array($mobile, $this->white_mobile)) {//校验手机号
$code_check = $this->lc_redis->get($key);
if (!$code || $code != $code_check) {
throw new Hd_exception("验证码不正确", 400);
}
}
}
$params['cf_platform'] = 'h5';
$params['mobile'] = $mobile;
$params['nickname'] = $nickname;
$params['uid'] = $this->uid;
$params['cf_id'] = $this->cf_id;
$this->load->library('entity/syt_entity');
$syt_entity = new Syt_entity(array('app_id' => $this->app_id));
$results = $syt_entity->kpi_enroll_log($params);
if ($results['code'] == 400) {
throw new Hd_exception($results['msg'], $results['code']);
}
$key && $this->lc_redis->delete($key);
throw new Hd_exception($results['msg'], $results['code']);
}
/**
* Notes:获取短信验证码
* Created on: 2020/10/14 22:10
* Created by: dengbw
* @throws Hd_exception
*/
protected function get_mcode()
{
$mobile = $this->input_param('mobile');
$title = $this->input_param('title');
if (!mobile_valid($mobile)) {
throw new Hd_exception('手机号码不正确', 400);
}
$key = $this->redis_mcode . $mobile;
$code = $this->lc_redis->get($key);
if (!$code) {
$this->load->helper('string');
$code = random_string('numeric', 6);
$this->lc_redis->save($key, $code, 600);
}
//$content = "【狸车】您正在参与{$title}报名,验证码 {$code}";
//b2m_send_sms($mobile, $content);
send_sms($mobile, $code);
throw new Hd_exception('发送成功', 200);
}
/**
* Notes:设置kpi加分
* Created on: 2020/10/15 10:09
* Created by: dengbw
* @param array $param
*/
private function set_kpi_score($param = array())
{
$param['uid'] = $this->uid;
if ($param['a_id'] && $param['uid'] && ($param['cf_uid'] || $param['biz_id'])) {
$this->load->library('entity/syt_entity');
$syt_entity = new Syt_entity(array('app_id' => $this->app_id));
$syt_entity->kpi_log($param);
}
}
/**
* Notes:未登录授权登录
* Created on: 2020/10/28 21:07
* Created by: dengbw
* @param $url
*/
protected function set_auth($url)
{
if ("wx" == checkua() && !$this->uid) {
$this->load->helper('url');
$config['appid'] = 'wxe66f905683582780';//小红榜授权
$config['appSecret'] = '9546cd4de877be13ce203f3e6140633f';
$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'];
$mobile = '';
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'];
$mobile = $row_wechat['mobile'];
}
}
if ($this->uid) {
$session = array('uid' => $this->uid, "unionid" => $unionid, "mobile" => $mobile);
$ukey = liche_authcode(json_encode($session, JSON_UNESCAPED_UNICODE), 'ENCODE', $this->secret);
set_cookie("ukey", $ukey, 86400 * 30);
}
} 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);
}
}
}
}