123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace app\Nutrition\controller;
- use app\common\model\User;
- use app\common\model\UserLearn;
- use library\Controller;
- use library\tools\Data;
- use think\Db;
- /**
- * 会员学习服务
- * Class Label
- * @package app\utrition\controller
- */
- class Learn extends Controller
- {
- /**
- * 绑定数据表
- * @var string
- */
- protected $table = 'UserLearn';
- /**
- * 列表
- * @auth true
- * @menu true
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function index()
- {
- $this->title = '列表';
- $where= [];
- $where[] = ['type','=',input('type',1)];
- $where[] = ['l.is_deleted','in','0,1'];
- if(input('first_id')) $where[] = ['first_id','=',input('first_id')];
- if(input('second_id')) $where[] = ['second_id','=',input('second_id')];
- if(input('phone')) $where[] = ['m.phone','=',input('phone')];
- $query = $this->_query($this->table)
- ->alias('l')
- ->field('l.*,m.name,m.headimg,m.phone')
- ->leftJoin('store_member m','m.id = l.user_id')
- ->where($where);
- $query->order(' sort desc , id desc')->page();
- }
- /**
- * 数据列表处理
- * @auth true
- * @menu true
- * @param array $data
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- protected function _index_page_filter(&$data)
- {
- }
- protected function _form_filter(&$data)
- {
- if($this->request->isGet() && $this->request->action() == 'edit')
- {
- $info = UserLearn::where('id',input('id'))->find()->toArray();
- $user_info = User::where('id',$info['user_id'])->find()->toArray();
- $data['phone'] = $user_info['phone'] ? $user_info['phone'] : $user_info['email'];
- $this->first_id =$info['first_id'];
- $this->second_id =$info['second_id'];
- $this->type =$info['type'];
- }
- if($this->request->isPost()){
- $phone = input('phone');
- $user_id = User::where('phone|email',$phone)->value('id');
- if(!$user_id)$this->error('用户不存在');
- if(empty($data['end_time']))$this->error('请选择时间');
- $data['user_id'] = $user_id;
- if(isset($data['end_time'])) $data['end_int'] = strtotime($data['end_time']);
- if($this->request->action() == 'add')
- {
- $check = UserLearn::where(['user_id'=>$user_id,'type'=>$data['type'],'first_id'=>$data['first_id']])->where('is_deleted','in','0,1')->value('id');
- if($check) {
- UserLearn::where(['id'=>$check])->update(['end_time'=>$data['end_time'],'end_int'=>$data['end_int']]);
- $this->success('保存成功');
- }
- }
- }
- }
- /**
- * 添加
- * @auth true
- * @menu true
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function add()
- {
- $this->title = '添加';
- $this->first_id = input('first_id');
- $this->second_id = input('second_id');
- $this->type = input('type');
- $this->_form($this->table, 'form');
- }
- /**
- * 标签
- * @auth true
- * @menu true
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function edit()
- {
- $this->title = '编辑';
- $this->_form($this->table, 'form');
- }
- /**
- * 删除
- * @auth true
- * @menu true
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function del()
- {
- $this->_save($this->table, ['is_deleted' => 2]);
- }
- /**
- * 批量删除
- * @auth true
- * @menu true
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function remove()
- {
- $this->_save($this->table, ['is_deleted' => 2]);
- }
- /**
- * 导入
- * @auth true
- * @menu true
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function import(){
- $data = $this->request->get();
- $file = request()->file('file');
- $file_size = $_FILES['file']['size'];
- if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
- //限制上传表格类型
- $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
- if ( $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xlsx格式!');
- $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/upload';
- if (!file_exists($dir)) mkdir($dir, 0777, true);
- $info = $file->move($dir);
- $fileName = $info->getSaveName();
- $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/upload/{$fileName}";
- /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
- if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');
- */
- $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
- $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- $arr = array('A','B');
- // 一次读取一列
- $res_arr = [];
- for ($row = 2; $row <= $highestRow; $row++) {
- $row_arr = array();
- for ($column = 0 ;$column < count($arr) ; $column++) {
- $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
- $row_arr[] = $val;
- }
- $res_arr[] = $row_arr;
- }
- $success_num = 0;
- foreach ($res_arr as $new_user) {
- if (!trim($new_user['0']) || empty($new_user['0']) || !trim($new_user['1']) || empty($new_user['1'])) continue;
- $phone = $new_user['0'];
- $user_id = User::where('phone',$phone)->value('id');
- if(!$user_id) continue;
- $insert= [];
- $insert['user_id'] = $user_id;
- $insert['end_time'] =trim($new_user['1']);
- $insert['end_int'] = strtotime($new_user['1']);
- $insert['first_id'] = $data['first_id'];
- $insert['type'] = $data['type'];
- $check = UserLearn::where(['user_id'=>$user_id,'type'=>$data['type'],'first_id'=>$data['first_id']])->where('is_deleted','in','0,1')->value('id');
- if($check) {
- UserLearn::where(['id'=>$check])->update(['end_time'=>$new_user['1'],'end_int'=>strtotime($new_user['1'])]);
- }else{
- UserLearn::create($insert);
- }
- $success_num++;
- }
- $this->success('成功导入:'.$success_num);
- }
- }
|