PassportController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\models\common\InviteCode;
  9. use app\models\SaasUser;
  10. use app\models\User;
  11. use app\modules\client\controllers\BaseController;
  12. use app\modules\client\models\v1\AuthLoginForm;
  13. use app\modules\client\models\v1\LoginForm;
  14. use yii\base\BaseObject;
  15. class PassportController extends BaseController
  16. {
  17. /**
  18. * 登录
  19. * @return \yii\web\Response
  20. * @throws \yii\base\Exception
  21. */
  22. public function actionLogin()
  23. {
  24. $form = new LoginForm();
  25. $form->scenario = get_params('_platform') ?: post_params('_platform');
  26. $form->attributes = all_params();
  27. $form->store_id = get_store_id();
  28. return $this->asJson($form->login());
  29. }
  30. public function actionEditPassword()
  31. {
  32. $form = new AuthLoginForm();
  33. $form->attributes = all_params();
  34. $form->store_id = get_store_id();
  35. return $this->asJson($form->editPassword());
  36. }
  37. //忘记密码
  38. public function actionForgetPassword()
  39. {
  40. $form = new AuthLoginForm();
  41. $form->attributes = all_params();
  42. $form->store_id = get_store_id();
  43. return $this->asJson($form->forgetPassword());
  44. }
  45. //使用账户密码注册账户
  46. public function actionUserPasswordRegedit()
  47. {
  48. $form = new AuthLoginForm();
  49. $form->attributes = all_params();
  50. $form->store_id = get_store_id();
  51. return $this->asJson($form->userPasswordRegedit());
  52. }
  53. public function actionCode()
  54. {
  55. $form = new AuthLoginForm();
  56. $form->attributes = all_params();
  57. $form->store_id = get_store_id();
  58. return $this->asJson($form->code());
  59. }
  60. public function actionGetUserInfoById()
  61. {
  62. $form = new AuthLoginForm();
  63. $form->attributes = all_params();
  64. $form->store_id = get_store_id();
  65. return $this->asJson($form->getUserInfoById());
  66. }
  67. public function actionGetUserInfoByMobile()
  68. {
  69. $form = new AuthLoginForm();
  70. $form->attributes = all_params();
  71. $form->store_id = get_store_id();
  72. return $this->asJson($form->getUserInfoByMobile());
  73. }
  74. /**
  75. * 发送验证码
  76. * @return \yii\web\Response
  77. */
  78. public function actionSendCode()
  79. {
  80. $form = new LoginForm();
  81. $form->phone = post_params('phone');
  82. $form->store_id = get_store_id();
  83. if (trim(post_params('code_type')) == LoginForm::TYPE_VERIFY_CODE_LOGIN) {
  84. return $this->asJson($form->sendCode(LoginForm::CACHE_KEY_SMS_LOGIN));
  85. } else if (trim(post_params('code_type')) == LoginForm::TYPE_VERIFY_CODE_BIND) {
  86. return $this->asJson($form->sendCode(LoginForm::CACHE_KEY_BIND_PHONE));
  87. } else if (trim(post_params('code_type')) == LoginForm::TYPE_VERIFY_CODE_FORGET_PASSWORD) {
  88. return $this->asJson($form->sendCode(LoginForm::CACHE_KEY_SMS_FORGET_PASSWORD));
  89. } else {
  90. return $this->asJson(['code' => 1, 'msg' => '参数不正确']);
  91. }
  92. }
  93. /**
  94. * 绑定手机号
  95. * @return \yii\web\Response
  96. */
  97. public function actionBindPhone()
  98. {
  99. $form = new LoginForm();
  100. $form->attributes = post_params();
  101. $form->user = get_user();
  102. return $this->asJson($form->bindPhone());
  103. }
  104. /**
  105. * 独立小程序登录手机号登录
  106. */
  107. public function actionAuthPhone()
  108. {
  109. $form = new AuthLoginForm();
  110. $form->attributes = all_params();
  111. $form->store_id = get_store_id();
  112. return $this->asJson($form->phoneAuth());
  113. }
  114. /**
  115. * 平台小程序手机号登录(平台个人中心,店铺个人中心)
  116. */
  117. public function actionPlatformAuthPhone()
  118. {
  119. $form = new AuthLoginForm();
  120. $form->attributes = all_params();
  121. $form->store_id = get_store_id();
  122. return $this->asJson($form->platformPhoneAuth());
  123. }
  124. public function actionLoginByOpenid()
  125. {
  126. $form = new AuthLoginForm();
  127. $form->attributes = all_params();
  128. $form->store_id = get_store_id();
  129. return $this->asJson($form->loginByOpenid());
  130. }
  131. public function actionLoginByUserId()
  132. {
  133. $form = new AuthLoginForm();
  134. $form->attributes = all_params();
  135. $form->store_id = get_store_id();
  136. return $this->asJson($form->loginByUserId());
  137. }
  138. // 用于供应链小程序切换商城后自动登录
  139. public function actionSwitchStoreAutoLogin()
  140. {
  141. $form = new AuthLoginForm();
  142. $form->attributes = all_params();
  143. $form->store_id = get_store_id();
  144. return $this->asJson($form->loginByAuto());
  145. }
  146. public function actionGetOptionByH5()
  147. {
  148. $form = new AuthLoginForm();
  149. $form->attributes = all_params();
  150. $form->store_id = get_store_id();
  151. return $this->asJson($form->getOptionByH5());
  152. }
  153. public function actionGetRedirectUri() {
  154. $form = new AuthLoginForm();
  155. $form->attributes = all_params();
  156. $form->store_id = get_store_id();
  157. return $this->asJson($form->getRedirectUri());
  158. }
  159. public function actionSetH5UserInfo() {
  160. $form = new AuthLoginForm();
  161. $form->attributes = all_params();
  162. $form->store_id = get_store_id();
  163. return $this->asJson($form->setH5UserInfo());
  164. }
  165. public function actionCaptchaGen()
  166. {
  167. $session = get_params('session');
  168. $type = get_params('type');
  169. $refresh = (bool)get_params('refresh', false);
  170. if (!$session) {
  171. return $this->asJson(['code' => 1, 'msg' => '参数错误']);
  172. }
  173. $key = 'captcha_' . $session;
  174. $data = cache()->get($key);
  175. if (!$data && !$refresh) {
  176. return $this->asJson(['code' => 1, 'msg' => '验证码已过期, 请刷新!']);
  177. }
  178. if ($refresh) {
  179. $data = [
  180. 'x' => rand(130, 550),
  181. 'y' => rand(50, 260),
  182. 'img' => rand(1, 61) . '.jpg',
  183. 'template' => 'template/' . rand(1, 4) . '.png',
  184. 'opacity' => rand(30, 80),
  185. 'error' => 2, // 初始错误次数为2, 可失败3次, 超过就要刷新
  186. ];
  187. cache()->set($key, $data, 600);
  188. return $this->asJson(['code' => 0, 'msg' => '验证码已刷新']);
  189. }
  190. try {
  191. $x = $data['x'];
  192. $y = $data['y'];
  193. $img = $data['img'];
  194. $template = $data['template'];
  195. $opacity = $data['opacity'];
  196. $basePath = \Yii::$app->basePath . '/web/statics/captcha/';
  197. $src = imagecreatefromjpeg($basePath . $img);
  198. $resImage = imagecreatetruecolor(90, 90);
  199. $transparent = imagecolorallocatealpha($resImage, 255, 255, 255, 127);
  200. imagecolortransparent($resImage, $transparent);
  201. imagefill($resImage, 0, 0, $transparent);
  202. $tempImage = @imagecreatefrompng($basePath . $template);
  203. for ($i = 0; $i < 90; $i++) {
  204. for ($j = 0; $j < 90; $j++) {
  205. if (imagecolorat($tempImage, $i, $j) !== 0) {
  206. $rgb = imagecolorat($src, $x + $i, $y + $j);
  207. imagesetpixel($resImage, $i, $j, $rgb);
  208. }
  209. }
  210. }
  211. if ($type == 'main') {
  212. $mengban = imagecreatetruecolor(90, 90);
  213. imagecolortransparent($mengban, $transparent);
  214. imagefill($mengban, 0, 0, $transparent);
  215. $huise = imagecolorallocatealpha($resImage, 255, 255, 255, $opacity);
  216. for ($i = 0; $i < 90; $i++) {
  217. for ($j = 0; $j < 90; $j++) {
  218. $rgb = imagecolorat($resImage, $i, $j);
  219. if ($rgb !== 2147483647) {
  220. imagesetpixel($mengban, $i, $j, $huise);
  221. }
  222. }
  223. }
  224. imagecopyresampled(
  225. $src,
  226. $resImage,
  227. $x, $y,
  228. 0,0,
  229. 90, 90,
  230. 90, 90
  231. );
  232. imagecopyresampled(
  233. $src,
  234. $mengban,
  235. $x + 1, $y + 1,
  236. 0,0,
  237. 90 - 2, 90 - 2,
  238. 90, 90
  239. );
  240. \ob_start();
  241. imagejpeg($src);
  242. $data = \ob_get_clean();
  243. \Yii::$app->response->headers->add('Content-Type', 'image/jpeg');
  244. \Yii::$app->response->data = $data;
  245. }
  246. if ($type == 'secondary') {
  247. $white = imagecolorallocatealpha($resImage, 255, 255, 255, 1);
  248. for ($i = 0; $i < 90; $i++) {
  249. for ($j = 0; $j < 90; $j++) {
  250. if(imagecolorat($tempImage, $i, $j) === 0) {
  251. imagesetpixel($resImage, $i, $j, $white);
  252. }
  253. }
  254. }
  255. $resImage2 = imagecreatetruecolor(90, 382);
  256. imagecolortransparent($resImage2, $transparent);
  257. imagefill($resImage2, 0, 0, $transparent);
  258. imagecopyresampled(
  259. $resImage2,
  260. $resImage,
  261. 0, $y,
  262. 0, 0,
  263. 90, 90,
  264. 90, 90
  265. );
  266. \ob_start();
  267. imagepng($resImage2);
  268. $data = ob_get_clean();
  269. \Yii::$app->response->headers->add('Content-Type', 'image/png');
  270. \Yii::$app->response->data = $data;
  271. }
  272. \Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
  273. return \Yii::$app->response;
  274. } catch (\Throwable $e) {
  275. return $this->asJson(['code' => 1, 'msg' => $e->getMessage()]);
  276. }
  277. }
  278. public function actionCaptchaCheck()
  279. {
  280. $session = get_params('session');
  281. $touchX = get_params('touch_x');
  282. $key = 'captcha_' . $session;
  283. if (!$session || !$touchX) {
  284. return $this->asJson(['code' => 1, 'msg' => '参数错误']);
  285. }
  286. $data = cache()->get($key);
  287. if (!$data) {
  288. return $this->asJson(['code' => 1, 'msg' => '验证码已过期, 请刷新']);
  289. }
  290. if ($data['error'] <= 0) {
  291. return $this->asJson(['code' => 1, 'msg' => '验证失败次数过多']);
  292. }
  293. if ($touchX < $data['x'] - 5 || $touchX > $data['x'] + 5) {
  294. $data['error']--;
  295. cache()->set($key, $data, 600);
  296. return $this->asJson(['code' => 1, 'msg' => '验证失败,请重试']);
  297. }
  298. cache()->delete($key);
  299. return $this->asJson(['code' => 0, 'msg' => '验证成功!']);
  300. }
  301. /**
  302. * 获取邀请码用户id
  303. */
  304. public function actionGetUserInfoByInvite()
  305. {
  306. $invite_code = input_params('invite_code', '');
  307. if(!$invite_code){
  308. return $this->asJson(['code' => 1, 'msg' => '请填写邀请码']);
  309. }
  310. //邀请码解密
  311. $uid = InviteCode::code2ID($invite_code);
  312. if(!$uid){
  313. return $this->asJson(['code' => 1, 'msg' => '邀请码不存在']);
  314. }
  315. $user = User::findOne(['id'=>$uid,'is_delete' => 0]);
  316. if(!$user){
  317. return $this->asJson(['code' => 1, 'msg' => '用户不存在']);
  318. }
  319. $data = [
  320. 'id' => $user->id,
  321. 'avatar_url' => $user->avatar_url,
  322. 'nickname' => $user->nickname,
  323. 'mobile' => $user->binding,
  324. ];
  325. $saasUser = SaasUser::find()->where(['mobile' => $user->binding])->one();
  326. if ($saasUser) {
  327. $data['avatar_url'] = $saasUser->avatar;
  328. $data['nickname'] = $saasUser->name;
  329. $data['mobile'] = $saasUser->mobile;
  330. }
  331. return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $data]);
  332. }
  333. }