AuthController.php 16 KB

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