AuthController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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'] = 2;
  172. $code = Yii::$app->request->get('code', '');
  173. if (empty($code)) {
  174. $demoId = dict::getDict('ghsDemoAdminId');
  175. $admin = AdminClass::getById($demoId);
  176. if (empty($admin)) {
  177. util::fail('没有演示权限');
  178. }
  179. } else {
  180. $wxMiniBase = WxOpenClass::getGhsWxInfo();
  181. $appId = $wxMiniBase['miniAppId'];
  182. $appSecret = $wxMiniBase['miniAppSecret'];
  183. if (empty($appSecret)) {
  184. util::fail('没有找到小程序的密钥');
  185. }
  186. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  187. $curl = new curl\Curl();
  188. $result = $curl->get($url);
  189. $arr = Json::decode($result);
  190. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  191. $openid = $arr['openid'] ?? '';
  192. $unionId = $arr['unionid'] ?? '';
  193. if (empty($openid)) {
  194. Yii::info(json_encode($arr));
  195. util::fail('没有获取到小程序openId');
  196. }
  197. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $openid;
  198. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  199. //用户来源
  200. $userSource = UserClass::$userSourceId['mini']['name'];
  201. $admin = AdminService::getByMiniOpenId($openid);
  202. }
  203. if (empty($admin)) {
  204. $adminInfo = ['miniOpenId' => $openid, 'style' => 2, 'unionId' => $unionId];
  205. $admin = AdminService::replaceAdmin($adminInfo, $userSource);
  206. }
  207. $adminId = $admin['id'];
  208. //1没有开店 2已申请待审核 3已开店
  209. $openShop = $admin['openShop'] ?? 1;
  210. $currentShopId = $admin['currentShopId'] ?? 0;
  211. //员工id
  212. $shopAdminId = 0;
  213. $switchShop = 0;
  214. if (!empty($currentShopId)) {
  215. $shopAdmin = ShopAdminClass::getByCondition(['shopId' => $currentShopId, 'adminId' => $adminId], true);
  216. if (empty($shopAdmin)) {
  217. Yii::info('没有找到员工哦 adminId:' . $adminId);
  218. util::fail('没有找到员工哦');
  219. }
  220. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  221. $shopAdmin->save();
  222. $shopAdminId = $shopAdmin->id ?? 0;
  223. //是否有切换门店的权限
  224. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  225. }
  226. $admin['avatar'] = imgUtil::getPrefix() . '/retail/default-img.png';
  227. $token = jwt::getNewToken($adminId);
  228. $showDemo = dict::getDict('showDemo');
  229. util::success([
  230. 'token' => $token,
  231. 'admin' => $admin,
  232. 'shopAdminId' => $shopAdminId,
  233. 'shopId' => $currentShopId,
  234. 'switchShop' => $switchShop,
  235. 'openShop' => $openShop,
  236. 'showDemo' => $showDemo,
  237. ]);
  238. }
  239. }