Skip to content

Commit d879af2

Browse files
committed
添加project功能
1 parent 9f53617 commit d879af2

File tree

11 files changed

+473
-7
lines changed

11 files changed

+473
-7
lines changed

application/common.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,28 @@
1919
*/
2020
function captcha_img_strong($id = '',$className ='',$widthHeightArray = [])
2121
{
22+
2223
if(!empty($widthHeightArray)){
2324
$img=<<<EOT
2425
<img src="%s" onclick="this.src='%s?r='+Math.random()" alt="captcha" class="%s" style="cursor: pointer;z-index:100;position:absolute;right:0;bottom:5px;width: %spx;height: %spx"/>
2526
EOT;
26-
return sprintf($img,'/index/auth/verify/id/'.$id,'/index/auth/verify/id/'.$id,$className,$widthHeightArray['width'],$widthHeightArray['height']);
27+
28+
if($id == '')
29+
{
30+
return sprintf($img,'/index/base/verify','/index/base/verify',$className,$widthHeightArray['width'],$widthHeightArray['height']);
31+
}else{
32+
return sprintf($img,'/index/base/verify/id/'.$id,'/index/base/verify/id/'.$id,$className,$widthHeightArray['width'],$widthHeightArray['height']);
33+
}
2734
}else{
2835
$img=<<<EOT
2936
<img src="%s" onclick="this.src='%s?r='+Math.random()" alt="captcha" style="cursor: pointer" class="%s" />
3037
EOT;
31-
return sprintf($img,captcha_src($id),captcha_src($id),$className);
38+
if($id == '')
39+
{
40+
return sprintf($img,'/index/base/verify','/index/base/verify',$className);
41+
}else{
42+
return sprintf($img,'/index/base/verify/id/'.$id,'/index/auth/verify/id/'.$id,$className);
43+
}
3244
}
3345

3446

application/index/controller/Auth.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ protected function initialize()
1919

2020
if(!$this->isLogin())
2121
{
22-
header('Location:'.$this->request->domain());
23-
exit;
22+
if($this->request->isAjax())
23+
{
24+
echo json(['status'=>0,'message'=>'请登录',],JSON_UNESCAPED_UNICODE);
25+
exit;
26+
}else{
27+
header('Location:'.$this->request->domain());
28+
exit;
29+
}
30+
2431
}
2532
}
2633

application/index/controller/Project.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,50 @@
88

99
namespace app\index\controller;
1010

11-
11+
use app\index\service\Project as ProjectService;
12+
use app\index\model\Project as ProjectModel;
1213
class Project extends Auth
1314
{
1415

1516
public function index()
1617
{
18+
19+
$list = ProjectModel::where('user_id',$this->user_id)->order('id desc')->paginate(3);
20+
$page = $list->render();
21+
22+
$this->assign('list', $list);
23+
$this->assign('page', $page);
1724
$this->assign('title',lang('Project List'));
25+
1826
return view('project/index');
1927
}
2028

29+
/**
30+
*
31+
* @return \app\index\service\ServiceResult
32+
*/
33+
public function create()
34+
{
35+
return ProjectService::create($this->request->param('project_name'),$this->user_id);
36+
}
37+
38+
39+
public function edit()
40+
{
41+
if($this->request->isGet())
42+
{
43+
$this->assign('project',ProjectModel::get($this->request->param('id')));
44+
$this->assign('title',lang('Edit Project'));
45+
return view('project/edit');
46+
}else{
47+
return ProjectService::edit($this->request->param('project_name'),$this->user_id,$this->request->param('id'));
48+
}
49+
50+
51+
}
52+
53+
public function delete()
54+
{
55+
return ProjectService::delete($this->request->param('id'),$this->user_id);
56+
}
2157
}

application/index/model/Project.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111

1212
class Project extends BaseModel
1313
{
14+
protected $autoWriteTimestamp = 'datetime';
1415

1516
}

application/index/service/Project.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: zhangcheng
5+
* Date: 2017/12/18
6+
* Time: 下午3:35
7+
*/
8+
9+
namespace app\index\service;
10+
11+
use app\index\model\Project as ProjectModel;
12+
use app\index\validate\Project as ProjectValidate;
13+
use think\facade\Request;
14+
15+
class Project
16+
{
17+
public static function create($project_name,$user_id)
18+
{
19+
$model = new ProjectModel();
20+
21+
$model->startTrans();
22+
try{
23+
24+
$validate = new ProjectValidate();
25+
26+
if(!$validate->check([
27+
'project_name'=>$project_name,
28+
]))
29+
{
30+
return ServiceResult::Error($validate->getError());
31+
}
32+
33+
$model->allowField(['project_name','user_id','create_ip']);
34+
$add = $model->save([
35+
'project_name'=>$project_name,
36+
'user_id'=>$user_id,
37+
'create_ip'=>Request::ip(),
38+
]);
39+
40+
$model->commit();
41+
}
42+
catch(\Exception $e)
43+
{
44+
$model->rollback();
45+
46+
return ServiceResult::Error($e->getMessage());
47+
}
48+
49+
if($add)
50+
{
51+
52+
return ServiceResult::Success(['id'=>$model->id],'创建成功');
53+
54+
}else{
55+
56+
return ServiceResult::Error($model->getError());
57+
58+
}
59+
}
60+
61+
public static function edit($project_name,$user_id,$id)
62+
{
63+
$project = ProjectModel::get($id);
64+
65+
if(!$project)
66+
{
67+
return ServiceResult::Error('项目不存在');
68+
}
69+
70+
if($project->user_id != $user_id)
71+
{
72+
return ServiceResult::Error('没有权限修改');
73+
}
74+
75+
76+
$project->startTrans();
77+
try{
78+
79+
$validate = new ProjectValidate();
80+
81+
if(!$validate->check([
82+
'project_name'=>$project_name,
83+
]))
84+
{
85+
return ServiceResult::Error($validate->getError());
86+
}
87+
88+
$project->allowField(['project_name']);
89+
90+
$update = $project->save([
91+
'project_name'=>$project_name,
92+
]);
93+
94+
$project->commit();
95+
}
96+
catch(\Exception $e)
97+
{
98+
$project->rollback();
99+
100+
return ServiceResult::Error($e->getMessage());
101+
}
102+
103+
if($update !== false)
104+
{
105+
106+
return ServiceResult::Success([],'修改成功');
107+
108+
}else{
109+
110+
return ServiceResult::Error($project->getError());
111+
112+
}
113+
}
114+
115+
public static function delete($id,$user_id)
116+
{
117+
$project = ProjectModel::get($id);
118+
119+
if(!$project)
120+
{
121+
return ServiceResult::Error('项目不存在');
122+
}
123+
124+
if($project->user_id != $user_id)
125+
{
126+
return ServiceResult::Error('没有权限');
127+
}
128+
129+
$del = $project->delete();
130+
131+
if($del)
132+
{
133+
return ServiceResult::Success([],'删除成功');
134+
}else{
135+
return ServiceResult::Error('删除失败');
136+
}
137+
}
138+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: zhangcheng
5+
* Date: 2017/12/18
6+
* Time: 下午3:36
7+
*/
8+
9+
namespace app\index\validate;
10+
11+
12+
class Project extends BaseValidate
13+
{
14+
protected $rule = [
15+
'project_name' => 'unique:project|require|max:255|min:1',
16+
17+
];
18+
19+
protected $message = [
20+
'project_name.unique' => '项目名称已存在',
21+
'project_name.require'=>'项目名称不为空',
22+
'project_name.max' => '项目名称不能超过255位',
23+
'project_name.min' => '项目名称不为空',
24+
25+
26+
];
27+
28+
//场景验证
29+
protected $scene = [
30+
'add' => ['project_name'],
31+
'edit' => ['project_name'],
32+
];
33+
}

application/index/view/extend/main.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@
324324
<script src="__JS__/material-dashboard-v=1.2.1.js"></script>
325325
<!-- Material Dashboard DEMO methods, don't include it in your project! -->
326326
<script src="__JS__/demo.js"></script>
327+
328+
<!--vue + axios(ajax) -->
329+
<script src="https://vuejs.org/js/vue.min.js"></script>
330+
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
331+
327332
<script type="text/javascript">
328333
$(document).ready(function() {
329334

@@ -333,5 +338,5 @@
333338
demo.initVectorMap();
334339
});
335340
</script>
336-
341+
{block name="footer"}{/block}
337342
</html>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{extend name="extend/main" /}
2+
{block name="content"}
3+
<div class="container-fluid">
4+
<div class="row">
5+
<!-- project form -->
6+
<div class="col-md-12">
7+
<div class="card">
8+
<form method="post" action="" class="form-horizontal">
9+
<div class="card-header card-header-text" data-background-color="rose">
10+
<h4 class="card-title">{:lang('Edit Project')}</h4>
11+
</div>
12+
<div class="card-content">
13+
<div class="row">
14+
<label class="col-sm-2 label-on-left">{:lang('Project Name')}</label>
15+
<div class="col-sm-10">
16+
<div class="form-group label-floating is-empty">
17+
<label class="control-label"></label>
18+
<input type="text" class="form-control" name="project_name" value="{$project.project_name}" required="true" />
19+
<span class="help-block">{:lang('Project Name')}.</span>
20+
</div>
21+
</div>
22+
</div>
23+
<div class="row">
24+
<label class="col-sm-2 label-on-left"></label>
25+
<div class="col-sm-10">
26+
<div class="form-group label-floating is-empty">
27+
<label class="control-label"></label>
28+
<input type="button" class="btn btn-success" id="add_project" value="{:lang('Submit')}">
29+
30+
</div>
31+
</div>
32+
</div>
33+
34+
</div>
35+
</form>
36+
</div>
37+
</div>
38+
39+
</div>
40+
</div>
41+
{/block}
42+
{block name="footer"}
43+
<script>
44+
$(function () {
45+
$('#add_project').click(function () {
46+
47+
axios.post('/index/project/edit', {
48+
project_name:$('input[name=project_name]').val(),
49+
id:'{$project.id}'
50+
}).then(function (response) {
51+
swal({
52+
title: "提示!",
53+
text: response.data.message,
54+
timer: 2000,
55+
showConfirmButton: false
56+
}).catch(swal.noop)
57+
58+
if(response.data.status == 1)
59+
{
60+
setTimeout(function(){
61+
location.href = '/project/index';
62+
},2100);
63+
}
64+
})
65+
.catch(function (error) {
66+
console.log(error);
67+
});
68+
69+
})
70+
});
71+
</script>
72+
{/block}

0 commit comments

Comments
 (0)