AuthController.php 9.3 KB

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