CommonController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * 只需要登入,无需检测权限的公共方法
  4. * 前端直接请求的话必须添加header Authorization: userStore.accessToken
  5. */
  6. class CommonController extends Controller
  7. {
  8. /**
  9. * 图片上传
  10. * 不同类型放到不同目录,返回格式也会不同
  11. */
  12. public function actionUploadImg()
  13. {
  14. $upType = '';
  15. if (!empty($_FILES['follow'])) {
  16. $upType = 'follow';
  17. $upArr = $_FILES['follow'];
  18. } elseif (!empty($_FILES['editor'])) {
  19. $upType = 'editor';
  20. $upArr = $_FILES['editor'];
  21. } elseif (!empty($_FILES['avatar'])) {
  22. $upType = 'avatar';
  23. $upArr = $_FILES['avatar'];
  24. } else {
  25. Helper::error('上传有误');
  26. }
  27. $type = strtolower($upArr['type']);
  28. if (!Helper::hasAnyString($type, ['png', 'jpeg', 'jpg'])) {
  29. Helper::error('图片格式不正确 ' . $type);
  30. }
  31. if ($upArr['size'] > 1024 * 1024 * 3) {
  32. Helper::error('图片大小不能超过3M');
  33. }
  34. $ext = strtolower(pathinfo($upArr['name'], PATHINFO_EXTENSION));
  35. $upPath = "zqcrm/{$upType}/" . date('Ymd') . '/' . Helper::getRandomString(16) . '.' . $ext;
  36. Helper::imageUpload($upArr['tmp_name'], $upPath);
  37. if ($upType == 'editor') {
  38. exit(json_encode([
  39. 'errno' => 0,
  40. 'data' => [
  41. 'url' => Helper::getImageUrl($upPath),
  42. ],
  43. ]));
  44. } else {
  45. Helper::ok(['name' => $upPath, 'url' => Helper::getImageUrl($upPath)]);
  46. }
  47. }
  48. public function actionDeleteImg()
  49. {
  50. $path = Helper::getPostString('path');
  51. if (empty($path)) {
  52. Helper::error('参数错误');
  53. }
  54. Helper::dealCommonResult(Helper::imageDelete($path));
  55. }
  56. /**
  57. * Logs out the current user and redirect to homepage.
  58. */
  59. public function actionLogout()
  60. {
  61. Yii::app()->user->logout();
  62. Helper::ok();
  63. }
  64. /******************************* 测试相关代码 ***************************************/
  65. public function actionPhp()
  66. {
  67. (new DBTable(Helper::getGetString('t1')))->echoEditHtml();
  68. }
  69. public function actionTs()
  70. {
  71. echo (new DBTable(Helper::getGetString('t1')))->getTsInterFace();
  72. }
  73. public function actionForm()
  74. {
  75. (new DBTable(Helper::getGetString('t1')))->editVue();
  76. }
  77. public function actionTable()
  78. {
  79. echo (new DBTable(Helper::getGetString('t1')))->getTableHtml();
  80. }
  81. public function actionInfo()
  82. {
  83. echo (new DBTable(Helper::getGetString('t1')))->getDetailHtml();
  84. }
  85. }