123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace app\admin\controller\coupon;
- use app\admin\controller\Admin;
- /**
- * 码
- */
- class Code extends Admin{
- protected $CouponCode = null;
- protected function _initialize(){
- parent::_initialize();
- $this->CouponCode = model('coupon.Code');
- }
- public function index(){
- $this->assign('meta_title','券码管理');
- return $this->fetch();
- }
- public function load(){
- $page = input('get.page');
- $limit = input('get.limit');
- $where = [];
- $no = input('param.no');
- if (!empty($no)) {
- $where['cc.no|cc.secret'] = $no;
- }
- $search = input('get.search');
- if (!empty($search)) {
- $where['c.cname|c.id'] = ['like','%'.$search.'%'];
- }
- $list = $this->CouponCode->field('cc.*')
- ->alias('cc')
- ->join('Coupon c','c.id = cc.coupon_id')
- ->where($where)->order('id desc')->paginate($limit,false,['page'=>$page]);
- $data = [];
- foreach ($list as $key => $value) {
- $data[$key]['id'] = $value['id'];
- $coupon = $value['coupon'];
- $data[$key]['coupon'] = $coupon['cname'].'['.$coupon['id'].']';
- $coupon_type = $coupon['type'];
- $data[$key]['type'] = $coupon_type['cname'];
- $data[$key]['sale_price'] = $coupon['sale_price'];
- $data[$key]['no'] = $value['no'];
- $data[$key]['secret'] = $value['secret'];
- $data[$key]['qrcode'] = '<img src="'.$value['qrcode'].'">';
- $data[$key]['barcode'] = '<img src="'.$value['barcode'].'">';
- $data[$key]['url'] = $value['url'];
- $data[$key]['expire_time'] = $value['expire_time'];
- $data[$key]['state'] = $value['state'];
- $data[$key]['state_text'] = $value['state_text'];
- $data[$key]['sale_time'] = date('Y-m-d H:i:s',$value['sale_time']);
- $data[$key]['create_time'] = $value['create_time'];
- }
- $this->output(0,'获取成功',$data,$list->total());
- }
- public function add(){
- if($this->request->isPost()) {
- $coupon_id = input('post.coupon_id');
- if (empty($coupon_id)) {
- $this->output(1,'请选择电子券');
- }
- $Coupon = model('Coupon');
- $coupon = $Coupon->where(['id'=>$coupon_id])->find();
- if (!$coupon) {
- $this->output(1,'参数错误');
- }
- $coupon_type = $coupon['type'];
- $type_name = $coupon_type['name'];
- $type_value = input('post.'.$type_name);
- if (empty($type_value)) {
- $this->output(1,'请输入'.$type['cname']);
- }
- if ($type_name == 'secret') {
- $no = input('post.no');
- if (empty($no)) {
- $this->output(1,'请输入卡号');
- }
- $this->CouponCode->no = $no;
- }
- $expire_time = input('post.expire_time');
- list($expire_start_time,$expire_end_time) = explode('~',$expire_time);
- if (strtotime($expire_end_time) < $this->request->time()) {
- $this->output(1,'有效结束时间必须大于当期时间');
- }
- $this->CouponCode->coupon_id = $coupon['id'];
- $this->CouponCode->$type_name = $type_value;
- $this->CouponCode->expire_time = $expire_time;
- $result = $this->CouponCode->save();
- if (!$result) {
- $this->output(1,'保存失败');
- }
- $this->output(0,'保存成功');
- }else{
- $Coupon = model('Coupon');
- $coupons = $Coupon->where(['state'=>1])->select();
- $coupon_group = [];
- foreach ($coupons as $key => $value) {
- $supplier = $value['supplier'];
- $coupon_group[$supplier['id']]['cname'] = $supplier['cname'];
- $option['id'] = $value['id'];
- $option['cname'] = $value['cname'];
- $option['type_name'] = $value['type']['name'];
- $coupon_group[$supplier['id']]['options'][] = $option;
- }
- $this->assign('coupon_group',$coupon_group);
- $this->assign('meta_title','添加券码');
- return $this->fetch();
- }
- }
- public function edit(){
- if($this->request->isPost()) {
- $id = input('post.id');
- $code = $this->CouponCode->where(['id'=>$id])->find();
- if (!$code) {
- $this->output(1,'参数错误');
- }
- $coupon_id = input('post.coupon_id');
- if (empty($coupon_id)) {
- $this->output(1,'请选择电子券');
- }
- $Coupon = model('Coupon');
- $coupon = $Coupon->where(['id'=>$coupon_id])->find();
- if (!$coupon) {
- $this->output(1,'参数错误');
- }
- $coupon_type = $coupon['type'];
- $type_name = $coupon_type['name'];
- if ($type_name == 'secret') {
- $no = input('post.no');
- if (empty($no)) {
- $this->output(1,'请输入卡号');
- }
- $code->no = $no;
- }
- $type_value = input('post.'.$type_name);
- if (empty($type_value)) {
- $this->output(1,'请输入'.$type['cname']);
- }
- $expire_time = input('post.expire_time');
- list($expire_start_time,$expire_end_time) = explode('~',$expire_time);
- if (strtotime($expire_end_time) < $this->request->time()) {
- $this->output(1,'有效结束时间必须大于当期时间');
- }
- $code->coupon_id = $coupon['id'];
- $code->$type_name = $type_value;
- $code->expire_time = $expire_time;
- $result = $code->save();
- if (!$result) {
- $this->output(1,'保存失败');
- }
- $this->output(0,'保存成功');
- }else{
- $id = input('get.id');
- $code = $this->CouponCode->where(['id'=>$id])->find();
- if (!$code) {
- $this->output(1,'参数错误');
- }
- $this->assign('code',$code);
- $Coupon = model('Coupon');
- $coupons = $Coupon->where(['state'=>1])->group('supplier_id')->select();
- $coupon_group = [];
- foreach ($coupons as $key => $value) {
- $supplier = $value['supplier'];
- $coupon_group[$supplier['id']]['cname'] = $supplier['cname'];
- $option['id'] = $value['id'];
- $option['cname'] = $value['cname'];
- $option['type_name'] = $value['type']['name'];
- $coupon_group[$supplier['id']]['options'][] = $option;
- }
- $this->assign('coupon_group',$coupon_group);
- $this->assign('meta_title','编辑券码');
- return $this->fetch();
- }
- }
- public function state(){
- if($this->request->isPost()){
- $id = input('post.id');
- $code = $this->CouponCode->where(['id'=>$id])->find();
- if (!$code) {
- $this->output(1,'参数错误');
- }
- $state = input('post.state');
- $code->state = $state == 'true'?1:0;
- $result = $code->save();
- if (!$result) {
- $this->output(1,'保存失败');
- }
- $this->output(0,'保存成功');
- }
- }
- public function delete(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $code = $this->CouponCode->where(['id'=>$id])->find();
- if (!$code) {
- $this->output(1,'参数错误');
- }
- $result = $code->delete();
- if (!$result) {
- $this->output(1,'删除失败');
- }
- $this->output(0,'删除成功');
- }
- }
- public function import(){
- vendor('PHPExcel.PHPExcel');
- $path = input('param.path');
- if (empty($path)) {
- $this->output(1,'文件地址不能为空');
- }
- $path_arr = explode('.',$path);
- $exts = $path_arr[count($path_arr) - 1];
- //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
- vendor('PHPExcel.PHPExcel');
- //创建PHPExcel对象,注意,不能少了\
- $PHPExcel = new \PHPExcel();
- //如果excel文件后缀名为.xls,导入这个类
- if ($exts == 'xls') {
- Vendor('PHPExcel.PHPExcel.Reader.Excel5');
- $PHPReader = new \PHPExcel_Reader_Excel5();
- } else if ($exts == 'xlsx') {
- Vendor('PHPExcel.PHPExcel.Reader.Excel2007');
- $PHPReader = new \PHPExcel_Reader_Excel2007();
- }
-
-
- //载入文件
- $PHPExcel = $PHPReader->load(ROOT_PATH.'public'.$path);
- //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
- $currentSheet = $PHPExcel->getSheet(0);
- //获取总列数
- $allColumn = $currentSheet->getHighestColumn();
- //获取总行数
- $allRow = $currentSheet->getHighestRow();
- $fields = ['A'=>'coupon_id','B'=>'no','C'=>'secret','D'=>'qrcode','E'=>'barcode','F'=>'expire_time'];
- //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
- $data = [];
- for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
- //从哪列开始,A表示第一列
- for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
- //数据坐标
- $address = $currentColumn . $currentRow;
- //读取到的数据,保存到数组$data中
- $cell = $currentSheet->getCell($address)->getValue();
-
- if ($cell instanceof PHPExcel_RichText) {
- $cell = $cell->__toString();
- }
- if (!isset($fields[$currentColumn])) {
- break;
- }
- $field = $fields[$currentColumn];
- $data[$currentRow - 1][$field] = $cell;
- // print_r($cell);
- }
-
- }
- var_dump($data);exit();
- $count = count($data);
- if ($count > 0) {
- $this->CouponCode->saveAll($data);
- }
- $this->output(0,'导入成功 条数:'.$count);
- }
- }
|