AuthController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\shop\classes\ShopClass;
  5. use bizHd\admin\classes\ShopAdminClass;
  6. use bizHd\admin\services\ShopAdminService;
  7. use bizHd\admin\services\AdminService;
  8. use bizHd\user\classes\UserClass;
  9. use bizHd\wx\services\WxOpenService;
  10. use common\components\dict;
  11. use common\components\httpUtil;
  12. use common\components\imgUtil;
  13. use common\components\jwt;
  14. use common\components\util;
  15. use Yii;
  16. use common\components\stringUtil;
  17. use biz\sj\services\MerchantService;
  18. use bizHd\user\services\UserService;
  19. use common\services\xhMerchantService;
  20. use common\components\wxUtil;
  21. use linslin\yii2\curl;
  22. use yii\helpers\Json;
  23. /**
  24. * 用户登陆
  25. */
  26. class AuthController extends PublicController
  27. {
  28. //微信手机号一键登录 shish 20210121
  29. public function actionWxMobileLogin()
  30. {
  31. $post = Yii::$app->request->post();
  32. $iv = $post['iv'];
  33. $encryptedData = $post['encryptedData'];
  34. $merchant = WxOpenService::getWxMiniBase();
  35. $appId = $merchant['miniAppId'];
  36. $miniOpenId = $post['miniOpenId'] ?? '';
  37. if (empty($miniOpenId)) {
  38. util::fail('登录失败...');
  39. }
  40. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  41. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  42. if (empty($sessionKey)) {
  43. util::fail('登录失败了哦');
  44. }
  45. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  46. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  47. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  48. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  49. if ($errCode != 0) {
  50. util::fail('登录失败..');
  51. }
  52. $arr = Json::decode($result);
  53. $phoneNumber = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  54. if (empty($phoneNumber)) {
  55. util::fail('登录失败了');
  56. }
  57. $admin = AdminService::getByCondition(['mobile' => $phoneNumber, 'style' => AdminClass::STYLE_HD]);
  58. if (empty($admin)) {
  59. util::fail('请先注册');
  60. }
  61. $openShop = $admin['openShop'] ?? 1;
  62. $currentShopId = $admin['currentShopId'] ?? 0;
  63. if (empty($currentShopId)) {
  64. if ($openShop == 2) {
  65. util::fail('审核中');
  66. }
  67. util::fail('请先注册');
  68. }
  69. $adminId = $admin['id'];
  70. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'shopId' => $currentShopId, 'delStatus' => 0], true);
  71. if (empty($shopAdmin)) {
  72. util::fail('没有权限操作');
  73. }
  74. if ($shopAdmin->status == 0) {
  75. util::fail("您的账号已冻结");
  76. }
  77. $shopId = $shopAdmin->shopId ?? 0;
  78. if (empty($shopId)) {
  79. util::fail('您不是管理员');
  80. }
  81. $token = jwt::getNewToken($adminId);
  82. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  83. $shopAdmin->save();
  84. $shopAdminId = $shopAdmin->id ?? 0;
  85. //是否有切换门店的权限
  86. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  87. $prefix = imgUtil::getPrefix();
  88. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  89. $admin['avatar'] = $avatar;
  90. $showDemo = 1;
  91. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  92. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  93. util::success([
  94. 'token' => $token,
  95. 'admin' => $admin,
  96. 'shopId' => $currentShopId,
  97. 'shopAdminId' => $shopAdminId,
  98. 'switchShop' => $switchShop,
  99. 'showDemo' => $showDemo,
  100. //有小程序新版本提示更新
  101. 'update' => $remind,
  102. ]);
  103. }
  104. //app登陆 shish 20211219
  105. public function actionAppLogin()
  106. {
  107. $get = Yii::$app->request->get();
  108. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  109. if (stringUtil::isMobile($mobile) == false) {
  110. util::fail('请填写正确手机号');
  111. }
  112. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  113. if (empty($password)) {
  114. util::fail('请输入密码');
  115. }
  116. $admin = AdminService::getByCondition(['mobile' => $mobile, 'style' => AdminClass::STYLE_HD]);
  117. if (password_verify($password, $admin['password']) == false) {
  118. util::fail('密码错误');
  119. }
  120. $openShop = $admin['openShop'] ?? 1;
  121. $currentShopId = $admin['currentShopId'] ?? 0;
  122. if (empty($currentShopId)) {
  123. if ($openShop == 2) {
  124. util::fail('审核中');
  125. }
  126. util::fail('请先注册');
  127. }
  128. $adminId = $admin['id'];
  129. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'shopId' => $currentShopId, 'delStatus' => 0], true);
  130. if (empty($shopAdmin)) {
  131. util::fail('没有权限操作');
  132. }
  133. if ($shopAdmin->status == 0) {
  134. util::fail("您的账号已冻结");
  135. }
  136. $shopId = $shopAdmin->shopId ?? 0;
  137. if (empty($shopId)) {
  138. util::fail('您不是管理员');
  139. }
  140. $token = jwt::getNewToken($adminId);
  141. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  142. $shopAdmin->save();
  143. $shopAdminId = $shopAdmin->id ?? 0;
  144. //是否有切换门店的权限
  145. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  146. $prefix = imgUtil::getPrefix();
  147. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  148. $admin['avatar'] = $avatar;
  149. $showDemo = 1;
  150. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  151. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  152. util::success([
  153. 'token' => $token,
  154. 'admin' => $admin,
  155. 'shopId' => $currentShopId,
  156. 'shopAdminId' => $shopAdminId,
  157. 'switchShop' => $switchShop,
  158. 'showDemo' => $showDemo,
  159. //有小程序新版本提示更新
  160. 'update' => $remind,
  161. ]);
  162. }
  163. //本机号码一键登录 shish 20210117
  164. public function actionPhoneLogin()
  165. {
  166. $get = Yii::$app->request->get();
  167. $access_token = $get['access_token'] ?? '';
  168. $openid = $get['openid'] ?? '';
  169. if (empty($access_token) || empty($openid)) {
  170. util::fail('参数错误');
  171. }
  172. //密钥,需要和uniCloud里的一致
  173. $secret = 'XHB2021198819640123';
  174. $params = ['access_token' => $access_token, 'openid' => $openid];
  175. $stringSignTemp = '';
  176. foreach ($params as $k => $v) {
  177. $stringSignTemp .= $k . '=' . $v . '&';
  178. }
  179. $stringSignTemp = rtrim($stringSignTemp, '&');
  180. $sign = hash_hmac('sha256', $stringSignTemp, $secret);
  181. $url = Yii::$app->params['hdUniCloudHost'] . "/getMobileNumber?access_token=" . $access_token . "&sign=" . $sign . "&" . $stringSignTemp;
  182. $curl = new curl\Curl();
  183. $respond = $curl->get($url);
  184. Yii::info($respond);
  185. $result = json_decode($respond, true);
  186. if (isset($result['code']) == false || $result['code'] != 1) {
  187. util::fail('获取手机号失败');
  188. }
  189. $phoneNumber = $result['data']['phoneNumber'] ?? '';
  190. if (empty($phoneNumber)) {
  191. util::fail('没有获取到手机号');
  192. }
  193. $admin = AdminService::getByCondition(['mobile' => $phoneNumber, 'style' => AdminClass::STYLE_HD]);
  194. if (empty($admin)) {
  195. util::fail('请先注册');
  196. }
  197. $openShop = $admin['openShop'] ?? 1;
  198. $currentShopId = $admin['currentShopId'] ?? 0;
  199. if (empty($currentShopId)) {
  200. if ($openShop == 2) {
  201. util::fail('审核中');
  202. }
  203. util::fail('请先注册');
  204. }
  205. $adminId = $admin['id'];
  206. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'shopId' => $currentShopId, 'delStatus' => 0], true);
  207. if (empty($shopAdmin)) {
  208. util::fail('没有权限操作');
  209. }
  210. if ($shopAdmin->status == 0) {
  211. util::fail("您的账号已冻结");
  212. }
  213. $shopId = $shopAdmin->shopId ?? 0;
  214. if (empty($shopId)) {
  215. util::fail('您不是管理员');
  216. }
  217. $token = jwt::getNewToken($adminId);
  218. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  219. $shopAdmin->save();
  220. $shopAdminId = $shopAdmin->id ?? 0;
  221. //是否有切换门店的权限
  222. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  223. $prefix = imgUtil::getPrefix();
  224. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  225. $admin['avatar'] = $avatar;
  226. $showDemo = 1;
  227. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  228. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  229. util::success([
  230. 'token' => $token,
  231. 'admin' => $admin,
  232. 'shopId' => $currentShopId,
  233. 'shopAdminId' => $shopAdminId,
  234. 'switchShop' => $switchShop,
  235. 'showDemo' => $showDemo,
  236. //有小程序新版本提示更新
  237. 'update' => $remind,
  238. ]);
  239. }
  240. //准备授权 ssh 2019.11.19
  241. public function actionPrepare()
  242. {
  243. $get = Yii::$app->request->get();
  244. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  245. if (empty($url)) {
  246. util::fail('没有网址');
  247. }
  248. $account = httpUtil::getAccount($url);
  249. if (empty($account)) {
  250. util::fail('没有商家帐号');
  251. }
  252. $merchant = MerchantService::getById($account);
  253. if (empty($merchant)) {
  254. util::fail('商家无效');
  255. }
  256. /**
  257. * authScope 参数的说明
  258. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  259. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  260. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  261. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  262. */
  263. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  264. $urlEncode = stringUtil::urlSafeB64Encode($url);
  265. //微信授权获取code ssh 2019.11.24
  266. $isOpen = 1;
  267. wxUtil::getCode($urlEncode, $merchant, $authScope, $isOpen);
  268. util::end();
  269. }
  270. //微信授权获取用户信息 ssh 2019.11.20
  271. public function actionGetUserInfo()
  272. {
  273. $get = Yii::$app->request->get();
  274. $code = isset($get['code']) ? $get['code'] : '';
  275. if (empty($code)) {
  276. util::fail('没有获取用户code');
  277. }
  278. if (isset($get['state']) && $get['state'] == 'authdeny') {
  279. util::fail('auth fail');
  280. }
  281. $urlEncode = $get['url'];
  282. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  283. $account = httpUtil::getAccount($url);
  284. if (empty($account)) {
  285. util::stop('商店ID无效!!');
  286. }
  287. $merchant = xhMerchantService::getByAccount($account);
  288. if (empty($merchant)) {
  289. util::stop('商店无效');
  290. }
  291. $sjId = $merchant['id'];
  292. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  293. if (empty($authScope)) {
  294. util::stop('没有授权类型');
  295. }
  296. $data = wxUtil::getOpenId($code, $merchant, 1);
  297. if ($data === false) {
  298. //微信授权获取code ssh 2019.11.24
  299. wxUtil::getCode($urlEncode, $merchant, $authScope, 1);
  300. util::end();
  301. }
  302. $openId = $data['openid'];
  303. $access_token = $data['access_token'];
  304. $user = UserService::getByOpenId($openId);
  305. //用户来源
  306. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  307. $source = $userSource['name'];
  308. if (empty($user)) {
  309. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  310. $originalInfo = [];
  311. $originalInfo['openId'] = $openId;
  312. $originalInfo['sjId'] = $sjId;
  313. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  314. if ($authScope == 'snsapi_userinfo') {
  315. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  316. $originalInfo = array_merge($baseInfo, $originalInfo);
  317. $originalInfo['isFull'] = 1;
  318. $originalInfo['unionId'] = $baseInfo['unionid'];
  319. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  320. $originalInfo['nickName'] = $baseInfo['nickName'];
  321. }
  322. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  323. } else {
  324. if ($user['isFull'] == 0) {
  325. if ($authScope == 'snsapi_userinfo') {
  326. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  327. $originalInfo = $baseInfo;
  328. $originalInfo['isFull'] = 1;
  329. $originalInfo['unionId'] = $baseInfo['unionid'];
  330. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  331. $originalInfo['openId'] = $baseInfo['openid'];
  332. $originalInfo['nickName'] = $baseInfo['nickName'];
  333. $originalInfo['sjId'] = $sjId;
  334. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  335. }
  336. }
  337. }
  338. //这个的基类没有设置统一的全局变量,这里进行设置一次
  339. $userId = $user['id'];
  340. $admin = AdminService::getByCondition(['userId' => $userId]);
  341. if (empty($admin)) {
  342. util::fail('您没有权限访问');
  343. }
  344. $adminId = $admin['id'];
  345. //获取token
  346. $token = jwt::getNewToken($adminId);
  347. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  348. $this->redirect($url);
  349. }
  350. //登陆并拿到token ssh 2019.11.23
  351. public function actionLogin()
  352. {
  353. $get = Yii::$app->request->get();
  354. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  355. if (stringUtil::isMobile($mobile) == false) {
  356. util::fail('请填写正常的手机号');
  357. }
  358. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  359. if (empty($password)) {
  360. util::fail('请输入密码');
  361. }
  362. $admin = AdminService::getByCondition(['mobile' => $mobile, 'style' => AdminClass::STYLE_HD]);
  363. if (password_verify($password, $admin['password']) == false) {
  364. util::fail('密码错误');
  365. }
  366. $adminId = $admin['id'];
  367. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'delStatus' => 0]);
  368. $sjId = isset($shopAdmin['sjId']) ? $shopAdmin['sjId'] : 0;
  369. $shopId = $shopAdmin['shopId'] ?? 0;
  370. if (empty($shopId)) {
  371. util::fail('您不是管理员');
  372. }
  373. //获取token
  374. $token = jwt::getNewToken($adminId);
  375. util::success(['token' => $token, 'account' => $sjId, 'shopId' => $shopId]);
  376. }
  377. //静默获取小程序用户信息
  378. public function actionMiniInfo()
  379. {
  380. //花店平台
  381. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  382. $code = Yii::$app->request->get('code', '');
  383. $wxMiniBase = WxOpenService::getWxMiniBase();
  384. $appId = $wxMiniBase['miniAppId'];
  385. $appSecret = $wxMiniBase['miniAppSecret'];
  386. if (empty($appSecret)) {
  387. Yii::info('没有找到小程序的密钥');
  388. //没有管理店铺不允许登录
  389. util::success(['token' => '']);
  390. }
  391. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  392. $curl = new curl\Curl();
  393. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  394. $result = $curl->get($url);
  395. $arr = Json::decode($result);
  396. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  397. $openid = $arr['openid'] ?? '';
  398. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $openid;
  399. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  400. if (empty($openid)) {
  401. Yii::info(json_encode($arr));
  402. //没有管理店铺不允许登录
  403. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  404. }
  405. $admin = AdminService::getByMiniOpenId($openid);
  406. if (empty($admin)) {
  407. //没有管理店铺不允许登录
  408. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  409. }
  410. $adminId = $admin['id'];
  411. $currentShopId = $admin['currentShopId'] ?? 0;
  412. if (empty($currentShopId)) {
  413. //没有管理店铺不允许登录
  414. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  415. }
  416. $shopAdmin = ShopAdminClass::getByCondition(['shopId' => $currentShopId, 'adminId' => $adminId], true);
  417. if (empty($shopAdmin)) {
  418. //没有管理店铺不允许登录
  419. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  420. }
  421. if (isset($shopAdmin->status) == false || $shopAdmin->status == 0) {
  422. //账号冻结不再自动登录
  423. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  424. }
  425. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  426. $shopAdmin->save();
  427. $shopAdminId = $shopAdmin->id ?? 0;
  428. //是否有切换门店的权限
  429. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  430. $prefix = imgUtil::getPrefix();
  431. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  432. $admin['avatar'] = $avatar;
  433. $token = jwt::getNewToken($adminId);
  434. $showDemo = 1;
  435. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  436. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  437. util::success([
  438. 'token' => $token,
  439. 'admin' => $admin,
  440. 'shopId' => $currentShopId,
  441. 'shopAdminId' => $shopAdminId,
  442. 'switchShop' => $switchShop,
  443. 'showDemo' => $showDemo,
  444. //有小程序新版本提示更新
  445. 'update' => $remind,
  446. ]);
  447. }
  448. //获取收款码的二维码图片 shish 20210602
  449. public function actionGatheringImgUrl()
  450. {
  451. $id = Yii::$app->request->get('id', 0);
  452. $shop = ShopClass::getById($id, true);
  453. if (empty($shop)) {
  454. util::fail('没有找到门店');
  455. }
  456. $sjId = $shop->sjId;
  457. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  458. $fileResource = file_get_contents($imgUrl);
  459. header('Content-type: image/jpeg');
  460. echo $fileResource;
  461. }
  462. //最新版本 shish 20220211
  463. public function actionGetApkVersion()
  464. {
  465. $version = getenv('HD_NEW_APK_VERSION') == false ? '1.0.0' : getenv('HD_NEW_APK_VERSION');
  466. $mustUpdate = getenv('HD_NEW_APK_VERSION_MUST_UPDATE') == false ? 0 : getenv('HD_NEW_APK_VERSION_MUST_UPDATE');
  467. $downloadVersion = getenv('HD_NEW_APK_DOWNLOAD_VERSION') == false ? '1_0_0' : getenv('HD_NEW_APK_DOWNLOAD_VERSION');
  468. util::success(['version' => $version, 'mustUpdate' => $mustUpdate,'downloadVersion'=>$downloadVersion]);
  469. }
  470. }