AuthController.php 7.9 KB

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