SettingsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/26
  6. * Time: 下午10:41
  7. */
  8. namespace common\modules\user\frontend\controllers;
  9. use common\modules\attachment\models\Attachment;
  10. use common\modules\user\models\Auth;
  11. use common\modules\user\models\Profile;
  12. use common\modules\user\models\Token;
  13. use common\modules\user\models\User;
  14. use Yii;
  15. use yii\filters\AccessControl;
  16. use yii\imagine\Image;
  17. use yii\web\Controller;
  18. use yii\web\NotFoundHttpException;
  19. class SettingsController extends Controller
  20. {
  21. public function behaviors()
  22. {
  23. return [
  24. 'accessControl' => [
  25. 'class' => AccessControl::className(),
  26. 'rules' => [
  27. [
  28. 'allow' => true,
  29. 'roles' => ['@'],
  30. ],
  31. ],
  32. ]
  33. ];
  34. }
  35. public function actionBasic()
  36. {
  37. return $this->render('basic', [
  38. 'model' => Yii::$app->user->identity
  39. ]);
  40. }
  41. public function actionProfile()
  42. {
  43. $userId = \Yii::$app->user->id;
  44. $profile = Profile::find()->where(['user_id' => $userId])->one();
  45. if ($profile->load(\Yii::$app->request->post()) && $profile->save()) {
  46. return $this->redirect(['profile']);
  47. } else {
  48. return $this->render('profile', [
  49. 'model' => $profile
  50. ]);
  51. }
  52. }
  53. public function actionAvatar()
  54. {
  55. $user = \Yii::$app->user->identity;
  56. if (Yii::$app->getRequest()->isAjax) {
  57. $avatar = Yii::$app->getRequest()->post("avatar");
  58. $x = Yii::$app->getRequest()->post("x");
  59. $y = Yii::$app->getRequest()->post("y");
  60. $w = Yii::$app->getRequest()->post("w");
  61. $h = Yii::$app->getRequest()->post("h");
  62. $cropUrl = Yii::$app->storage->crop($avatar, $w, $h, [$x, $y]);
  63. //再传一次裁剪后的图片
  64. list($cropAttachment, $error) = Attachment::uploadFromUrl('avatar/' . $user->id, $cropUrl);
  65. if ($error == null) {
  66. $user->saveAvatar($cropAttachment->url);
  67. Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  68. $result = [
  69. 'status' => true,
  70. 'msg' => '保存成功'
  71. ];
  72. } else {
  73. $result = [
  74. 'status' => false,
  75. 'msg' => $error
  76. ];
  77. }
  78. return $result;
  79. }
  80. return $this->render('avatar', [
  81. 'user' => $user
  82. ]);
  83. }
  84. public function actionAuth()
  85. {
  86. return $this->render('auth');
  87. }
  88. public function actionDisconnect($id)
  89. {
  90. $auth = Auth::find()->where(['user_id' => Yii::$app->user->id, 'source' => $id])->one();
  91. if ($auth === null) {
  92. throw new NotFoundHttpException();
  93. }
  94. $auth->delete();
  95. return $this->redirect(['auth']);
  96. }
  97. public function actionSendConfirm()
  98. {
  99. Yii::$app->response->format = 'json';
  100. $email = Yii::$app->request->post('email');
  101. /**
  102. * @var User $user
  103. */
  104. $user = Yii::$app->user->identity;
  105. list($result, $msg) = $user->sendConfirm($email);
  106. if ($result) {
  107. return [
  108. 'status' => 1,
  109. 'msg' => \Yii::t(
  110. 'user',
  111. 'A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'
  112. )
  113. ];
  114. } else {
  115. return [
  116. 'status' => 0,
  117. 'msg' => $msg
  118. ];
  119. }
  120. }
  121. }