SiteController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\filters\VerbFilter;
  6. use yii\filters\AccessControl;
  7. use yii\web\Response;
  8. use common\enums\AccessTokenGroupEnum;
  9. use common\enums\MemberTypeEnum;
  10. use common\helpers\ResultHelper;
  11. use backend\forms\LoginForm;
  12. /**
  13. * Class SiteController
  14. * @package backend\controllers
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class SiteController extends Controller
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'access' => [
  26. 'class' => AccessControl::class,
  27. 'rules' => [
  28. [
  29. 'actions' => ['login', 'get-wechat-login-qr', 'qr', 'wechat-login', 'error', 'captcha'],
  30. 'allow' => true,
  31. ],
  32. [
  33. 'actions' => ['logout'],
  34. 'allow' => true,
  35. 'roles' => ['@'],
  36. ],
  37. ],
  38. ],
  39. 'verbs' => [
  40. 'class' => VerbFilter::class,
  41. 'actions' => [
  42. 'logout' => ['post'],
  43. ],
  44. ],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function actions()
  51. {
  52. return [
  53. 'error' => [
  54. 'class' => 'yii\web\ErrorAction',
  55. ],
  56. 'captcha' => [
  57. 'class' => 'yii\captcha\CaptchaAction',
  58. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  59. 'maxLength' => 6, // 最大显示个数
  60. 'minLength' => 6, // 最少显示个数
  61. 'padding' => 5, // 间距
  62. 'height' => 32, // 高度
  63. 'width' => 100, // 宽度
  64. 'offset' => 4, // 设置字符偏移量
  65. 'backColor' => 0xffffff, // 背景颜色
  66. 'foreColor' => 0x62a8ea, // 字体颜色
  67. ]
  68. ];
  69. }
  70. /**
  71. * 登录
  72. *
  73. * @return string|\yii\web\Response
  74. * @throws \yii\base\InvalidConfigException
  75. */
  76. public function actionLogin()
  77. {
  78. if (!Yii::$app->user->isGuest) {
  79. // 记录行为日志
  80. Yii::$app->services->actionLog->create('login', '自动登录', 0, [], false);
  81. return $this->goHome();
  82. }
  83. $model = new LoginForm();
  84. $model->loginCaptchaRequired();
  85. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  86. // 记录行为日志
  87. Yii::$app->services->actionLog->create('login', '账号登录', 0, [], false);
  88. return $this->goHome();
  89. } else {
  90. $model->password = '';
  91. return $this->renderPartial('login', [
  92. 'model' => $model,
  93. 'hasWechat' => Yii::$app->has('wechatService'), // 微信插件是否安装
  94. ]);
  95. }
  96. }
  97. /**
  98. * 微信登录
  99. *
  100. * @param $uuid
  101. * @return mixed
  102. * @throws \yii\base\InvalidConfigException
  103. */
  104. public function actionWechatLogin($ticket)
  105. {
  106. $data = Yii::$app->wechatService->qrcode->findByWhere([
  107. 'ticket' => $ticket
  108. ]);
  109. if (empty($data)) {
  110. return ResultHelper::json(422, '无效的ticket');
  111. }
  112. if ($data['end_time'] <= time()) {
  113. return ResultHelper::json(422, '无效的ticket');
  114. }
  115. if (empty($data['extend']['openid'])) {
  116. return ResultHelper::json(422, '未登录');
  117. }
  118. $auth = Yii::$app->services->memberAuth->findOauthClient(AccessTokenGroupEnum::WECHAT_MP, $data['extend']['openid'], MemberTypeEnum::MANAGER);
  119. if (empty($auth) || empty($auth->member)) {
  120. return ResultHelper::json(422, '未绑定账号');
  121. }
  122. // 登录
  123. Yii::$app->user->login($auth->member);
  124. // 记录行为日志
  125. Yii::$app->services->actionLog->create('login', '二维码登录', 0, [], false);
  126. return ResultHelper::json(200, '登录成功');
  127. }
  128. /**
  129. * 微信登录
  130. *
  131. * @param $uuid
  132. * @return mixed
  133. * @throws \yii\base\InvalidConfigException
  134. */
  135. public function actionGetWechatLoginQr()
  136. {
  137. try {
  138. $data = Yii::$app->wechatService->qrcode->syncCreateByData([
  139. 'name' => '账号绑定',
  140. 'model_type' => 1,
  141. 'expire_seconds' => 5 * 60,
  142. 'extend' => [
  143. 'type' => 'login',
  144. 'member_id' => -1,
  145. 'remind' => [
  146. 'success' => '登录成功, 操作时间: {time}',
  147. 'error' => '登录失败,未绑定账号, 操作时间: {time}',
  148. ]
  149. ],
  150. ]);
  151. $data->save();
  152. return ResultHelper::json(200, '返回登录', [
  153. 'ticket' => $data['ticket'],
  154. 'url' => $data['url'],
  155. 'expire_seconds' => $data['expire_seconds'],
  156. ]);
  157. } catch (\Exception $e) {
  158. return ResultHelper::json(422, $e->getMessage());
  159. }
  160. }
  161. /**
  162. * 二维码显示
  163. *
  164. * @param $uuid
  165. * @return mixed
  166. * @throws \yii\base\InvalidConfigException
  167. */
  168. public function actionQr($url)
  169. {
  170. $qr = Yii::$app->get('qr');
  171. Yii::$app->response->format = Response::FORMAT_RAW;
  172. Yii::$app->response->headers->add('Content-Type', $qr->getContentType());
  173. return $qr->setText($url)
  174. ->setErrorCorrectionLevel('quartile')
  175. ->setSize(200)
  176. ->setMargin(7)
  177. ->writeString();
  178. }
  179. /**
  180. * @return \yii\web\Response
  181. * @throws \yii\base\InvalidConfigException
  182. */
  183. public function actionLogout()
  184. {
  185. Yii::$app->services->actionLog->create('logout', '退出登录');
  186. Yii::$app->user->logout();
  187. return $this->goHome();
  188. }
  189. }
粤ICP备19079148号