Base.php 3.6 KB

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