model = new \app\common\model\user4s\User; $this->level_model = new \app\common\model\user4s\Level; $this->levellog_model = new \app\common\model\user4s\Levellog; $this->log_model = new \app\common\model\user4s\Log; $this->view->assign("genderdataList", $this->model->getGenderdataList()); $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); //var_dump($where); $list = $this->model ->with(['category', 'level']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->getRelation('category')->visible(['name']); $row->getRelation('level')->visible(['name']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } public function info($id = null) { $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { $id = $this->request->param('id'); if (empty($id)) { $result = array("code" => 1, "msg" => '用户ID为空'); return json($result); }; $row = $this->model->get($id); if (!$row) { $result = array("code" => 1, "msg" => __('No Results were found')); return json($result); } else { //$row->getRelation('category')->visible(['name']); //$row->getRelation('level')->visible(['name']); $result = array("code" => 0, "msg" => 'success', "data" => $row); return json($result); } } $result = array("code" => 1, "msg" => '出现错误,请重试!'); return json($result); } public function add() { if ($this->request->isPost()) { //$this->token(); } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; $this->model->startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException(true)->validate($validate); } $cardinfo = $this->checkcardid($params['cardid']); if ($cardinfo['iscard'] == false) { $this->model->rollback(); $this->error('身份证号错误'); } else { $params['birthday'] = $cardinfo['birthday']; $params['genderdata'] = $cardinfo['sex']; } $has_frameno = $this->model->get(['frameno', $params['frameno']]); if (!empty($has_frameno->id)) { $this->model->rollback(); $this->error('车架号重复'); } $result = $this->model->allowField(true)->save($params); //var_dump($params['need_visit']); //新加用户期初等级 if ($result && $this->model->id) { if ($params['need_visit'] == 1) { $log_res = $this->model->afterlog($this->auth->id, $this->model, $this->model->id, '', true); } else { $log_res = $this->model->afterlog($this->auth->id, $this->model, $this->model->id, '', false); } if (!$log_res) { $this->model->rollback(); $this->error('新增失败'); } } //var_dump($result,$this->model->id); $this->model->commit(); } catch (ValidateException $e) { $this->model->rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { $this->model->rollback(); $this->error($e->getMessage()); } catch (Exception $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were inserted')); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = null) { if ($this->request->isPost()) { //$this->token(); } $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); $result = false; $this->model->startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException(true)->validate($validate); } if(!empty($params['cardid'])){ $cardinfo = $this->checkcardid($params['cardid']); if ($cardinfo['iscard'] == false) { $this->model->rollback(); $this->error('身份证号错误'); } else { $params['birthday'] = $cardinfo['birthday']; $params['genderdata'] = $cardinfo['sex']; } } if(!empty($params['cardid'])){ $has_frameno = $this->model->get(['frameno', $params['frameno']]); if (!empty($has_frameno->id)) { $this->model->rollback(); $this->error('车架号重复'); } } // $result = $this->model->allowField(true)->save($params); $result = $row->allowField(true)->save($params); $this->model->commit(); } catch (ValidateException $e) { $this->model->rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { $this->model->rollback(); $this->error($e->getMessage()); } catch (Exception $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were updated')); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } public function check_cardid() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $cardinfo = $this->checkcardid($params['cardid']); if ($cardinfo['iscard']) { $this->success('身份证号正确'); } } $this->error('身份证号错误'); } $this->error('获取数据错误'); } public function check_frameno() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $has_frameno = $this->model->where(['frameno' => $params['frameno']])->find(); //var_dump($has_frameno); if (empty($has_frameno->id)) { $this->success('车架号正确'); } } $this->error('车架号重复'); } $this->error('车架号重复'); } /** * 充值 */ public function recharge($ids = null) { if ($this->request->isPost()) { $this->token(); } $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } $all_level = $this->level_model->where('status', 1)->column('id,name'); if ($this->request->isPost()) { $params = $this->request->post("row/a"); //var_dump($params); $user_data = array( 'id' => $row->id ); //记录用户充值、积分、等级记录 $log_data = array( 'user4s_id' => $row->id, 'admin_id' => $this->auth->id ); //余额充值 if ($params['balance'] > 0) { $now = round($params['balance'], 2); $end = round($row->balance + $now, 2); $log_data['start'] = $row->balance; $log_data['end'] = $end; $log_data['state'] = 0; $log_data['description'] = $params['description']; $this->log_model->isUpdate(false)->save($log_data); $user_data['balance'] = $end; } //积分记录 if ($params['integral'] > 0) { $now = intval($params['integral']); $end = intval($row->integral + $now); $log_data['start'] = $row->integral; $log_data['end'] = $end; $log_data['state'] = 1; if ($now > 0) { $log_data['description'] = $params['description'] . ' 赠送积分:' . $now; } else { $log_data['description'] = $params['description'] . ' 系统后台扣除积分' . $now; } $this->log_model = new \app\common\model\user4s\Log(); $this->log_model->isUpdate(false)->save($log_data); $user_data['integral'] = $end; //var_dump($log_data, $user_data); } //等级记录 if ($params['level_id'] != $row->level_id && $params['level_id'] > 0) { $end = intval($params['level_id']); $log_data['start'] = $row->level_id; $log_data['end'] = $end; $log_data['description'] = '会员等级变动,由 ' . $all_level[$row->level_id] . ' 更改为:' . $all_level[$end]; //var_dump($log_data); $this->levellog_model->isUpdate(false)->save($log_data); $user_data['level_id'] = $end; } if (isset($user_data['balance']) || isset($user_data['integral']) || isset($user_data['level_id'])) { //var_dump($user_data); //$this->model->get($row->id); $this->model->save($user_data, ['id' => $row->id]); } $this->success(); } //var_dump($col); $this->view->assign("all_level", $all_level); $this->view->assign('row', $row); return $this->view->fetch(); } /** * 保险临期用户列表 */ public function expireins() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); $ins_where = array(); $start = strtotime("-1 year"); $end = strtotime("+2 month", $start); $start_date = $this->request->get("start_date", ''); $end_date = $this->request->get("end_date", ''); if(!empty($start_date) && !empty($end_date)){ $start = strtotime(date('Y-').$start_date); $end = strtotime(date('Y-').$end_date); for ($y=1; $y<=30; $y++) { $start_dates = strtotime("-".$y." year",$start); $end_dates = strtotime("-".$y." year", $end); $ins_where[] = array('insdate' => ['between', [date('Y-m-d', $start_dates), date('Y-m-d', $end_dates)]]); } } if ($this->request->isAjax()) { // $start_date = $this->request->post("start_date", ''); // $end_date = $this->request->post("end_date", ''); list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['category', 'level']) ->where($where) ->where(function ($query) use($ins_where) { //todo:测试闭包传递参数 if(!empty($ins_where)){ foreach($ins_where as $item){ $query->whereOr($item); } }else{ for ($y=1; $y<=30; $y++) { $start = strtotime("-".$y." year"); $end = strtotime("+2 month", $start); $ins_where = array( 'insdate' => ['between', [date('Y-m-d', $start), date('Y-m-d', $end)]] ); $query->whereOr($ins_where); } } }) ->order($sort, $order) ->paginate($limit); foreach ($list as &$row) { $row->getRelation('category')->visible(['name']); $row->getRelation('level')->visible(['name']); $tip_start = Date::unixtime('year', 0,'start'); $tip_end = Date::unixtime('year', 0,'end'); //var_dump($tip_start,$tip_end); if($row->instiptime >=$tip_start && $row->instiptime <=$tip_end){ $row->instip = 1; }else{ $row->instip = 0; } } $result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['start_date' => date('m-d', $start), 'end_date' => date('m-d', $end)],'sql'=>$this->model->getLastSql()); return json($result); } return $this->view->fetch(); } /** * 续保操作 */ public function renewal($ids = null) { if ($this->request->isPost()) { $this->token(); } $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } //$all_level = $this->level_model->where('status', 1)->column('id,name'); if ($this->request->isPost()) { $params = $this->request->post("row/a"); //记录用户续保记录 if (!empty($params['inscom']) && !empty($params['insdate'])) { $log_data = array( 'user4s_id' => $row->id, 'admin_id' => $this->auth->id, 'instype' => $params['instype'], 'inscom' => $params['inscom'], 'insdate' => $params['insdate'], 'star' => $params['star'], 'description' => $params['description'] ); $this->Inslog_model = new \app\common\model\user4s\Inslog(); $this->Inslog_model->isUpdate(false)->save($log_data); $user_data = array( 'instype' => $params['instype'], 'inscom' => $params['inscom'], 'insdate' => $params['insdate'], ); $this->model->save($user_data, ['id' => $row->id]); } else { $this->error('请填写续保信息', url('user4s/user/expireins', ['user4s_id' => $row->id])); } $this->success('续保成功!', url('user4s/user/expireins', ['user4s_id' => $row->id])); } //var_dump($col); $all_star = array( 1 => '★', 2 => '★★', 3 => '★★★', 4 => '★★★★', 5 => '★★★★★', ); $this->view->assign("now", date('Y-m-d')); $this->view->assign("all_star", $all_star); $this->view->assign('row', $row); return $this->view->fetch(); } /** * 续保提醒 */ public function instip($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } //$all_level = $this->level_model->where('status', 1)->column('id,name'); if ($this->request->isPost()) { //记录用户续保提醒 $row->instiptime = time(); $row->save(); $this->success('记录提醒状态成功!', url('user4s/user/expireins'),array('tiptime'=>date('Y-m-d H:i:s'))); } $this->view->assign('row', $row); return $this->view->fetch(); } /** * 取消续保提醒 */ public function instipno($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } //$all_level = $this->level_model->where('status', 1)->column('id,name'); if ($this->request->isPost()) { //记录用户续保提醒 $row->instiptime = 0; $row->save(); $this->success('取消提醒状态成功', url('user4s/user/expireins'),array('tiptime'=>date('Y-m-d H:i:s'))); } $this->view->assign('row', $row); return $this->view->fetch(); } /** * 过生日用户列表 */ public function birthday() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { $birthday = $this->request->get("birthday", ''); if(!empty($birthday)){ //halt($birthday); $search_date = '2022-'.$birthday; }else{ $filter = $this->request->get("filter", ''); $op = $this->request->get("op", '', 'trim'); $filter = (array)json_decode($filter, true); $op = (array)json_decode($op, true); if(!empty($filter['birthday'])){ $search_date = '2022-'.$filter['birthday']; $_GET['filter']= '{}'; $_GET['op']= '{}'; }else{ $search_date = date('Y-m-d'); } } $birthday_users = Db::query("select id from car_user4s_user where MONTH(birthday) = MONTH('".$search_date."') and DAY(birthday) = DAY('".$search_date."')"); if(empty($birthday_users)){ $result = array("total" => 0, "rows" => [], "extend" => ['start_date' => date('m-d',strtotime($search_date))]); return json($result); }else{ foreach ($birthday_users as $key => $value) { $birthday_user_ids[] = $value['id']; } } if(empty($birthday_user_ids)){ $result = array("total" => 0, "rows" => [], "extend" => ['start_date' => date('m-d',strtotime($search_date))]); return json($result); } //var_dump($filter,$op); //var_dump($birthday_user_ids); list($where, $sort, $order, $offset, $limit) = $this->buildparamsu('{}','{}'); $list = $this->model ->with(['category', 'level']) ->where($where) ->where('user.id', 'in', $birthday_user_ids) ->where('user.birthday', '<>', '0000-00-00') ->order($sort, $order) ->paginate($limit); //echo($this->model->getLastSql()); foreach ($list as $row) { $row->getRelation('category')->visible(['name']); $row->getRelation('level')->visible(['name']); } $result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['start_date' => date('m-d',strtotime($search_date))]); return json($result); } return $this->view->fetch(); } protected function buildparamsu($filter,$op,$searchfields = null, $relationSearch = null) { $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields; $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch; $search = $this->request->get("search", ''); // $filter = $this->request->get("filter", ''); // $op = $this->request->get("op", '', 'trim'); $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id'); $order = $this->request->get("order", "DESC"); $offset = $this->request->get("offset/d", 0); $limit = $this->request->get("limit/d", 999999); //新增自动计算页码 $page = $limit ? intval($offset / $limit) + 1 : 1; if ($this->request->has("page")) { $page = $this->request->get("page/d", 1); } $this->request->get([config('paginate.var_page') => $page]); $filter = (array)json_decode($filter, true); $op = (array)json_decode($op, true); $filter = $filter ? $filter : []; $where = []; $alias = []; $bind = []; $name = ''; $aliasName = ''; if (!empty($this->model) && $this->relationSearch) { $name = $this->model->getTable(); $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model)))); $aliasName = $alias[$name] . '.'; } $sortArr = explode(',', $sort); foreach ($sortArr as $index => & $item) { $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item; } unset($item); $sort = implode(',', $sortArr); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds]; } if ($search) { $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields); foreach ($searcharr as $k => &$v) { $v = stripos($v, ".") === false ? $aliasName . $v : $v; } unset($v); $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"]; } $index = 0; foreach ($filter as $k => $v) { if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) { continue; } $sym = isset($op[$k]) ? $op[$k] : '='; if (stripos($k, ".") === false) { $k = $aliasName . $k; } $v = !is_array($v) ? trim($v) : $v; $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym); //null和空字符串特殊处理 if (!is_array($v)) { if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) { $sym = strtoupper($v); } if (in_array($v, ['""', "''"])) { $v = ''; $sym = '='; } } switch ($sym) { case '=': case '<>': $where[] = [$k, $sym, (string)$v]; break; case 'LIKE': case 'NOT LIKE': case 'LIKE %...%': case 'NOT LIKE %...%': $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"]; break; case '>': case '>=': case '<': case '<=': $where[] = [$k, $sym, intval($v)]; break; case 'FINDIN': case 'FINDINSET': case 'FIND_IN_SET': $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v)); $findArr = array_values($v); foreach ($findArr as $idx => $item) { $bindName = "item_" . $index . "_" . $idx; $bind[$bindName] = $item; $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)"; } break; case 'IN': case 'IN(...)': case 'NOT IN': case 'NOT IN(...)': $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)]; break; case 'BETWEEN': case 'NOT BETWEEN': $arr = array_slice(explode(',', $v), 0, 2); if (stripos($v, ',') === false || !array_filter($arr, function($v){ return $v != '' && $v !== false && $v !== null; })) { continue 2; } //当出现一边为空时改变操作符 if ($arr[0] === '') { $sym = $sym == 'BETWEEN' ? '<=' : '>'; $arr = $arr[1]; } elseif ($arr[1] === '') { $sym = $sym == 'BETWEEN' ? '>=' : '<'; $arr = $arr[0]; } $where[] = [$k, $sym, $arr]; break; case 'RANGE': case 'NOT RANGE': $v = str_replace(' - ', ',', $v); $arr = array_slice(explode(',', $v), 0, 2); if (stripos($v, ',') === false || !array_filter($arr)) { continue 2; } //当出现一边为空时改变操作符 if ($arr[0] === '') { $sym = $sym == 'RANGE' ? '<=' : '>'; $arr = $arr[1]; } elseif ($arr[1] === '') { $sym = $sym == 'RANGE' ? '>=' : '<'; $arr = $arr[0]; } $tableArr = explode('.', $k); if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) { //修复关联模型下时间无法搜索的BUG $relation = Loader::parseName($tableArr[0], 1, false); $alias[$this->model->$relation()->getTable()] = $tableArr[0]; } $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr]; break; case 'NULL': case 'IS NULL': case 'NOT NULL': case 'IS NOT NULL': $where[] = [$k, strtolower(str_replace('IS ', '', $sym))]; break; default: break; } $index++; } if (!empty($this->model)) { $this->model->alias($alias); } $model = $this->model; $where = function ($query) use ($where, $alias, $bind, &$model) { if (!empty($model)) { $model->alias($alias); $model->bind($bind); } foreach ($where as $k => $v) { if (is_array($v)) { call_user_func_array([$query, 'where'], $v); } else { $query->where($v); } } }; return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind]; } /** * sms */ public function sms($ids = null) { if ($this->request->isPost()) { $this->token(); } if(stripos($ids, ',') >= 1){ $ids = explode(',',$ids); }else{ $ids = array($ids); } $row = $this->model->where('id','in',$ids)->select(); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } //短信info $sms_config = array( 'userid' => Config::get('site.sms_userid'), 'account' => Config::get('site.sms_user'), 'password' => Config::get('site.sms_passwd'), ); $sms = new Sms($sms_config); $sms_info = $sms->getOverage(); if(!$sms_info['status']){ $this->error('短信平台配置错误,请检查配置!'.$sms_info['message']); } $this->view->assign("sms_info", $sms_info); if ($this->request->isPost()) { $content = $this->request->post("sms_content"); if ($content && is_array($ids)) { //var_dump($ids); //halt($content); $send_status = $sms->send($ids,$content); if($send_status['code'] == 0){ $this->error(__('发送失败!错误提示:'.$send_status['msg'], ''),'',$send_status); }else{ $this->success('发送成功!共计:'.$send_status['total'].'个,发送成功:'.$send_status['success'].'个','',$send_status); } //var_dump($send_status); } $this->error(__('短信内容为空', '')); } $this->view->assign("row", collection($row)->toarray()); $this->view->assign("user_num", count($row)); return $this->view->fetch(); } }