文件
mashangyoujiu/Application/Retail/Controller/IndexController.class.php

417 行
15 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace Vip\Controller;
use Think\Controller;
use Addons\Api\CommonApi;
use Think\Jssdk;
use Think\Jssdks;
//[1.6.46]
class IndexController extends BaseController
{
Public function index()
{
$merchantid = session('merchantid');
//会员充值页面跳转
$map['merchantid'] = $merchantid;
$wechat = M('wxconfig')->where($map)->find();
if ($wechat['type'] == 1) {
$jssdk = new Jssdk($wechat['appid'], self::$_wxappsecret);
} else if ($wechat['type'] == 2) {
$access_token = get_authorizer_access_token($map['merchantid']);
$jssdk = new Jssdks($wechat['authorizer_appid'], $access_token);
} else {
$this->error('微信配置参数有误');
}
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
$vid = session('vid');
$where['id'] = $vid;
$vip = D('Vip')->getFind($where);
$grade = A('Api/Vipintegral')->getGrade($merchantid, $vip['grade_integral']);//查找会员等级
$grade['cardname'] = $grade['cardname'] ? $grade['cardname'] : "平民卡";
if ($vip['plus_type'] == 2) {//plus会员
$vip['cardname'] = 'Plus会员';
} else {
$vip['cardname'] = $grade['cardname'] ? $grade['cardname'] : '普通会员';
}
$vip['gradeimgurl'] = $grade['gradeimgurl'] ? $grade['gradeimgurl'] : '/Public/vip/images/top_name_grade.png';
if ($vip['cardnum']) {
$vip['cardnum1'] = substr($vip['cardnum'], 0, 4) . "****" . substr($vip['cardnum'], -(strlen($vip['cardnum']) - 8));
}
//查看是否完善资料
if ($vip['is_perfect'] == 0) {
$where1['id'] = $merchantid;
$vipConfig = M('vip_config')->where($where1)->find();
if (intval($vipConfig['infoscore']) > 0) {
$this->assign('canPerfect', 1);
$this->assign('score', $vipConfig['infoscore']);
}
}
// 所有隐藏的默认菜单
$hidedefaultindex = M('retail_vipcardmenu_config')
->where(array('merchantid' => $merchantid, 'type' => 1, 'hide' => 2))
->getField('defaultindex', true);
$hidedefaultindex = $hidedefaultindex ? $hidedefaultindex : array(0);
$this->assign('defaultindex1', 1);
$this->assign('defaultindex2', 2);
$this->assign('defaultindex3', 3);
$this->assign('defaultindex4', 4);
$this->assign('defaultindex5', 5);
$this->assign('hidedefaultindex', $hidedefaultindex);
// 所有隐藏的插件菜单
$hideplugarr = M('retail_vipcardmenu_config')
->where(array('merchantid' => $merchantid, 'type' => 2, 'hide' => 2))->getField('plugid', true);
$hideplugarr = $hideplugarr ? $hideplugarr : array(0);
$this->assign('hideplugarr', $hideplugarr);
unset($map);
$map['pm.merchant_id'] = $vip['merchantid'];
$map['pm.m_status'] = 1;
$map['pm.status'] = 1;
$map['p.type'] = 1;
$map['p.status'] = 1;
$map['p.id'] = 38;//积分商城
$plug = M('plug_merchant as pm')
->join('ls_plug as p on p.id=pm.plug_id')
->where($map)
->Field('p.id')
->find();
$this->assign('is_score_mall', $plug);
unset($map);
$map = array();
$map['pm.merchant_id'] = $vip['merchantid'];
$map['pm.m_status'] = 1;
$map['pm.status'] = 1;
$map['p.type'] = 1;
$map['p.status'] = 1;
$map['p.id'] = 41;//优惠券
$couponplug = M('plug_merchant as pm')
->join('ls_plug as p on p.id=pm.plug_id')
->where($map)
->find();
$this->assign('is_coupon', $couponplug);
unset($map);
$map = array();
$map['pm.merchant_id'] = $vip['merchantid'];
$map['pm.m_status'] = 1;
$map['pm.status'] = 1;
$map['p.type'] = 1;
$map['p.status'] = 1;
$map['p.id'] = 44;//次卡
$subCardPlug = M('plug_merchant as pm')
->join('ls_plug as p on p.id=pm.plug_id')
->where($map)
->find();
$this->assign('is_sub_card', $subCardPlug);
$turntable = $this->getTurntable($vid, $merchantid);//大转盘
$reward = getRewardActivity(1, $merchantid, '', $vid);//红包
$notices = M('vipcard_notice')
->where(array('merchantid' => $merchantid, 'status' => 1, 'state' => 1))
->order('id desc')
->select();
$noticeArr = array();
foreach ($notices as $n) {
switch ($n['plug_id']) {
// case '36':
// //大转盘
// if ($activity['code'] == 0 && $activity['activity_type'] == 1) {
// $n['url'] = "/index.php/Vip/Turntable/index?turntable_id={$activity['activity_id']}";
// $noticeArr[] = $n;
// }
// break;
case '36':
//大转盘
if ($turntable==1) {
$n['url'] = "/index.php/Vip/Activity/turntable";
$noticeArr[] = $n;
}
break;
case '39':
//红包
if ($reward['status'] == 1) {
$n['url'] = "/index.php/Vip/Reward/reward.html";
$noticeArr[] = $n;
}
break;
default:
$noticeArr[] = $n;
}
}
$this->assign('notices', $noticeArr);
$merchant_name = M('retail_merchant')->where(array('id' => $merchantid))->getField('name');
$this->assign('merchant_name', $merchant_name);
//获取会员签名包
$info = $this->storeCardInfo();
if (!empty($info)) {
$mem_signPackage = $this->getHuiYuanSignPackage();
$this->assign('nocard', 0);
} else {
$this->assign('nocard', 1);
}
$this->assign('mem_signPackage', $mem_signPackage);
$this->assign('vip', $vip);
//如果设置了分享设置
$shareinfo = M('retail_vipshare')->where(array('merchantid' => $merchantid))->find();
if ($shareinfo) {
$http = C('shop_http');
if (!empty($shareinfo['image_id'])) {
$shareinfo['image'] = D('ImageGallery')->getFieldPath(array('id' => $shareinfo['image_id']));
} else {
$shareinfo['image'] = C('shop_http') . $shareinfo['image'];
}
$this->assign('shareinfo', $shareinfo);
$this->assign('http', $http);
}
$userInfo = session('userinfo');
if ($userInfo['style_model'] == 2) {
$this->display('index2');
} else if ($userInfo['style_model'] == 3) {
$this->display('index3');
} else {
$this->display();
}
}
/**
* 判断是否有大转盘的机会
* @param $vid 会员ID
* @param $merchant_id 商户ID
* @param $money 支付金额
* @return int
*/
private function getTurntable($vid, $merchant_id)
{
$map['pm.merchant_id'] = $merchant_id;
$map['pm.m_status'] = 1;
$map['p.id'] = 36;
$turntable_plug = M('plug_merchant as pm')
->join("ls_plug as p on p.id=pm.plug_id")
->where($map)
->find();
if (!empty($turntable_plug)) {
$turntable = M('turntable')
->where(array('merchantid' => $merchant_id, 'status' => 1, 'type' => 1))
->find();
$us['vid'] = $vid;
$us['turntableid'] = $turntable['id'];
$us['type'] = array('neq', 5);
if ($turntable['maxnum']) {
$user_max_num = M('turntable_use')->where($us)->count();//用户总中奖次数
if (intval($turntable['maxnum']) <= $user_max_num) {
return 0;
}
}
if ($turntable['playnum']) {
unset($us['type']);
$user_play_num = M('turntable_use')->where($us)->count();//用户已抽奖次数
if (intval($turntable['playnum']) <= $user_play_num) {
return 0;
}
}
if ($turntable['daymaxnum']) {
//限制每日中奖次数
$us['type'] = array('neq', 5);
$us['_string'] = 'addtime <' . time() . ' AND addtime >' . strtotime(date('Y-m-d'));
$turntable_number = M('turntable_use')->where($us)->count();
if ($turntable_number >= intval($turntable['daymaxnum'])) {
$where['type'] = 5;
$where['turntableid'] = $turntable['id'];
$prize = M('turntable_prize')->where($where)->find();
if (empty($prize)) {
return 0;
}
}
}
return 1;
} else {
return 0;
}
}
public function card()
{
$merchantid = session('merchantid');
//会员充值页面跳转
$map['merchantid'] = $merchantid;
$wechat = M('wxconfig')->where($map)->find();
if ($wechat['type'] == 1) {
$jssdk = new Jssdk($wechat['appid'], self::$_wxappsecret);
} else if ($wechat['type'] == 2) {
$access_token = get_authorizer_access_token($map['merchantid']);
$jssdk = new Jssdks($wechat['authorizer_appid'], $access_token);
} else {
$this->error('微信配置参数有误');
}
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
//获取会员签名包
$info = $this->storeCardInfo();
if (!empty($info)) {
$mem_signPackage = $this->getHuiYuanSignPackage();
$this->assign('mem_signPackage', $mem_signPackage);
} else {
$this->tipsMsg(0, '商户未创建会员卡');
}
$this->display();
}
function clearsessionandcookie()
{
foreach ($_COOKIE as $key => $value) {
setcookie($key, null);
}
session_unset();
$data['cookie'] = $_COOKIE;
$data['session'] = $_SESSION;
$this->ajaxReturn($data);
}
//获取token
public function getToKen()
{
//获取会员卡信息
//获取token
if (self::$SET['type'] == 1) {
$appid = self::$_wxappid;
$secret = self::$_wxappsecret;
$json_token = http_request('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret);
$escape_token = json_decode($json_token);//转义
$token = $escape_token->{'access_token'};//2个小时换一次,一天2000次
return $token;//返回token值
} elseif (self::$SET['type'] == 2) {
$tokens = get_authorizer_access_token(session('merchantid'));
return $tokens;
}
}
//拉去卡券信息
public function getApiTicket()
{
$token = $this->getToKen();//获取token
$ticket = http_request('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $token . '&type=wx_card');
$ticket_success = json_decode($ticket);
if ($ticket_success->{'errmsg'} == 'ok') {
return $ticket_success->{'ticket'};
} else {
return false;
}
}
//获取会员卡信息
public function storeCardInfo()
{
//获取会员卡信息
$where['merchantid'] = session('merchantid');
$cardinfo = M('wx_membercard')->Field('id,card_id')->where($where)->find();
return $cardinfo;
}
//获取会员卡签名包
public function getHuiYuanSignPackage()
{
$apiTicket = $this->getApiTicket();
// 注意 URL 一定要动态获取,不能 hardcode.(获取当前网页的url)
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//时间戳
$CardInfo = $this->storeCardInfo();
$data['timestamp'] = time();
$data['card_id'] = $CardInfo['card_id'];
$data['api_ticket'] = $apiTicket;
/* print_r($data);
exit;*/
//字典排序
$datas = $data;
sort($data, SORT_STRING);
$signature = sha1(implode($data));
$data['signature'] = $signature;
$mem_signPackage = array(
"timestamp" => $datas['timestamp'],
"signature" => $signature,
'card_id' => $CardInfo['card_id']
);
return $mem_signPackage;
}
public function zidian($str)
{
$arr = array();
for ($i = 0; $i < strlen($str); $i++) {
array_push($arr, substr($str, $i, 1));
}
sort($arr);
return $arr;
}
//随机字符串
private function createNonceStr($length = 16)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/*
* 这里通过post
*/
function curl_post($url, array $params = array(), $timeout)
{
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return ($data);
}
private function http_post($url, $param, $post_file = false)
{
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
// dump($aStatus);
// exit;
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
}