接着前面第一篇往下面讲,今天主要实现是电脑版首页部分的功能,先上一些效果图先。
代码部分
namespace app\home\controller;
use think\Page;
use think\Verify;
use think\Db;
use think\Image;
class Index extends Base
{
public function index()
{
$web_list = S('web_index_data');
if(!$web_list){
$web_list = M('web')->where(array('web_show'=>1))->order('web_sort')->select();
if($web_list){
foreach ($web_list as $kb=>$vb){
$block_list = M('web_block')->where(array('web_id'=>$vb['web_id']))->order('web_id')->select();
if(is_array($block_list) && !empty($block_list)) {
foreach ($block_list as $key => $val) {//将变量输出到页面
$val['block_info'] = unserialize($val['block_info']);
$web_list[$kb][$val['var_name']] = $val['block_info'];
}
}
}
S('web_index_data',$web_list);
}
}
$this->assign('web_list', $web_list);
return $this->fetch();
}
公告详情页
public function notice()
{
return $this->fetch();
}
public function qr_code_raw()
{
ob_end_clean();
// 导入Vendor类库包 Library/Vendor/Zend/Server.class.php
vendor('phpqrcode.phpqrcode');
//import('Vendor.phpqrcode.phpqrcode');
error_reporting(E_ERROR);
$url = urldecode($_GET["data"]);
\QRcode::png($url);
exit;
}
用户分销二维码
public function qr_code()
{
ob_end_clean();
vendor('topthink.think-image.src.Image');
vendor('phpqrcode.phpqrcode');
error_reporting(E_ERROR);
$url = isset($_GET['data']) ? $_GET['data'] : '';
$url = urldecode($url);
$head_pic = input('get.head_pic', '');
$back_img = input('get.back_img', '');
$valid_date = input('get.valid_date', 0);
$qr_code_path = './public/upload/qr_code/';
if (!file_exists($qr_code_path)) {
mkdir($qr_code_path);
}
/* 生成二维码 */
$qr_code_file = $qr_code_path.time().rand(1, 10000).'.png';
\QRcode::png($url, $qr_code_file, QR_ECLEVEL_M);
/* 二维码叠加水印 */
$QR = Image::open($qr_code_file);
$QR_width = $QR->width();
$QR_height = $QR->height();
/* 添加背景图 */
if ($back_img && file_exists($back_img)) {
$back =Image::open($back_img);
$back->thumb($QR_width, $QR_height, \think\Image::THUMB_CENTER)
->water($qr_code_file, \think\Image::WATER_NORTHWEST, 60);//->save($qr_code_file);
$QR = $back;
}
/* 添加头像 */
if ($head_pic) {
//如果是网络头像
if (strpos($head_pic, 'http') === 0) {
//下载头像
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $head_pic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file_content = curl_exec($ch);
curl_close($ch);
//保存头像
if ($file_content) {
$head_pic_path = $qr_code_path.time().rand(1, 10000).'.png';
file_put_contents($head_pic_path, $file_content);
$head_pic = $head_pic_path;
}
}
//如果是本地头像
if (file_exists($head_pic)) {
$logo = Image::open($head_pic);
$logo_width = $logo->height();
$logo_height = $logo->width();
$logo_qr_width = $QR_width / 5;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
$logo_file = $qr_code_path.time().rand(1, 10000);
$logo->thumb($logo_qr_width, $logo_qr_height)->save($logo_file, null, 100);
$QR = $QR->thumb($QR_width, $QR_height)->water($logo_file, \think\Image::WATER_CENTER);
unlink($logo_file);
}
if ($head_pic_path) {
unlink($head_pic_path);
}
}
if ($valid_date && strpos($url, 'weixin.qq.com') !== false) {
$QR = $QR->text('有效时间 '.$valid_date, "./vendor/topthink/think-captcha/assets/zhttfs/1.ttf", 7, '#00000000', Image::WATER_SOUTH);
}
$QR->save($qr_code_file, null, 100);
$qrHandle = imagecreatefromstring(file_get_contents($qr_code_file));
unlink($qr_code_file); //删除二维码文件
header("Content-type: image/png");
imagepng($qrHandle);
imagedestroy($qrHandle);
exit;
}
public function street()
{
$sc_id = I('get.sc_id/d');
$province = I('get.province', 0);
$city = I('get.city', 0);
$order = I('order', 0);
$key = I('q/s', '');
$store_class = Db::name('store_class')->cache(true)->field('sc_id,sc_name')->where('')->select();
$store_where = ['store_state' => 1,'deleted'=>0,'store_recommend'=>1];
if ($sc_id) {
$store_where['sc_id'] = $sc_id;
}
if ($province) {
$store_where['province_id'] = $province;
}
if ($city) {
$store_where['city_id'] = $city;
}
if($order){
$orderBy[$order] = 'desc';
}else{
$orderBy = ['store_sort' => 'desc','store_recommend'=> 'desc'];
}
if(!empty($key)){
$store_where['store_name'] = array('like' , "%$key%");
}
$store_count = Db::name('store')->alias('s')->where($store_where)->count();
$page = new Page($store_count, 12);
$store_list = Db::name('store')->field("store_id,store_name,seo_description,store_logo,store_banner")
->where($store_where)->order($orderBy)->limit($page->firstRow, $page->listRows)->select();
if(is_array($store_list)){
foreach ($store_list as $key => $value) {
$goods_array['goods_list'] = Db::name('goods')->field("goods_id,goods_name,shop_price,is_virtual")
->where([ 'is_on_sale'=>1, 'goods_state'=>1,'store_id'=>$value['store_id']])->limit(3)->order('sort desc')->select();
$goods_array['goods_list']['goods_count'] = Db::name('goods')->where(['store_id'=>$value['store_id']])->count();
$store_list[$key]['goods_array'] = $goods_array;
}
}
$region = Db::name('region')->cache(true)->where("`level` = 1")->getField("id,name");
$this->assign('province', $province);
$this->assign('city', $city);
$this->assign('region', $region);
$this->assign('page', $page);
$this->assign('store_list', $store_list);
$this->assign('store_class', $store_class);//店铺分类
return $this->fetch();
}
public function store_qrcode()
{
require_once 'vendor/phpqrcode/phpqrcode.php';
error_reporting(E_ERROR);
$store_id = I('store_id/d', 1);
\QRcode::png(U('Mobile/Store/index', array('store_id' => $store_id), true, true));
}