74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
namespace app\admin\controller;
|
|
use app\common\controller\Admin;
|
|
use think\Request;
|
|
use think\Loader;
|
|
use think\Db;
|
|
// 优惠券管理
|
|
class Coupon extends Admin{
|
|
|
|
public function index(){
|
|
$data = db('coupon')->order('id desc')->select();
|
|
$this->assign('data',$data);
|
|
$this->setMeta('优惠券列表');
|
|
return $this->fetch();
|
|
}
|
|
|
|
public function add(){
|
|
$data = input('');
|
|
if($data){
|
|
$is = db('coupon')->where('money',$data['money'])->value('id');
|
|
if($is){
|
|
exit(json_encode(array('status'=>2,'reg'=>'已存在相同优惠券')));
|
|
}
|
|
$arr = array(
|
|
'money'=>$data['money'],
|
|
'period'=>$data['period'],
|
|
'addtime'=>time()
|
|
);
|
|
$id = db('coupon')->insertGetId($arr);
|
|
if($id){
|
|
$url = config('web_site_url')."/coupon?id=".$id;
|
|
$code = $this->getCode($url);
|
|
db('coupon')->where('id',$id)->update(['code'=>$code]);
|
|
exit(json_encode(array('status'=>1,'reg'=>'添加成功')));
|
|
}else{
|
|
exit(json_encode(array('status'=>2,'reg'=>'添加失败')));
|
|
}
|
|
|
|
}else{
|
|
$this->setMeta('添加优惠券');
|
|
return $this->fetch();
|
|
}
|
|
|
|
}
|
|
|
|
// 删除优惠券
|
|
public function del(){
|
|
$id = input('id');
|
|
$code = db('coupon')->where('id',$id)->value('code');
|
|
$res = db('coupon')->delete($id);
|
|
if($res){
|
|
unlink(substr($code,25,100));
|
|
exit(json_encode(array('status'=>1,'reg'=>'删除成功')));
|
|
}else{
|
|
exit(json_encode(array('status'=>2,'reg'=>'错误')));
|
|
}
|
|
}
|
|
|
|
// 通过连接生成2维码
|
|
public function getCode($link){
|
|
import("Qrcode", EXTEND_PATH,'.php');
|
|
$value = $link;
|
|
$errorCorrectionLevel = 'L'; //容错级别
|
|
$matrixPointSize = 6; //生成图片大小
|
|
$filename = 'public/qrcode/'.time().rand(10000,9999999).'.png';
|
|
$qrcode = new \Qrcode;
|
|
$qrcode->png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
|
|
$request = Request::instance();
|
|
$domain = $request->domain();
|
|
$imgurl = $domain.'/'.$filename;
|
|
return $imgurl;
|
|
}
|
|
|
|
} |