123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace app\admin\controller\qingdongams;
- use addons\qingdongams\controller\File;
- use addons\qingdongams\model\AdminConfig;
- use addons\qingdongams\model\Staff;
- use app\common\controller\Backend;
- use app\common\exception\UploadException;
- use app\common\library\Upload;
- /**
- * qingdongams 基础控制器
- *
- * @icon fa fa-circle-o
- */
- class Base extends Backend
- {
- protected $noNeedRight = [];
- protected $_staff = null;
- public function _initialize()
- {
- parent::_initialize();
- $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/qingdongams/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('当前账号未绑定员工,要使用插件功能,请在员工列表添加账号', 'qingdongams/department/staff');
- }
- }
- $map_key=AdminConfig::getConfigValue('map_key', 'wechat');
- $this->assign('map_key',$map_key);
- }
- public function getStaff()
- {
- return $this->_staff;
- }
- protected function qingdongamsValidate($params, $class, $scene, $rules = []) {
- $validate = validate('addons\qingdongams\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();
- if (stristr($attachment->mimetype, 'image')) {
- $types = 'image';
- } else {
- $types = 'file';
- }
- $file = new \addons\qingdongams\model\File();
- $params = [
- 'name' => $info['filename'],
- 'save_name' => $info['url'],
- 'size' => isset($info['filesize'])?$info['filesize']:0,
- 'types' => $types,
- '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']
- ]);
- }
- }
|