SiteController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace merchant\controllers;
  3. use common\enums\AccessTokenGroupEnum;
  4. use common\enums\MemberTypeEnum;
  5. use Yii;
  6. use yii\web\NotFoundHttpException;
  7. use yii\web\Response;
  8. use yii\web\UnauthorizedHttpException;
  9. use yii\web\Controller;
  10. use yii\filters\VerbFilter;
  11. use yii\filters\AccessControl;
  12. use common\helpers\ResultHelper;
  13. use common\models\extend\SmsLog;
  14. use common\enums\AppEnum;
  15. use common\enums\OperatingTypeEnum;
  16. use common\enums\WhetherEnum;
  17. use common\traits\BaseAction;
  18. use merchant\forms\SmsCodeForm;
  19. use merchant\forms\LoginForm;
  20. use merchant\forms\SignUpForm;
  21. use addons\Merchants\common\models\SettingForm;
  22. /**
  23. * Class SiteController
  24. * @package merchant\controllers
  25. * @author jianyan74 <751393839@qq.com>
  26. */
  27. class SiteController extends Controller
  28. {
  29. use BaseAction;
  30. /**
  31. * @var string
  32. */
  33. public $layout = "@backend/views/layouts/blank";
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function behaviors()
  38. {
  39. return [
  40. 'access' => [
  41. 'class' => AccessControl::class,
  42. 'rules' => [
  43. [
  44. 'actions' => ['login', 'get-wechat-login-qr', 'qr', 'wechat-login', 'register', 'sms-code', 'register-protocol', 'error', 'captcha'],
  45. 'allow' => true,
  46. ],
  47. [
  48. 'actions' => ['logout'],
  49. 'allow' => true,
  50. 'roles' => ['@'],
  51. ],
  52. ],
  53. ],
  54. 'verbs' => [
  55. 'class' => VerbFilter::class,
  56. 'actions' => [
  57. 'logout' => ['post'],
  58. ],
  59. ],
  60. ];
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function actions()
  66. {
  67. return [
  68. 'error' => [
  69. 'class' => 'yii\web\ErrorAction',
  70. // 'layout' => '@backend/views/layouts/blank'
  71. ],
  72. 'captcha' => [
  73. 'class' => 'yii\captcha\CaptchaAction',
  74. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  75. 'maxLength' => 6, // 最大显示个数
  76. 'minLength' => 6, // 最少显示个数
  77. 'padding' => 5, // 间距
  78. 'height' => 32, // 高度
  79. 'width' => 100, // 宽度
  80. 'offset' => 4, // 设置字符偏移量
  81. 'backColor' => 0xffffff, // 背景颜色
  82. 'foreColor' => 0x62a8ea, // 字体颜色
  83. ],
  84. ];
  85. }
  86. /**
  87. * 登录
  88. *
  89. * @return string|\yii\web\Response
  90. * @throws \yii\base\InvalidConfigException
  91. */
  92. public function actionLogin()
  93. {
  94. if (!Yii::$app->has('merchantsService')) {
  95. throw new UnauthorizedHttpException('未安装商户插件,请联系管理员');
  96. }
  97. if (!Yii::$app->user->isGuest) {
  98. // 记录行为日志
  99. Yii::$app->services->actionLog->create('login', '自动登录', 0, [], false);
  100. return $this->goHome();
  101. }
  102. $model = new LoginForm();
  103. $model->loginCaptchaRequired();
  104. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  105. // 记录行为日志
  106. Yii::$app->services->actionLog->create('login', '账号登录', 0, [], false);
  107. return $this->goHome();
  108. } else {
  109. $model->password = '';
  110. return $this->render('login', [
  111. 'model' => $model,
  112. 'hasWechat' => Yii::$app->has('wechatService'), // 微信插件是否安装
  113. ]);
  114. }
  115. }
  116. /**
  117. * 注册
  118. *
  119. * @return string|\yii\web\Response
  120. * @throws NotFoundHttpException
  121. */
  122. public function actionRegister()
  123. {
  124. if (!Yii::$app->has('merchantsService')) {
  125. throw new UnauthorizedHttpException('未安装商户插件,请联系管理员');
  126. }
  127. /** @var SettingForm $setting */
  128. $setting = Yii::$app->merchantsService->config->setting();
  129. // 判断开放注册
  130. if (empty($setting->register_apply)){
  131. throw new NotFoundHttpException('找不到页面');
  132. }
  133. $model = new SignUpForm();
  134. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  135. if ($model->register()) {
  136. return $setting->register_auto_pass == WhetherEnum::ENABLED
  137. ? $this->redirect(['login'])
  138. : $this->message('等待管理员审核中...', $this->redirect(['login']));
  139. }
  140. return $this->redirect(['register']);
  141. }
  142. return $this->render($this->action->id, [
  143. 'model' => $model,
  144. 'registerProtocolTitle' => $setting->register_protocol_title,
  145. 'merchantCate' => Yii::$app->merchantsService->cate->findAll(),
  146. 'authRoleEnter' => Yii::$app->services->rbacAuthRole->getMapList(AppEnum::MERCHANT, 0, ['operating_type' => OperatingTypeEnum::ENTER]),
  147. ]);
  148. }
  149. /**
  150. * 获取验证码
  151. *
  152. * @return int|mixed
  153. * @throws \yii\web\UnprocessableEntityHttpException
  154. */
  155. public function actionSmsCode()
  156. {
  157. if (!Yii::$app->has('merchantsService')) {
  158. throw new UnauthorizedHttpException('未安装商户插件,请联系管理员');
  159. }
  160. $setting = Yii::$app->merchantsService->config->setting();
  161. // 判断开放注册
  162. if (empty($setting->register_apply)){
  163. throw new NotFoundHttpException('找不到页面');
  164. }
  165. $model = new SmsCodeForm();
  166. $model->attributes = Yii::$app->request->post();
  167. if (!$model->validate()) {
  168. return ResultHelper::json(422, Yii::$app->services->base->analysisErr($model->getFirstErrors()));
  169. }
  170. // 测试
  171. if (YII_DEBUG) {
  172. $code = rand(1000, 9999);
  173. $log = new SmsLog();
  174. $log = $log->loadDefaultValues();
  175. $log->attributes = [
  176. 'mobile' => $model->mobile,
  177. 'code' => $code,
  178. 'member_id' => 0,
  179. 'usage' => $model->usage,
  180. 'error_code' => 200,
  181. 'error_msg' => 'ok',
  182. 'error_data' => '',
  183. ];
  184. $log->save();
  185. return ResultHelper::json(200, '发送成功', [
  186. 'code' => $code
  187. ]);
  188. }
  189. // 发送短信
  190. $model->send();
  191. return ResultHelper::json(200, '发送成功', []);
  192. }
  193. /**
  194. * 注册协议
  195. *
  196. * @return string
  197. * @throws NotFoundHttpException
  198. */
  199. public function actionRegisterProtocol()
  200. {
  201. $setting = Yii::$app->merchantsService->config->setting();
  202. // 判断开放注册
  203. if (empty($setting->register_apply)){
  204. throw new NotFoundHttpException('找不到页面');
  205. }
  206. return $this->render($this->action->id, [
  207. 'register_protocol_title' => $setting->register_protocol_title,
  208. 'register_protocol' => $setting->register_protocol,
  209. ]);
  210. }
  211. /**
  212. * 微信登录
  213. *
  214. * @param $uuid
  215. * @return mixed
  216. * @throws \yii\base\InvalidConfigException
  217. */
  218. public function actionWechatLogin($ticket)
  219. {
  220. if (!Yii::$app->has('merchantsService')) {
  221. throw new UnauthorizedHttpException('未安装商户插件,请联系管理员');
  222. }
  223. $data = Yii::$app->wechatService->qrcode->findByWhere([
  224. 'ticket' => $ticket
  225. ]);
  226. if (empty($data)) {
  227. return ResultHelper::json(422, '无效的ticket');
  228. }
  229. if ($data['end_time'] <= time()) {
  230. return ResultHelper::json(422, '无效的ticket');
  231. }
  232. if (empty($data['extend']['openid'])) {
  233. return ResultHelper::json(422, '未登录');
  234. }
  235. $auth = Yii::$app->services->memberAuth->findOauthClient(AccessTokenGroupEnum::WECHAT_MP, $data['extend']['openid'], MemberTypeEnum::MERCHANT);
  236. if (empty($auth) || empty($auth->member)) {
  237. return ResultHelper::json(422, '未绑定账号');
  238. }
  239. // 登录
  240. Yii::$app->user->login($auth->member);
  241. // 记录行为日志
  242. Yii::$app->services->actionLog->create('login', '二维码登录', 0, [], false);
  243. return ResultHelper::json(200, '登录成功');
  244. }
  245. /**
  246. * 微信登录
  247. *
  248. * @param $uuid
  249. * @return mixed
  250. * @throws \yii\base\InvalidConfigException
  251. */
  252. public function actionGetWechatLoginQr()
  253. {
  254. if (!Yii::$app->has('merchantsService')) {
  255. throw new UnauthorizedHttpException('未安装商户插件,请联系管理员');
  256. }
  257. try {
  258. $data = Yii::$app->wechatService->qrcode->syncCreateByData([
  259. 'name' => '账号绑定',
  260. 'model_type' => 1,
  261. 'expire_seconds' => 5 * 60,
  262. 'extend' => [
  263. 'type' => 'merchantLogin',
  264. 'member_id' => -1,
  265. 'remind' => [
  266. 'success' => '商户登录成功, 操作时间: {time}',
  267. 'error' => '登录失败,未绑定商户账号, 操作时间: {time}',
  268. ]
  269. ],
  270. ]);
  271. $data->save();
  272. return ResultHelper::json(200, '返回登录', [
  273. 'ticket' => $data['ticket'],
  274. 'url' => $data['url'],
  275. 'expire_seconds' => $data['expire_seconds'],
  276. ]);
  277. } catch (\Exception $e) {
  278. return ResultHelper::json(422, $e->getMessage());
  279. }
  280. }
  281. /**
  282. * 二维码显示
  283. *
  284. * @param $uuid
  285. * @return mixed
  286. * @throws \yii\base\InvalidConfigException
  287. */
  288. public function actionQr($url)
  289. {
  290. $qr = Yii::$app->get('qr');
  291. Yii::$app->response->format = Response::FORMAT_RAW;
  292. Yii::$app->response->headers->add('Content-Type', $qr->getContentType());
  293. return $qr->setText($url)
  294. ->setErrorCorrectionLevel('quartile')
  295. ->setSize(200)
  296. ->setMargin(7)
  297. ->writeString();
  298. }
  299. /**
  300. * @return \yii\web\Response
  301. * @throws \yii\base\InvalidConfigException
  302. */
  303. public function actionLogout()
  304. {
  305. Yii::$app->services->actionLog->create('logout', '退出登录');
  306. Yii::$app->user->logout();
  307. return $this->goHome();
  308. }
  309. }
粤ICP备19079148号