AuthController.php 18 KB

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