2023-01-29 10:26:52 +08:00

73 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 Bookcode extends Admin{
public function index(){
$data = db('book_roll')->order('id desc')->select();
$this->assign('data',$data);
$this->setMeta('书币卷列表');
return $this->fetch();
}
public function add(){
$data = input('');
if($data){
$is = db('book_roll')->where('money',$data['money'])->value('id');
if($is){
exit(json_encode(array('status'=>2,'reg'=>'已存在相同优惠券')));
}
$arr = array(
'money'=>$data['money'],
'addtime'=>time()
);
$id = db('book_roll')->insertGetId($arr);
if($id){
$url = config('web_site_url')."/bookroll?id=".$id;
$code = $this->getCode($url);
db('book_roll')->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('book_roll')->where('id',$id)->value('code');
$res = db('book_roll')->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;
}
}