AuthController.php 14 KB

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