AuthController.php 18 KB

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