AuthController.php 7.0 KB

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