route['controller_name'] = '\\' . $route_arr[0]; $this->route['action_name'] = $route_arr[1]; $this->route['controller_base'] = str_replace('Controller', '', str_replace('App\\Http\\Controllers\\Merchant\\', '', $route_arr[0])); $this->route['blade'] = self::ADMIN_BLADE_DIR . strtolower($this->route['controller_base']) . '.' . $this->route['action_name']; $this->share_view['controller'] = "\\" . $this->route['controller_name']; $this->title = ucwords($this->route['action_name']); $this->share_view['title'] = $this->title; $this->setPagesInfo(); //$this->handleUrl(); $this->getTable(); $this->share_view['action_name'] = $this->route['action_name']; $this->share_view['page'] = $this->pages; $this->setTitle('商户后台'); $this->shareView(); } public function getMerchantId() { return $this->merchant_id = merchant()->id; } public function isProxy() { if (merchant('level') != 3) { return true; } return false; } public function dayDrawRatio() { return Merchant::getDayDrawRatio($this->getMerchantId()); } /** * 验证规则 * @param array $data * @return array */ protected function checkRule($id = '') { } /** * 设置表单验证错误信息 * @return array */ public function setErrorMsg() { $messages = [ ]; return $messages; } /** * 全局返回表单错误消息 * @param $error * @return array */ protected function formError($error) { $error = $error->all(); if (count($error) <= 0) return []; $error_str = ''; foreach ($error as $k => $v) { $error_str .= $v . "*
"; } return ['error' => 1, 'msg' => $error_str, 'type' => 'validator']; } public function alertError($error_str, $status = 1) { return response()->json(['error' => $status, 'msg' => $error_str, 'type' => 'validator']); } /** * 全局检测表单是否错误,如果又返回错误数组,否则空数组 * @param $request * @param string $id * @return array */ protected function validatorForm($request, $id = '') { $validator = Validator::make($request->all(), $this->checkRule($id), $this->setErrorMsg()); if ($validator->fails()) { if ($request->ajax() || $request->wantsJson()) { return $validator->errors(); } } return []; } /** * 设置创建数据键值对 * @param $obj * @param $data * @return mixed */ protected function setDbKeyValue($obj, $data) { $fileds = $this->getField(); foreach ($data as $field => $v) { if (in_array($field, $fileds)) { $v = is_null($v) ? '' : $v; $obj->$field = trim($v); } } return $obj; } /** * 全局创建和更新操作返回消息 * @param $result * @param $request * @param string $id * @return \Illuminate\Http\JsonResponse */ protected function saveMessage($result, $request, $id = '') { $type = $id ? '更新' : '创建'; if ($request->method() == 'DELETE') { $type = '删除'; } $msg_type = $result ? '成功' : '失败'; $msg_str = $this->pages['name'] . $type . $msg_type; $log_msg_str = $this->pages['name'] . 'ID:' . $this->dblast_id . ',' . $type . $msg_type; //$msg_log_str=$this->pages['name'].'ID:'.$this->dblast_id.','.$type.$msg_type; if ($result) { $this->insertLog($log_msg_str); } if ($request->ajax() || $request->wantsJson()) { $data = [ 'error' => $result ? 0 : 1, 'msg' => $msg_str ]; $data=$this->addOutData($id)+$data; return response()->json($data); } } public function addOutData($id='') { $model=$this->store_model; return []; } protected function insertLog($msg) { MerchantLog::addLog($msg); } protected function backMessage($data = [], $success, $request_type, $type) { $return_arr = []; $type_str = $request_type == 'edit' ? '更新' : '创建'; if ($type == 'ajax') { if ($success) { $return_arr['error'] = 0; $return_arr['msg'] = $type_str . '成功'; } else { $return_arr['error'] = 1; $return_arr['msg'] = $type_str . '失败,请重新操作'; } } return $return_arr; } /** * 共享视图资源 * @return mixed */ public function shareView() { return view()->share($this->share_view); } /** * 设置页面标题 * @param string $title */ protected function setTitle($title = '') { $title = $title ? $title : $this->pages['name']; $this->share_view['title'] = $title; } /** * 页面page设置 */ protected function setPagesInfo() { $this->pages = [ 'name' => '' ]; } /** * 改变自动获取blade * @param $path * @param int $cover 是否完全覆盖 * @param int $is_seft 是否在当前的控制下目录 * @return string */ protected function setViewPath($path = '', $filename = '', $cover = 0, $is_seft = 1) { if ($cover) { return $this->route['blade'] = $path; } if ($is_seft == 0) { return $this->route['blade'] = self::ADMIN_BLADE_DIR . $path; } if ($filename != '') { $this->route['action_name'] = $filename; } if ($path) { $this->route['action_name'] = $path . "." . $this->route['action_name']; } return $this->route['blade'] = self::ADMIN_BLADE_DIR . strtolower($this->route['controller_base']) . '.' . $this->route['action_name']; } /** * 取得当前的模型的字段列表 * @param $model * @return mixed */ protected function getField() { $table = $this->getTable(); $data = Schema::getColumnListing($table); if (array_key_exists('id', $data)) { unset($data['id']); } return $data; } /** * 取得table * @return string */ protected function getTable() { if (gettype($this->setModel()) != 'object') { $table = ''; } else { $table = $this->setModel()->getTable(); } $this->share_view['table_name'] = $table; $this->shareView(); return $table; } /** * 本页面操作的模型 */ protected function setModel() { } public function checkSelf($show) { return true; } protected function getShow($id) { $show = $this->setModel()->find($id); //检查是否是自己的不 if (!$this->checkSelf($show)) { return $this->noSelfMsg(); } if ($show->shop) if (!$show) { return abort(404); } view()->share('show', $show); return $this->display($this->shareData($show)); } /** * 输出视图 * @param array $data * @return mixed */ public function display($data = []) { return view($this->route['blade'], $data); } public function jsonDebug($json) { print_r($json); return ''; } public function setDbLastId($result, $obj) { if ($result) { $this->dblast_id = $obj->id; return true; } return false; } protected function saveAfter($request, $model, $id = '') { } protected function postDataDb($request, $id = '') { return $request->all(); } protected function searchKey($model, $arr, $where = 1) { if (empty($arr)) { return $model; } return $model->search($arr, $where); } public function indexData() { return []; } //共享创建和编辑数据 protected function shareData($id = '') { return []; } public function checkValueData() { $filed_arr = []; $value = 0; if (count($filed_arr) <= 0) { return false; } return ['fields' => $filed_arr, 'value' => $value]; } public function checkOnValue($data, $handel_arr, $value = 0) { $handel_data = $this->checkValueData(); if ($handel_data) { $handel_arr = $handel_data['fields'] + $handel_arr; $value = $handel_data['value']; } if (count($handel_arr) <= 0) { return $data; } foreach ($handel_arr as $k => $v) { if (array_key_exists($v, $data)) { continue; } $data[$v] = $value; } return $data; } public function errorJosn($msg) { return ['error' => 1, 'msg' => $msg]; } /** * 检查是否有更新权限 */ public function checkUpdatePermission($model) { return true; } //附加验证 protected function extendValidate($request, $model, $id) { return true; } /** * 创建和更新操作 * @param $request * @param $model * @param string $id * @return array|\Illuminate\Http\JsonResponse */ protected function saveData($request, $model, $id = '') { $error = $this->validatorForm($request, $id); if (count($error) > 0) { return $this->formError($error); }; $extend_checked = $this->extendValidate($request, $model, $id); if (is_array($extend_checked)) { return response()->json($extend_checked); } //设置请求参数 $data = $this->postDataDb($request, $id); //设置is_checkd不选择的时候为0 if ($id) { // if(in_array('is_checked',$this->getField())) // { // $data=$this->checkOnValue($data,['is_checked'],0); // } } $model = $this->setDbKeyValue($model, $data); $begin = isset($data['begindb']) ? $data['begindb'] : 0; if ($begin != 0) { DB::beginTransaction(); $this->store_model = $model; $result = $model->save(); $this->setDbLastId($result, $model); $after_result = $this->saveAfter($request, $model, $id); if ($result && $after_result) { $result = 1; DB::commit(); $this->beginAfter($request, $model, $data); } else { $result = 0; DB::rollback(); } } else { $this->store_model = $model; $result = $model->save(); $this->setDbLastId($result, $model); $this->saveAfter($request, $model, $id); } return $this->saveMessage($result, $request, $id); } public function beginAfter($request, $model, $data = []) { } public function jsonMsg($error, $str) { return response()->json(['error' => $error, 'msg' => $str]); } /*********基本控制器资源设置*********/ public function noSelfMsg() { if (\request()->ajax() || \request()->wantsJson()) { return response()->json(['error' => '1', 'msg' => '非法操作数据']); } return abort(401, '非法操作数据'); } public function __call($method, $parameters) { return abort('401', $method . '方法不存在'); } }