AuthController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. if (empty($code)) {
  263. $experienceCode = Yii::$app->request->get('experienceCode', '');
  264. if (getenv('YII_ENV') == 'production') {
  265. $code = '1988';
  266. } else {
  267. $code = '1964';
  268. }
  269. if ($experienceCode != $code) {
  270. util::fail('验证码错误');
  271. }
  272. $demoId = getenv('GHS_DEMO_ADMIN_ID');
  273. $demoId = $demoId === false ? 4 : $demoId;
  274. $admin = AdminClass::getById($demoId);
  275. if (empty($admin)) {
  276. util::fail('没有演示权限');
  277. }
  278. } else {
  279. $wxMiniBase = WxOpenClass::getGhsWxInfo();
  280. $appId = $wxMiniBase['miniAppId'];
  281. $appSecret = $wxMiniBase['miniAppSecret'];
  282. if (empty($appSecret)) {
  283. util::fail('没有找到小程序的密钥');
  284. }
  285. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  286. $curl = new curl\Curl();
  287. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  288. $result = $curl->get($url);
  289. $arr = Json::decode($result);
  290. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  291. $openid = $arr['openid'] ?? '';
  292. $unionId = $arr['unionid'] ?? '';
  293. if (empty($openid)) {
  294. Yii::info(json_encode($arr));
  295. util::fail('没有获取到小程序openId');
  296. }
  297. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $openid;
  298. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  299. //用户来源
  300. $userSource = UserClass::$userSourceId['mini']['name'];
  301. $admin = AdminService::getByMiniOpenId($openid);
  302. }
  303. if (empty($admin)) {
  304. $adminInfo = [
  305. 'miniOpenId' => $openid,
  306. 'unionId' => $unionId
  307. ];
  308. $admin = AdminService::replaceAdmin($adminInfo, $userSource);
  309. }
  310. $adminId = $admin['id'];
  311. //1没有开店 2已申请待审核 3已开店
  312. $openShop = $admin['openShop'] ?? 1;
  313. $currentShopId = $admin['currentShopId'] ?? 0;
  314. //员工id
  315. $shopAdminId = 0;
  316. $switchShop = 0;
  317. if (!empty($currentShopId)) {
  318. $shopAdmin = ShopAdminClass::getByCondition(['shopId' => $currentShopId, 'adminId' => $adminId], true);
  319. if (empty($shopAdmin)) {
  320. util::fail('没有找到员工哦');
  321. }
  322. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  323. $shopAdmin->save();
  324. $shopAdminId = $shopAdmin->id ?? 0;
  325. //是否有切换门店的权限
  326. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  327. }
  328. $admin['avatar'] = imgUtil::getPrefix() . '/retail/default-img.png';
  329. $token = jwt::getNewToken($adminId);
  330. $showDemo = 1;
  331. $remind = getenv('GHS_UPDATE_REMIND') == false ? 0 : getenv('GHS_UPDATE_REMIND');
  332. $shop = ShopClass::getById($currentShopId, true);
  333. $skCustomId = $shop->skCustomId ?? 0;
  334. $apiHost = Yii::$app->params['ghsHost'];
  335. $imgUploadApi = $apiHost . '/upload/save-file';
  336. //使用手册
  337. $cacheKey = 'close_book_' . $shopAdminId;
  338. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  339. $showBook = !empty($hasClose) ? 0 : 1;
  340. $cacheKey = 'has_hit_navigate_' . $shopAdminId;
  341. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  342. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  343. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  344. util::success([
  345. 'token' => $token,
  346. 'admin' => $admin,
  347. 'shopAdminId' => $shopAdminId,
  348. 'shopId' => $currentShopId,
  349. 'switchShop' => $switchShop,
  350. 'openShop' => $openShop,
  351. 'showDemo' => $showDemo,
  352. //有小程序新版本提示更新
  353. 'update' => $remind,
  354. 'skCustomId' => $skCustomId,
  355. 'apiHost' => $apiHost,
  356. 'imgUploadApi' => $imgUploadApi,
  357. 'showBook' => $showBook,
  358. 'hasHitNavigate' => $hasHitNavigate,
  359. ]);
  360. }
  361. //app登陆 shish 20211219
  362. public function actionAppLogin()
  363. {
  364. $get = Yii::$app->request->get();
  365. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  366. if (stringUtil::isMobile($mobile) == false) {
  367. util::fail('请填写正确手机号');
  368. }
  369. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  370. if (empty($password)) {
  371. util::fail('请输入密码');
  372. }
  373. $admin = AdminService::getByCondition(['mobile' => $mobile, 'style' => AdminClass::STYLE_GHS]);
  374. if (password_verify($password, $admin['password']) == false) {
  375. util::fail('密码错误');
  376. }
  377. $adminId = $admin['id'];
  378. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'delStatus' => 0], true);
  379. if ($shopAdmin->status == 0) {
  380. util::fail("您的账号还未激活");
  381. }
  382. $shopId = $shopAdmin->shopId ?? 0;
  383. if (empty($shopId)) {
  384. util::fail('您不是管理员');
  385. }
  386. $token = jwt::getNewToken($adminId);
  387. $shopAdminId = $shopAdmin->id ?? 0;
  388. $currentShopId = $admin['currentShopId'] ?? 0;
  389. //是否有切换门店的权限
  390. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  391. //1没有开店 2已申请待审核 3已开店
  392. $openShop = $admin['openShop'] ?? 1;
  393. $showDemo = 1;
  394. $shop = ShopClass::getById($currentShopId, true);
  395. $skCustomId = $shop->skCustomId ?? 0;
  396. $apiHost = Yii::$app->params['ghsHost'];
  397. $imgUploadApi = $apiHost . '/upload/save-file';
  398. //使用手册
  399. $cacheKey = 'close_book_' . $shopAdminId;
  400. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  401. $showBook = !empty($hasClose) ? 0 : 1;
  402. $cacheKey = 'has_hit_navigate_' . $shopAdminId;
  403. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  404. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  405. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  406. util::success([
  407. 'token' => $token,
  408. 'admin' => $admin,
  409. 'shopAdminId' => $shopAdminId,
  410. 'shopId' => $currentShopId,
  411. 'switchShop' => $switchShop,
  412. 'openShop' => $openShop,
  413. 'showDemo' => $showDemo,
  414. //有小程序新版本提示更新
  415. 'update' => 0,
  416. 'skCustomId' => $skCustomId,
  417. 'apiHost' => $apiHost,
  418. 'imgUploadApi' => $imgUploadApi,
  419. 'showBook' => $showBook,
  420. 'hasHitNavigate' => $hasHitNavigate,
  421. ]);
  422. }
  423. }