CommonController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. $maxSize = 3;
  16. if (!empty($_FILES['follow'])) {
  17. $upType = 'follow';
  18. $upArr = $_FILES['follow'];
  19. } elseif (!empty($_FILES['editor'])) {
  20. $upType = 'editor';
  21. $upArr = $_FILES['editor'];
  22. } elseif (!empty($_FILES['avatar'])) {
  23. $upType = 'avatar';
  24. $upArr = $_FILES['avatar'];
  25. $maxSize = 0.3;
  26. } elseif (!empty($_FILES['canteen'])) {
  27. $upType = 'canteen';
  28. $upArr = $_FILES['canteen'];
  29. } else {
  30. Helper::error('上传有误');
  31. }
  32. $type = strtolower($upArr['type']);
  33. if (!Helper::hasAnyString($type, ['png', 'jpeg', 'jpg'])) {
  34. Helper::error('图片格式不正确 ' . $type);
  35. }
  36. if ($upArr['size'] > $maxSize * 1024 * 1024) {
  37. Helper::error("图片大小不能超过{$maxSize}M");
  38. }
  39. $ext = strtolower(pathinfo($upArr['name'], PATHINFO_EXTENSION));
  40. $upPath = "zqcrm/{$upType}/" . date('Ymd') . '/' . Helper::getRandomString(16) . '.' . $ext;
  41. $res = Helper::imageUpload($upArr['tmp_name'], $upPath);
  42. if (empty($res['code']) || $res['code'] != 200) {
  43. Helper::error($res['msg'] ?? '上传出错');
  44. }
  45. if ($upType == 'avatar') {
  46. $info = DB::getInfoById('useradmin', \Yii::app()->user->_id);
  47. Helper::imageDelete($info['avatar']);
  48. DB::updateById('useradmin', ['avatar' => $upPath], \Yii::app()->user->_id);
  49. }
  50. if ($upType == 'editor') {
  51. exit(json_encode([
  52. 'errno' => 0,
  53. 'data' => [
  54. 'url' => Helper::getImageUrl($upPath),
  55. ],
  56. ]));
  57. } else {
  58. Helper::ok(['name' => $upPath, 'url' => Helper::getImageUrl($upPath)]);
  59. }
  60. }
  61. public function actionDeleteImg()
  62. {
  63. $path = Helper::getPostString('path');
  64. if (empty($path)) {
  65. Helper::error('参数错误');
  66. }
  67. Helper::dealCommonResult(Helper::imageDelete($path));
  68. }
  69. public function actionChangePassword()
  70. {
  71. $old = Helper::getPostString('password');
  72. $new = Helper::getPostString('newPassword');
  73. $new1 = Helper::getPostString('confirmPassword');
  74. if (!$old || !$new) {
  75. Helper::error('参数错误');
  76. }
  77. if ( $new != $new1){
  78. Helper::error('新密码不一致');
  79. }
  80. $info = DB::getInfoById('useradmin', \Yii::app()->user->_id);
  81. if (!$info) {
  82. Helper::error('用户未找到');
  83. }
  84. if (md5($old) != $info['password']) {
  85. Helper::error('旧密码错误');
  86. }
  87. DB::updateById('useradmin', ['password' => md5($new)], \Yii::app()->user->_id);
  88. Helper::ok();
  89. }
  90. public function actionEditUser()
  91. {
  92. $info = [
  93. 'username' => Helper::getPostString('username'),
  94. 'phone' => Helper::getPostString('phone'),
  95. 'email' => Helper::getPostString('email'),
  96. 'descr' => Helper::getPostString('descr'),
  97. 'sex' => Helper::getPostInt('sex'),
  98. ];
  99. if (!Helper::checkEmptyKey($info, ['username', 'phone', 'email'])) {
  100. Helper::error('参数错误');
  101. }
  102. DB::updateById('useradmin', $info, \Yii::app()->user->_id);
  103. Helper::ok();
  104. }
  105. /**
  106. * Logs out the current user and redirect to homepage.
  107. */
  108. public function actionLogout()
  109. {
  110. Yii::app()->user->logout();
  111. Helper::ok();
  112. }
  113. }