| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- * 只需要登入,无需检测权限的公共方法
- * 前端直接请求的话必须添加header Authorization: userStore.accessToken
- */
- class CommonController extends Controller
- {
- /**
- * 图片上传
- * 不同类型放到不同目录,返回格式也会不同
- */
- public function actionUploadImg()
- {
- $upType = '';
- $maxSize = 3;
- if (!empty($_FILES['follow'])) {
- $upType = 'follow';
- $upArr = $_FILES['follow'];
- } elseif (!empty($_FILES['editor'])) {
- $upType = 'editor';
- $upArr = $_FILES['editor'];
- } elseif (!empty($_FILES['avatar'])) {
- $upType = 'avatar';
- $upArr = $_FILES['avatar'];
- $maxSize = 0.3;
- } elseif (!empty($_FILES['canteen'])) {
- $upType = 'canteen';
- $upArr = $_FILES['canteen'];
- } else {
- Helper::error('上传有误');
- }
- $type = strtolower($upArr['type']);
- if (!Helper::hasAnyString($type, ['png', 'jpeg', 'jpg'])) {
- Helper::error('图片格式不正确 ' . $type);
- }
- if ($upArr['size'] > $maxSize * 1024 * 1024) {
- Helper::error("图片大小不能超过{$maxSize}M");
- }
- $ext = strtolower(pathinfo($upArr['name'], PATHINFO_EXTENSION));
- $upPath = "zqcrm/{$upType}/" . date('Ymd') . '/' . Helper::getRandomString(16) . '.' . $ext;
- $res = Helper::imageUpload($upArr['tmp_name'], $upPath);
- if (empty($res['code']) || $res['code'] != 200) {
- Helper::error($res['msg'] ?? '上传出错');
- }
- if ($upType == 'avatar') {
- $info = DB::getInfoById('useradmin', \Yii::app()->user->_id);
- Helper::imageDelete($info['avatar']);
- DB::updateById('useradmin', ['avatar' => $upPath], \Yii::app()->user->_id);
- }
- if ($upType == 'editor') {
- exit(json_encode([
- 'errno' => 0,
- 'data' => [
- 'url' => Helper::getImageUrl($upPath),
- ],
- ]));
- } else {
- Helper::ok(['name' => $upPath, 'url' => Helper::getImageUrl($upPath)]);
- }
- }
- public function actionDeleteImg()
- {
- $path = Helper::getPostString('path');
- if (empty($path)) {
- Helper::error('参数错误');
- }
- Helper::dealCommonResult(Helper::imageDelete($path));
- }
- public function actionChangePassword()
- {
- $old = Helper::getPostString('password');
- $new = Helper::getPostString('newPassword');
- $new1 = Helper::getPostString('confirmPassword');
- if (!$old || !$new) {
- Helper::error('参数错误');
- }
- if ( $new != $new1){
- Helper::error('新密码不一致');
- }
- $info = DB::getInfoById('useradmin', \Yii::app()->user->_id);
- if (!$info) {
- Helper::error('用户未找到');
- }
- if (md5($old) != $info['password']) {
- Helper::error('旧密码错误');
- }
- DB::updateById('useradmin', ['password' => md5($new)], \Yii::app()->user->_id);
- Helper::ok();
- }
- public function actionEditUser()
- {
- $info = [
- 'username' => Helper::getPostString('username'),
- 'phone' => Helper::getPostString('phone'),
- 'email' => Helper::getPostString('email'),
- 'descr' => Helper::getPostString('descr'),
- 'sex' => Helper::getPostInt('sex'),
- ];
- if (!Helper::checkEmptyKey($info, ['username', 'phone', 'email'])) {
- Helper::error('参数错误');
- }
- DB::updateById('useradmin', $info, \Yii::app()->user->_id);
- Helper::ok();
- }
- /**
- * Logs out the current user and redirect to homepage.
- */
- public function actionLogout()
- {
- Yii::app()->user->logout();
- Helper::ok();
- }
- }
|