Base.php
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace app\admin\controller\antis;
use addons\antis\controller\File;
use addons\antis\model\Staff;
use app\common\controller\Backend;
use app\common\exception\UploadException;
use app\common\library\Upload;
/**
* antis 基础控制器
*
* @icon fa fa-circle-o
*/
class Base extends Backend
{
protected $noNeedRight = [];
protected $_staff = null;
public function _initialize()
{
parent::_initialize();
// 检查是否已登录,如果没有登录则跳过staff初始化
if (!$this->auth || !$this->auth->id) {
return;
}
$staff = Staff::where(['admin_id' => $this->auth->id])->find();
if (!empty($staff)) {
$this->_staff = $staff;
}else{
$data = [
'admin_id' => $this->auth->id,
'group_ids' => implode(',', $this->auth->getGroupIds()),
'name' => $this->auth->nickname,
'num'=>'01',
'mobile' => $this->auth->username,
'salt' => $this->auth->salt,
'password' => $this->auth->password,
'img' => '/assets/addons/antis/mini/avatar.png',
'email' => $this->auth->email,
'sex' => 1,
'status' => 1,
'createtime' => $this->auth->createtime,
'updatetime' => $this->auth->updatetime
];
$staffModel = new Staff();
$result = $staffModel->save($data);
if ($result) {
$staff = Staff::where(['admin_id' => $this->auth->id])->find();
$this->_staff = $staff;
} else {
$this->error('当前账号未绑定员工,要使用插件功能,请在员工列表添加账号', 'antis/department/staff');
}
}
}
public function getStaff()
{
return $this->_staff;
}
protected function qingdongValidate($params, $class, $scene, $rules = []) {
$validate = validate('addons\antis\validate\\'.$class);
if (!$validate->check($params, $rules, $scene)) {
return $validate->getError();
}
return true;
}
/**
* 上传文件
* @ApiMethod (POST)
* @param File $file 文件流
*/
public function upload() {
$attachment = null;
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$attachment = $upload->upload();
$info = $attachment->toArray();
$file = new \addons\antis\model\File();
$params = [
'name' => $info['filename'],
'save_name' => $info['url'],
'size' => isset($info['filesize'])?$info['filesize']:0,
'types' => $info['mimetype'],
'file_path' => $info['url'],
'create_staff_id' => empty($staff)?0:$staff->id,
];
$file->data(array_filter($params));
$file->save();
$fileId = $file->id;
} catch (UploadException $e) {
return json_encode(['code' => 0, 'msg' => $e->getMessage()]);
}
$this->success(__('Uploaded successful'),'', [
'id' => $fileId,
'url' =>$params['file_path']
]);
}
}