Api.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://blog.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\AdminLog;
  11. use app\admin\validate\AdminCheck;
  12. use think\exception\ValidateException;
  13. use think\facade\Db;
  14. use think\facade\Session;
  15. use think\Image;
  16. use app\wechat\controller\Officialaccount;
  17. use Qiniu\Auth;
  18. use Qiniu\Storage\UploadManager;
  19. class Api extends BaseController
  20. {
  21. //上传文件
  22. public function upload()
  23. {
  24. $param = get_params();
  25. // var_dump($param);exit;
  26. $sourse = 'file';
  27. if (isset($param['sourse'])) {
  28. $sourse = $param['sourse'];
  29. }
  30. if ($sourse == 'file' || $sourse == 'tinymce') {
  31. if (request()->file('file')) {
  32. $file = request()->file('file');
  33. } else {
  34. return to_assign(1, '没有选择上传文件');
  35. }
  36. } else {
  37. if (request()->file('editormd-image-file')) {
  38. $file = request()->file('editormd-image-file');
  39. } else {
  40. return to_assign(1, '没有选择上传文件');
  41. }
  42. }
  43. // halt($file);
  44. // 获取上传文件的hash散列值
  45. $sha1 = $file->hash('sha1');
  46. $md5 = $file->hash('md5');
  47. $rule = [
  48. 'image' => 'jpg,png,jpeg,gif',
  49. 'doc' => 'doc,docx,ppt,pptx,xls,xlsx,pdf',
  50. 'file' => 'zip,gz,7z,rar,tar',
  51. 'video' => 'mpg,mp4,mpeg,avi,wmv,mov,flv,m4v',
  52. ];
  53. $fileExt = $rule['image'] . ',' . $rule['doc'] . ',' . $rule['file'] . ',' . $rule['video'];
  54. //1M=1024*1024=1048576字节
  55. $fileSize = 100 * 1024 * 1024;
  56. if (isset($param['type']) && $param['type']) {
  57. $fileExt = $rule[$param['type']];
  58. }
  59. if (isset($param['size']) && $param['size']) {
  60. $fileSize = $param['size'];
  61. }
  62. $validate = \think\facade\Validate::rule([
  63. 'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt,
  64. ]);
  65. $file_check['image'] = $file;
  66. if (!$validate->check($file_check)) {
  67. return to_assign(1, $validate->getError());
  68. }
  69. // 日期前綴
  70. $dataPath = date('Ym');
  71. $use = 'thumb';
  72. $filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
  73. return $md5;
  74. });
  75. if ($filename) {
  76. $path = get_config('filesystem.disks.public.url');
  77. $filepath = $path . '/' . $filename;
  78. if (isset($param['thumb'])) {
  79. $realPath = CMS_ROOT . "public" . $path . '/' . $filename;
  80. $image = Image::open($realPath);
  81. // 按照原图的比例生成一个最大为500*500的缩略图并保存为thumb.png
  82. $image->thumb(500, 500, Image::THUMB_CENTER)->save($realPath . '_thumb.' . $file->extension());
  83. $filepath = $filepath . '_thumb.' . $file->extension();
  84. }
  85. $imageret = self::qiniuUpload($filepath,$filename);
  86. $res['filepath'] = $imageret;
  87. //写入到附件表
  88. $data = [];
  89. $data['filepath'] = $imageret;
  90. $data['name'] = $file->getOriginalName();
  91. $data['mimetype'] = $file->getOriginalMime();
  92. $data['fileext'] = $file->extension();
  93. $data['filesize'] = $file->getSize();
  94. $data['filename'] = $filename;
  95. $data['sha1'] = $sha1;
  96. $data['md5'] = $md5;
  97. $data['module'] = "admin";
  98. $data['action'] = app('request')->action();
  99. $data['uploadip'] = app('request')->ip();
  100. $data['create_time'] = time();
  101. $data['user_id'] = get_login_admin('id') ? get_login_admin('id') : 0;
  102. if ($data['module'] = 'admin') {
  103. //通过后台上传的文件直接审核通过
  104. $data['status'] = 1;
  105. $data['admin_id'] = $data['user_id'];
  106. $data['audit_time'] = time();
  107. }
  108. $data['use'] = request()->has('use') ? request()->param('use') : $use; //附件用处
  109. $res['id'] = Db::name('file')->insertGetId($data);
  110. $res['filepath'] = $data['filepath'];
  111. $res['name'] = $data['name'];
  112. $res['filename'] = $data['filename'];
  113. add_log('upload', $data['user_id'], $data, '文件');
  114. if ($sourse == 'editormd') {
  115. //editormd编辑器上传返回
  116. return json(['success' => 1, 'message' => '上传成功', 'url' => $data['filepath']]);
  117. } else if ($sourse == 'tinymce') {
  118. //tinymce编辑器上传返回
  119. return json(['success' => 1, 'message' => '上传成功', 'location' => $data['filepath']]);
  120. } else {
  121. //普通上传返回
  122. return to_assign(0, '上传成功', $res);
  123. }
  124. } else {
  125. return to_assign(1, '上传失败,请重试');
  126. }
  127. }
  128. public function qiniuUpload($filepath,$filename)
  129. {
  130. $accessKey = 'va_jSLgv-VlomxzMU-6lroagyFoUWxayoxsq7FRg';
  131. $secretKey = 'vVXEUwrvq-H5YIJNzu3u46aM92IE91x6tGjIRonL';
  132. //构建鉴权对象
  133. $auth = new Auth($accessKey, $secretKey);
  134. //在七牛的存储空间
  135. $bucket = 'yiguancaiping';
  136. $token = $auth->uploadToken($bucket);
  137. // 上传到七牛后保存的文件名
  138. $date = time();
  139. $filePath = '.././public' . $filepath;
  140. // 初始化 UploadManager 对象并进行文件的上传。
  141. $uploadMgr = new UploadManager();
  142. // 调用 UploadManager 的 putFile 方法进行文件的上传。
  143. list($ret, $err) = $uploadMgr->putFile($token, $filename, $filePath, null, 'application/octet-stream', true, null, 'v2');
  144. if ($err !== null) {
  145. return $this->error('上传失败!!');
  146. } else {
  147. unlink($filePath);
  148. return 'https://yfw.yiguanfep.com/' . $ret['key'];
  149. }
  150. }
  151. //获取权限树所需的节点列表
  152. public function get_rule()
  153. {
  154. $rule = get_admin_rule();
  155. $group = [];
  156. if (!empty(get_params('id'))) {
  157. $group = get_admin_group_info(get_params('id'))['rules'];
  158. }
  159. $list = create_tree_list(0, $rule, $group);
  160. return to_assign(0, '', $list);
  161. }
  162. //获取关键字
  163. public function get_keyword_cate()
  164. {
  165. $keyword = get_keywords();
  166. return to_assign(0, '', $keyword);
  167. }
  168. //获取话题
  169. public function get_topics_cate()
  170. {
  171. $topic = get_topics();
  172. return to_assign(0, '', $topic);
  173. }
  174. //清空缓存
  175. public function cache_clear()
  176. {
  177. \think\facade\Cache::clear();
  178. return to_assign(0, '系统缓存已清空');
  179. }
  180. //发送测试邮件
  181. public function email_to($email)
  182. {
  183. $name = empty(get_config('webconfig.admin_title')) ? '系统' : get_config('webconfig.admin_title');
  184. if (send_email($email, "一封来自{$name}的测试邮件。")) {
  185. return to_assign(0, '发送成功,请注意查收');
  186. }
  187. return to_assign(1, '发送失败');
  188. }
  189. //修改个人信息
  190. public function edit_personal()
  191. {
  192. $wechat = new Officialaccount();
  193. // $QrCode = $wechat->getQrCode_bindAccount();
  194. $QrCode = "!!!!";
  195. return view('admin/edit_personal', [
  196. 'admin' => get_login_admin(),
  197. 'QrCode' => $QrCode,
  198. ]);
  199. }
  200. //保存个人信息修改
  201. public function personal_submit()
  202. {
  203. if (request()->isAjax()) {
  204. $param = get_params();
  205. try {
  206. validate(AdminCheck::class)->scene('editPersonal')->check($param);
  207. } catch (ValidateException $e) {
  208. // 验证失败 输出错误信息
  209. return to_assign(1, $e->getError());
  210. }
  211. if(!empty($param["thumb"])){
  212. $param["thumb"] = cleanHtml($param["thumb"]);
  213. }
  214. unset($param['username']);
  215. $uid = get_login_admin('id');
  216. Db::name('Admin')->where([
  217. 'id' => $uid,
  218. ])->strict(false)->field(true)->update($param);
  219. $unit_type = Db::name("department")->where("id", get_login_admin("unit_name"))->value("type");
  220. if($unit_type == 2){
  221. $res = Db::name('cost_project')->where('review_head',$uid)->update(['review_head_name' => $param['nickname']]);
  222. $res2 = Db::name('cost_project')->where('operate_head',$uid)->update(['operate_head_name' => $param['nickname']]);
  223. }elseif ($unit_type == 0){
  224. $res = Db::name('cost_project')->where('entrust_maker',$uid)->update(['entrust_maker_name' => $param['nickname']]);
  225. $res2 = Db::name('cost_project')->where('entrust_approver',$uid)->update(['entrust_approver_name' => $param['nickname']]);
  226. }
  227. $session_admin = get_config('app.session_admin');
  228. Session::set($session_admin, Db::name('admin')->find($uid));
  229. return to_assign();
  230. }
  231. }
  232. //修改密码
  233. public function edit_password()
  234. {
  235. return view('admin/edit_password', [
  236. 'admin' => get_login_admin(),
  237. ]);
  238. }
  239. //保存密码修改
  240. public function password_submit()
  241. {
  242. if (request()->isAjax()) {
  243. $param = get_params();
  244. try {
  245. validate(AdminCheck::class)->scene('editpwd')->check($param);
  246. } catch (ValidateException $e) {
  247. // 验证失败 输出错误信息
  248. return to_assign(1, $e->getError());
  249. }
  250. $admin = get_login_admin();
  251. if (set_password($param['old_pwd'], $admin['salt']) !== $admin['pwd']) {
  252. return to_assign(1, '旧密码不正确!');
  253. }
  254. unset($param['username']);
  255. $param['salt'] = set_salt(20);
  256. $param['pwd'] = set_password($param['pwd'], $param['salt']);
  257. Db::name('Admin')->where([
  258. 'id' => $admin['id'],
  259. ])->strict(false)->field(true)->update($param);
  260. $session_admin = get_config('app.session_admin');
  261. Session::set($session_admin, Db::name('admin')->find($admin['id']));
  262. return to_assign();
  263. }
  264. }
  265. // 测试邮件发送
  266. public function email_test()
  267. {
  268. $sender = get_params('email');
  269. //检查是否邮箱格式
  270. $validate = \think\facade\Validate::rule([
  271. 'email' => 'email'
  272. ]);
  273. $data = [
  274. 'email' => $sender
  275. ];
  276. if (!$validate->check($data)) {
  277. return to_assign(1, $validate->getError());
  278. }
  279. $email_config = \think\facade\Db::name('config')->where('name', 'email')->find();
  280. $config = unserialize($email_config['content']);
  281. $content = $config['template'];
  282. //所有项目必须填写
  283. if (empty($config['smtp']) || empty($config['smtp_port']) || empty($config['smtp_user']) || empty($config['smtp_pwd'])) {
  284. return to_assign(1, '请完善邮件配置信息!');
  285. }
  286. $send = send_email($sender, '测试邮件', $content);
  287. if ($send) {
  288. return to_assign(0, '邮件发送成功!');
  289. } else {
  290. return to_assign(1, '邮件发送失败!');
  291. }
  292. }
  293. //首页获取
  294. public function get_admin_list()
  295. {
  296. $content = Db::name('Admin')
  297. ->where(['status' => 1])
  298. ->order('id desc')
  299. ->limit(10)
  300. ->select()->toArray();
  301. $res['data'] = $content;
  302. return table_assign(0, '', $res);
  303. }
  304. //首页获取最新10位用户
  305. public function get_user_list()
  306. {
  307. $list = Db::name('User')
  308. ->where(['status' => 1])
  309. ->order('id desc')
  310. ->limit(10)
  311. ->select()->toArray();
  312. foreach ($list as $key => $val) {
  313. $list[$key]['last_login_time'] = date('Y-m-d H:i:s', $val['last_login_time']);
  314. }
  315. $res['data'] = $list;
  316. return table_assign(0, '', $res);
  317. }
  318. //首页文章
  319. public function get_article_list()
  320. {
  321. $list = Db::name('Article')
  322. ->field('a.id,a.title,a.read,a.status,a.create_time,c.title as cate_title')
  323. ->alias('a')
  324. ->join('article_cate c', 'a.cate_id = c.id')
  325. ->where(['a.delete_time' => 0])
  326. ->order('a.id desc')
  327. ->limit(10)
  328. ->select()->toArray();
  329. foreach ($list as $key => $val) {
  330. $list[$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
  331. }
  332. $res['data'] = $list;
  333. return table_assign(0, '', $res);
  334. }
  335. //系统操作日志
  336. public function log_list()
  337. {
  338. return view('admin/log_list');
  339. }
  340. //获取系统操作日志
  341. public function get_log_list()
  342. {
  343. $param = get_params();
  344. $log = new AdminLog();
  345. $content = $log->get_log_list($param);
  346. return table_assign(0, '', $content);
  347. }
  348. //获取访问记录
  349. public function get_view_data()
  350. {
  351. $param = get_params();
  352. $first_time = time();
  353. $second_time = $first_time - 86400;
  354. $three_time = $first_time - 86400*365;
  355. $begin_first = strtotime(date('Y-m-d', $first_time) . " 00:00:00");
  356. $end_first = strtotime(date('Y-m-d', $first_time) . " 23:59:59");
  357. $begin_second = strtotime(date('Y-m-d', $second_time) . " 00:00:00");
  358. $end_second = strtotime(date('Y-m-d', $second_time) . " 23:59:59");
  359. $begin_three = strtotime(date('Y-m-d', $three_time) . " 00:00:00");
  360. $data_first = Db::name('UserLog')->field('create_time')->whereBetween('create_time', "$begin_first,$end_first")->select();
  361. $data_second = Db::name('UserLog')->field('create_time')->whereBetween('create_time', "$begin_second,$end_second")->select();
  362. $data_three = Db::name('UserLog')->field('create_time')->whereBetween('create_time', "$begin_three,$end_first")->select();
  363. return to_assign(0, '', ['data_first' => hour_document($data_first), 'data_second' => hour_document($data_second), 'data_three'=>date_document($data_three)]);
  364. }
  365. }