投个票吧,路过的大大们给打个分,忘记提标准了,就是比比哪坨更差就行了
```
private function save(Request $request, $id = 0){
header('Content-type: application/json; charset=utf-8');
if(empty($id)){
if(empty($this->rights['add'])){
die(json_encode(array('msg'=>'对不起,你没有权限,请与管理员联系', 'code'=>0)));
}
}else{
if(empty($this->rights['edit'])){
die(json_encode(array('msg'=>'对不起,你没有权限,请与管理员联系', 'code'=>0)));
}
}
if(!empty($id)){
$old = $this->getArticleByID($id);
if(empty($old) || empty($old->id)){
die(json_encode(array('msg'=>'对不起,记录不存在,请重新确认', 'code'=>0)));
}
if($this->rights['edit'] == 3 && $this->invest_id != $old->teacher_id){
die(json_encode(array('msg'=>'对不起,你没有权限,请与管理员联系', 'code'=>0)));
}
}
if(empty($id)){
$data['type'] = self::type;
$data['teacher_id'] = session('invest_id');
if(empty($data['teacher_id'])){
die(json_encode(array('msg'=>'对不起,你不是老师,请绑定老师,请联系管理员', 'code'=>0)));
}
}
$data['title'] = $request->input('title');
if(empty($data['title'])){
die(json_encode(array('msg'=>'对不起,标题不能为空,请重新确认', 'code'=>0)));
}
if(!empty($id)){
$count = Article::where('title', '=', $data['title'])->count();
if($old->title == $data['title'] && $count > 1){
die(json_encode(array('msg'=>'对不起,标题已经存在,请重新输入', 'code'=>0)));
}else if($old->title != $data['title'] && $count >= 1){
die(json_encode(array('msg'=>'对不起,标题已经存在,请重新输入', 'code'=>0)));
}
}
$data['content'] = $request->input('content');
if(empty($data['content'])){
die(json_encode(array('msg'=>'对不起,内容不能为空,请重新确认', 'code'=>0)));
}
if($this->isSuperUser()){
$data['reads'] = $request->input('reads');
if(empty($data['reads'])){
$data['reads'] = 0;
}else{
$data['reads'] = intval($data['reads']);
}
$data['avg_score'] = $request->input('avg_score');
if(empty($data['avg_score'])){
$data['avg_score'] = 0;
}else{
$data['avg_score'] = intval($data['avg_score']);
}
}
if($this->isSuperUser()){
$this->validator($data, (!empty($id) ? $this->rules_update_2 : $this->rules_create_2));
}else{
$this->validator($data, (!empty($id) ? $this->rules_update_1 : $this->rules_create_1));
}
$admin = $request->user();
$data['user_id'] = $admin->id;
$data['user_name'] = $admin->name;
if(empty($id)){
$newItem = new Article();
foreach ($data as $k => $v){
$newItem->{$k} = $v;
}
$ret = $newItem->save();
}else{
$update = [];
foreach ($data as $k => $v){
if($old->{$k} != $v){
$update[$k] = $v;
}
}
if(empty($update)){
$ret = true;
}else{
$ret = $old->update($update);
}
}
if($ret > 0){
die(json_encode(array('msg'=> '恭喜您,成功', 'code'=>1)));
}else{
die(json_encode(array('msg'=>'失败,请重新确认或请联系管理员', 'code'=>0)));
}
}
```
```
public function store( Request $request)
{
//
$result = array('msg'=> '', 'code'=>0);
if(empty($this->rights['add'])){
$result['msg'] = '对不起,你没有权限,请与管理员联系';
} else {
$data = $request->all();
//TODO add content remove html tag validate
$validator = Validator::make($request->all(), $this->rules_create_1);
if ($validator->fails())
{
$errors = $validator->getMessageBag()->toArray();
$result['msg'] = array_values($errors)[0];
} else {
//
$data['teacher_id'] = session('invest_id');
$tg_teacher = Teacher::find($data['teacher_id']);
//目标老师是否存在,有可能被删除
if ( is_null($tg_teacher) ) {
$result['msg'] = '该老师已经被系统删除或禁言';
} else {
if(empty($data['teacher_id'])){
$result['msg'] = '对不起,你不是老师,请绑定老师,请联系管理员';
} else {
$data['type'] = self::type;
$admin = $request->user();
$data['user_id'] = $admin->id;
$data['user_name'] = $admin->name;
$newItem = new Article();
$saved = $newItem->create($data);
if ( $saved ) {
//add operator articles count
$admin->articles += 1;
$admin->save();
//get admin's teacher add articles count
$tg_teacher->articles += 1;
$tg_teacher->save();
$result['msg'] = '恭喜您,成功';
$result['code'] = 1;
} else {
$result['msg'] = '失败,请重新确认或请联系管理员';
}
}
}
}
}
return $result;
}
```