input('table'); // 表名 //运行更新和删除的操作数据表 if (!in_array($table, $this->allowTable())) { return $this->noSelfMsg(); } switch ($type) { case 'del': return $this->delete($request); break; case 'edit': return $this->editField($request); break; } } public function editField(Request $request) { $table = $request->input('table'); // 表名 $id_name = $request->input('id_name', 'id'); // 表主键id名 $id_value = $request->input('id_value'); // 表主键id值 $field = $request->input('field'); // 修改哪个字段 $value = $request->input('value'); // 修改字段值 if ($table == '' or $id_name == '' or $id_value == '' or $field == '' or $value == '') { $data = [ 'error' => 1, 'msg' => '参数不正确' ]; return response()->json($data); } //验证是否自己的信息 if (!$this->checkSelfUser($id_value, $id_name, $table)) { $data = [ 'error' => 1, 'msg' => '非法操作别人数据' ]; return response()->json($data); } $model = DB::table($table)->where($id_name, $id_value)->update([$field => $value]); if ($table == 'merchants') { write_merchant();//更新商户信息 } if ($model) { $data = [ 'error' => 0, 'msg' => '设置成功' ]; return response()->json($data); } else { $data = [ 'error' => 0, 'msg' => '设置失败' ]; return response()->json($data); } } /** * 软删除开启 * @param $table * @return int */ public function deletType($table) { switch ($table) { case 'shops': return 1; case 'shop_servers': return 1; case 'under_orders': return 1; default: return 0; } } public function delete(Request $request) { $id = $request->input('id'); $type_id = $request->input('type_id', 'id'); $table = $request->input('table'); $handle_str = $request->input('handle_str'); if ($table == '') { $table = $request->input('model'); } $rdel = $this->deletType($table); if ($id == '') { $data = [ 'error' => 1, 'msg' => '编号不能为空', 'type' => 'del' ]; return response()->json($data); } if ($table == '') { $data = [ 'error' => 1, 'msg' => '没有选择数据表', 'type' => 'del' ]; return response()->json($data); } $id_arr = explode(",", $id); if (count($id_arr) <= 0) { $data = [ 'error' => 1, 'msg' => '编号不能为空', 'type' => 'del' ]; return response()->json($data); } /* if (count($id_arr) >= 2) { $data = [ 'error' => 1, 'msg' => '商户版本不支持批量删除操作', 'type'=>'del' ]; return response()->json($data); }*/ //验证是否自己的信息 if (!$this->checkSelfUser($id, 'id', $table)) { $data = [ 'error' => 1, 'msg' => '非法操作别人数据' ]; return response()->json($data); } DB::beginTransaction(); $result = DB::table($table)->whereIn($type_id, $id_arr); //删除条件附加 $result = $this->whereDelete($result, $table); if ($rdel) { $result = $result->update(['deleted_at' => date('Y-m-d H:i:s', time())]); } else { $result = $result->delete(); } //删除事务 if ($result && $this->deleteCustom($table, $id_arr)) { DB::commit(); $this->eventCustom($table, $id_arr); $this->afterUpdate($table, $result, $id_arr); $this->insertLog($handle_str . '删除 ID:' . implode('、', $id_arr)); $data = [ 'error' => 0, 'msg' => '删除成功', 'type' => 'del' ]; return response()->json($data); } DB::rollBack(); $data = [ 'error' => 1, 'msg' => '删除失败', 'type' => 'del' ]; return response()->json($data); } public function deleteCustom($table, $id) { switch ($table) { case 'admins': return DB::table('model_has_roles')->where('model_type', 'admin')->whereIn('model_id', $id)->delete(); break; } return true; } public function eventCustom($table, $id) { } public function afterUpdate($table, $result, $id_arr) { switch ($table) { } } /** * 检查是否自己信息,不然不给修改 * @param $id * @param $table * @param string $checkid */ function checkSelfUser($id, $id_name, $table) { switch ($table) { case 'merchants': // $has=DB::table($table)->where($id_name,$id)->where('from_id',$this->getMerchantId())->count(); $has = MerchantRatio::where('parent_id', $this->getMerchantId())->count(); if ($has <= 0) return false; break; case 'bank_rels': $has = BankRel::where($id_name,$id)->where('model_id', $this->getMerchantId())->count(); if ($has <= 0) return false; break; } return true; } /** * 删除条件附加 * @param $model_obj */ public function whereDelete($model_obj, $table) { switch ($table) { /* case 'coupon_cards': $model_obj=$model_obj->where('create_user_type','user')->where('create_user_id',admin_shop('id')); break; case 'activities': $model_obj=$model_obj->where('create_user_type','user')->where('create_user_id',admin_shop('id')); break;*/ } return $model_obj; } }