AuthController.php 18 KB

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