CommonController.php 3.7 KB

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