AuthController.php 17 KB

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