AuthController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use bizHd\admin\classes\ShopAdminClass;
  5. use bizGhs\shop\services\ShopAdminService;
  6. use bizGhs\admin\services\AdminService;
  7. use bizHd\user\classes\UserClass;
  8. use bizHd\wx\classes\WxOpenClass;
  9. use common\components\dict;
  10. use common\components\httpUtil;
  11. use common\components\imgUtil;
  12. use common\components\jwt;
  13. use common\components\util;
  14. use Yii;
  15. use common\components\stringUtil;
  16. use biz\sj\services\MerchantService;
  17. use bizHd\user\services\UserService;
  18. use common\services\xhMerchantService;
  19. use common\components\wxUtil;
  20. use linslin\yii2\curl;
  21. use yii\helpers\Json;
  22. /**
  23. * 用户登陆
  24. */
  25. class AuthController extends PublicController
  26. {
  27. //准备授权 ssh 2019.11.19
  28. public function actionPrepare()
  29. {
  30. $get = Yii::$app->request->get();
  31. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  32. if (empty($url)) {
  33. util::fail('没有网址');
  34. }
  35. $account = httpUtil::getAccount($url);
  36. if (empty($account)) {
  37. util::fail('没有商家帐号');
  38. }
  39. $merchant = MerchantService::getById($account);
  40. if (empty($merchant)) {
  41. util::fail('商家无效');
  42. }
  43. /**
  44. * authScope 参数的说明
  45. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  46. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  47. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  48. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  49. */
  50. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  51. $urlEncode = stringUtil::urlSafeB64Encode($url);
  52. //微信授权获取code ssh 2019.11.24
  53. $isOpen = 2;
  54. wxUtil::getCode($urlEncode, $merchant, $authScope, $isOpen);
  55. util::end();
  56. }
  57. //微信授权获取用户信息 ssh 2019.11.20
  58. public function actionGetUserInfo()
  59. {
  60. $get = Yii::$app->request->get();
  61. $code = isset($get['code']) ? $get['code'] : '';
  62. if (empty($code)) {
  63. util::fail('没有获取用户code');
  64. }
  65. if (isset($get['state']) && $get['state'] == 'authdeny') {
  66. util::fail('auth fail');
  67. }
  68. $urlEncode = $get['url'];
  69. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  70. $account = httpUtil::getAccount($url);
  71. if (empty($account)) {
  72. util::stop('商店ID无效!!');
  73. }
  74. $merchant = xhMerchantService::getByAccount($account);
  75. if (empty($merchant)) {
  76. util::stop('商店无效');
  77. }
  78. $sjId = $merchant['id'];
  79. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  80. if (empty($authScope)) {
  81. util::stop('没有授权类型');
  82. }
  83. $data = wxUtil::getOpenId($code, $merchant, 2);
  84. if ($data === false) {
  85. //微信授权获取code ssh 2019.11.24
  86. wxUtil::getCode($urlEncode, $merchant, $authScope, 2);
  87. util::end();
  88. }
  89. $openId = $data['openid'];
  90. $access_token = $data['access_token'];
  91. $user = UserService::getByOpenId($openId);
  92. //用户来源
  93. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  94. $source = $userSource['name'];
  95. if (empty($user)) {
  96. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  97. $originalInfo = [];
  98. $originalInfo['openId'] = $openId;
  99. $originalInfo['sjId'] = $sjId;
  100. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  101. if ($authScope == 'snsapi_userinfo') {
  102. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  103. $originalInfo = array_merge($baseInfo, $originalInfo);
  104. $originalInfo['isFull'] = 1;
  105. $originalInfo['unionId'] = $baseInfo['unionid'];
  106. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  107. $originalInfo['nickName'] = $baseInfo['nickName'];
  108. }
  109. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  110. } else {
  111. if ($user['isFull'] == 0) {
  112. if ($authScope == 'snsapi_userinfo') {
  113. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  114. $originalInfo = $baseInfo;
  115. $originalInfo['isFull'] = 1;
  116. $originalInfo['unionId'] = $baseInfo['unionid'];
  117. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  118. $originalInfo['openId'] = $baseInfo['openid'];
  119. $originalInfo['nickName'] = $baseInfo['nickName'];
  120. $originalInfo['sjId'] = $sjId;
  121. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  122. }
  123. }
  124. }
  125. //这个的基类没有设置统一的全局变量,这里进行设置一次
  126. $userId = $user['id'];
  127. $admin = AdminService::getByCondition(['userId' => $userId]);
  128. if (empty($admin)) {
  129. util::fail('您没有权限访问');
  130. }
  131. $adminId = $admin['id'];
  132. //获取token
  133. $token = jwt::getNewToken($adminId);
  134. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  135. $this->redirect($url);
  136. }
  137. //登陆并拿到token ssh 2019.11.23
  138. public function actionLogin()
  139. {
  140. $get = Yii::$app->request->get();
  141. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  142. if (stringUtil::isMobile($mobile) == false) {
  143. util::fail('请填写正常的手机号');
  144. }
  145. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  146. if (empty($password)) {
  147. util::fail('请输入密码');
  148. }
  149. $admin = AdminService::getByCondition(['mobile' => $mobile, 'style' => AdminClass::STYLE_GHS]);
  150. if (password_verify($password, $admin['password']) == false) {
  151. util::fail('密码错误');
  152. }
  153. $adminId = $admin['id'];
  154. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'delStatus' => 0]);
  155. if ($shopAdmin['status'] == 0) {
  156. util::fail("您的账号还未激活");
  157. }
  158. $sjId = isset($shopAdmin['sjId']) ? $shopAdmin['sjId'] : 0;
  159. $shopId = $shopAdmin['shopId'] ?? 0;
  160. if (empty($shopId)) {
  161. util::fail('您不是管理员');
  162. }
  163. //获取token
  164. $token = jwt::getNewToken($adminId);
  165. util::success(['token' => $token, 'account' => $sjId, 'shopId' => $shopId]);
  166. }
  167. //静默获取小程序用户信息
  168. public function actionMiniInfo()
  169. {
  170. //供应商平台
  171. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  172. $code = Yii::$app->request->get('code', '');
  173. if (empty($code)) {
  174. $password = Yii::$app->request->get('password', '');
  175. if ($password != '01231964') {
  176. util::fail('验证码错误');
  177. }
  178. $demoId = dict::getDict('ghsDemoAdminId');
  179. $admin = AdminClass::getById($demoId);
  180. if (empty($admin)) {
  181. util::fail('没有演示权限');
  182. }
  183. } else {
  184. $wxMiniBase = WxOpenClass::getGhsWxInfo();
  185. $appId = $wxMiniBase['miniAppId'];
  186. $appSecret = $wxMiniBase['miniAppSecret'];
  187. if (empty($appSecret)) {
  188. util::fail('没有找到小程序的密钥');
  189. }
  190. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  191. $curl = new curl\Curl();
  192. $result = $curl->get($url);
  193. $arr = Json::decode($result);
  194. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  195. $openid = $arr['openid'] ?? '';
  196. $unionId = $arr['unionid'] ?? '';
  197. if (empty($openid)) {
  198. Yii::info(json_encode($arr));
  199. util::fail('没有获取到小程序openId');
  200. }
  201. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $openid;
  202. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  203. //用户来源
  204. $userSource = UserClass::$userSourceId['mini']['name'];
  205. $admin = AdminService::getByMiniOpenId($openid);
  206. }
  207. if (empty($admin)) {
  208. $adminInfo = [
  209. 'miniOpenId' => $openid,
  210. 'unionId' => $unionId
  211. ];
  212. $admin = AdminService::replaceAdmin($adminInfo, $userSource);
  213. }
  214. $adminId = $admin['id'];
  215. //1没有开店 2已申请待审核 3已开店
  216. $openShop = $admin['openShop'] ?? 1;
  217. $currentShopId = $admin['currentShopId'] ?? 0;
  218. //员工id
  219. $shopAdminId = 0;
  220. $switchShop = 0;
  221. if (!empty($currentShopId)) {
  222. $shopAdmin = ShopAdminClass::getByCondition(['shopId' => $currentShopId, 'adminId' => $adminId], true);
  223. if (empty($shopAdmin)) {
  224. Yii::info('没有找到员工哦 adminId:' . $adminId);
  225. util::fail('没有找到员工哦');
  226. }
  227. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  228. $shopAdmin->save();
  229. $shopAdminId = $shopAdmin->id ?? 0;
  230. //是否有切换门店的权限
  231. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  232. }
  233. $admin['avatar'] = imgUtil::getPrefix() . '/retail/default-img.png';
  234. $token = jwt::getNewToken($adminId);
  235. $showDemo = dict::getDict('showDemo');
  236. util::success([
  237. 'token' => $token,
  238. 'admin' => $admin,
  239. 'shopAdminId' => $shopAdminId,
  240. 'shopId' => $currentShopId,
  241. 'switchShop' => $switchShop,
  242. 'openShop' => $openShop,
  243. 'showDemo' => $showDemo,
  244. ]);
  245. }
  246. }