| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\controllers\v1;
- use app\models\common\InviteCode;
- use app\models\SaasUser;
- use app\models\User;
- use app\modules\client\controllers\BaseController;
- use app\modules\client\models\v1\AuthLoginForm;
- use app\modules\client\models\v1\LoginForm;
- use yii\base\BaseObject;
- class PassportController extends BaseController
- {
- /**
- * 登录
- * @return \yii\web\Response
- * @throws \yii\base\Exception
- */
- public function actionLogin()
- {
- $form = new LoginForm();
- $form->scenario = get_params('_platform') ?: post_params('_platform');
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->login());
- }
- public function actionEditPassword()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->editPassword());
- }
- //忘记密码
- public function actionForgetPassword()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->forgetPassword());
- }
- //使用账户密码注册账户
- public function actionUserPasswordRegedit()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->userPasswordRegedit());
- }
- public function actionCode()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->code());
- }
- public function actionGetUserInfoById()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->getUserInfoById());
- }
- public function actionGetUserInfoByMobile()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->getUserInfoByMobile());
- }
- /**
- * 发送验证码
- * @return \yii\web\Response
- */
- public function actionSendCode()
- {
- $form = new LoginForm();
- $form->phone = post_params('phone');
- $form->store_id = get_store_id();
- if (trim(post_params('code_type')) == LoginForm::TYPE_VERIFY_CODE_LOGIN) {
- return $this->asJson($form->sendCode(LoginForm::CACHE_KEY_SMS_LOGIN));
- } else if (trim(post_params('code_type')) == LoginForm::TYPE_VERIFY_CODE_BIND) {
- return $this->asJson($form->sendCode(LoginForm::CACHE_KEY_BIND_PHONE));
- } else if (trim(post_params('code_type')) == LoginForm::TYPE_VERIFY_CODE_FORGET_PASSWORD) {
- return $this->asJson($form->sendCode(LoginForm::CACHE_KEY_SMS_FORGET_PASSWORD));
- } else {
- return $this->asJson(['code' => 1, 'msg' => '参数不正确']);
- }
- }
- /**
- * 绑定手机号
- * @return \yii\web\Response
- */
- public function actionBindPhone()
- {
- $form = new LoginForm();
- $form->attributes = post_params();
- $form->user = get_user();
- return $this->asJson($form->bindPhone());
- }
- /**
- * 独立小程序登录手机号登录
- */
- public function actionAuthPhone()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->phoneAuth());
- }
- /**
- * 平台小程序手机号登录(平台个人中心,店铺个人中心)
- */
- public function actionPlatformAuthPhone()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->platformPhoneAuth());
- }
- public function actionLoginByOpenid()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->loginByOpenid());
- }
- public function actionLoginByUserId()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->loginByUserId());
- }
- // 用于供应链小程序切换商城后自动登录
- public function actionSwitchStoreAutoLogin()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->loginByAuto());
- }
- public function actionGetOptionByH5()
- {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->getOptionByH5());
- }
- public function actionGetRedirectUri() {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->getRedirectUri());
- }
- public function actionSetH5UserInfo() {
- $form = new AuthLoginForm();
- $form->attributes = all_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->setH5UserInfo());
- }
- public function actionCaptchaGen()
- {
- $session = get_params('session');
- $type = get_params('type');
- $refresh = (bool)get_params('refresh', false);
- if (!$session) {
- return $this->asJson(['code' => 1, 'msg' => '参数错误']);
- }
- $key = 'captcha_' . $session;
- $data = cache()->get($key);
- if (!$data && !$refresh) {
- return $this->asJson(['code' => 1, 'msg' => '验证码已过期, 请刷新!']);
- }
- if ($refresh) {
- $data = [
- 'x' => rand(130, 550),
- 'y' => rand(50, 260),
- 'img' => rand(1, 61) . '.jpg',
- 'template' => 'template/' . rand(1, 4) . '.png',
- 'opacity' => rand(30, 80),
- 'error' => 2, // 初始错误次数为2, 可失败3次, 超过就要刷新
- ];
- cache()->set($key, $data, 600);
- return $this->asJson(['code' => 0, 'msg' => '验证码已刷新']);
- }
- try {
- $x = $data['x'];
- $y = $data['y'];
- $img = $data['img'];
- $template = $data['template'];
- $opacity = $data['opacity'];
- $basePath = \Yii::$app->basePath . '/web/statics/captcha/';
- $src = imagecreatefromjpeg($basePath . $img);
- $resImage = imagecreatetruecolor(90, 90);
- $transparent = imagecolorallocatealpha($resImage, 255, 255, 255, 127);
- imagecolortransparent($resImage, $transparent);
- imagefill($resImage, 0, 0, $transparent);
- $tempImage = @imagecreatefrompng($basePath . $template);
- for ($i = 0; $i < 90; $i++) {
- for ($j = 0; $j < 90; $j++) {
- if (imagecolorat($tempImage, $i, $j) !== 0) {
- $rgb = imagecolorat($src, $x + $i, $y + $j);
- imagesetpixel($resImage, $i, $j, $rgb);
- }
- }
- }
- if ($type == 'main') {
- $mengban = imagecreatetruecolor(90, 90);
- imagecolortransparent($mengban, $transparent);
- imagefill($mengban, 0, 0, $transparent);
- $huise = imagecolorallocatealpha($resImage, 255, 255, 255, $opacity);
- for ($i = 0; $i < 90; $i++) {
- for ($j = 0; $j < 90; $j++) {
- $rgb = imagecolorat($resImage, $i, $j);
- if ($rgb !== 2147483647) {
- imagesetpixel($mengban, $i, $j, $huise);
- }
- }
- }
- imagecopyresampled(
- $src,
- $resImage,
- $x, $y,
- 0,0,
- 90, 90,
- 90, 90
- );
- imagecopyresampled(
- $src,
- $mengban,
- $x + 1, $y + 1,
- 0,0,
- 90 - 2, 90 - 2,
- 90, 90
- );
- \ob_start();
- imagejpeg($src);
- $data = \ob_get_clean();
- \Yii::$app->response->headers->add('Content-Type', 'image/jpeg');
- \Yii::$app->response->data = $data;
- }
- if ($type == 'secondary') {
- $white = imagecolorallocatealpha($resImage, 255, 255, 255, 1);
- for ($i = 0; $i < 90; $i++) {
- for ($j = 0; $j < 90; $j++) {
- if(imagecolorat($tempImage, $i, $j) === 0) {
- imagesetpixel($resImage, $i, $j, $white);
- }
- }
- }
- $resImage2 = imagecreatetruecolor(90, 382);
- imagecolortransparent($resImage2, $transparent);
- imagefill($resImage2, 0, 0, $transparent);
- imagecopyresampled(
- $resImage2,
- $resImage,
- 0, $y,
- 0, 0,
- 90, 90,
- 90, 90
- );
- \ob_start();
- imagepng($resImage2);
- $data = ob_get_clean();
- \Yii::$app->response->headers->add('Content-Type', 'image/png');
- \Yii::$app->response->data = $data;
- }
- \Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
- return \Yii::$app->response;
- } catch (\Throwable $e) {
- return $this->asJson(['code' => 1, 'msg' => $e->getMessage()]);
- }
- }
- public function actionCaptchaCheck()
- {
- $session = get_params('session');
- $touchX = get_params('touch_x');
- $key = 'captcha_' . $session;
- if (!$session || !$touchX) {
- return $this->asJson(['code' => 1, 'msg' => '参数错误']);
- }
- $data = cache()->get($key);
- if (!$data) {
- return $this->asJson(['code' => 1, 'msg' => '验证码已过期, 请刷新']);
- }
- if ($data['error'] <= 0) {
- return $this->asJson(['code' => 1, 'msg' => '验证失败次数过多']);
- }
- if ($touchX < $data['x'] - 5 || $touchX > $data['x'] + 5) {
- $data['error']--;
- cache()->set($key, $data, 600);
- return $this->asJson(['code' => 1, 'msg' => '验证失败,请重试']);
- }
- cache()->delete($key);
- return $this->asJson(['code' => 0, 'msg' => '验证成功!']);
- }
- /**
- * 获取邀请码用户id
- */
- public function actionGetUserInfoByInvite()
- {
- $invite_code = input_params('invite_code', '');
- if(!$invite_code){
- return $this->asJson(['code' => 1, 'msg' => '请填写邀请码']);
- }
- //邀请码解密
- $uid = InviteCode::code2ID($invite_code);
- if(!$uid){
- return $this->asJson(['code' => 1, 'msg' => '邀请码不存在']);
- }
- $user = User::findOne(['id'=>$uid,'is_delete' => 0]);
- if(!$user){
- return $this->asJson(['code' => 1, 'msg' => '用户不存在']);
- }
- $data = [
- 'id' => $user->id,
- 'avatar_url' => $user->avatar_url,
- 'nickname' => $user->nickname,
- 'mobile' => $user->binding,
- ];
- $saasUser = SaasUser::find()->where(['mobile' => $user->binding])->one();
- if ($saasUser) {
- $data['avatar_url'] = $saasUser->avatar;
- $data['nickname'] = $saasUser->name;
- $data['mobile'] = $saasUser->mobile;
- }
- return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $data]);
- }
- }
|