Base.php 3.2 KB

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