AuthController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use bizHd\admin\classes\ShopAdminClass;
  5. use bizHd\admin\services\ShopAdminService;
  6. use bizHd\admin\services\AdminService;
  7. use bizHd\auth\services\AuthService;
  8. use bizHd\user\classes\UserClass;
  9. use bizHd\wx\services\WxOpenService;
  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. //准备授权 shish 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 shish 2019.11.24
  53. $isOpen = 1;
  54. wxUtil::getCode($urlEncode, $merchant, $authScope, $isOpen);
  55. util::end();
  56. }
  57. //微信授权获取用户信息 shish 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. $merchantId = $merchant['id'];
  79. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  80. if (empty($authScope)) {
  81. util::stop('没有授权类型');
  82. }
  83. $data = wxUtil::getOpenId($code, $merchant, 1);
  84. if ($data === false) {
  85. //微信授权获取code shish 2019.11.24
  86. wxUtil::getCode($urlEncode, $merchant, $authScope, 1);
  87. util::end();
  88. }
  89. $openId = $data['openid'];
  90. $access_token = $data['access_token'];
  91. $user = UserService::getByOpenId($openId, $merchantId);
  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['merchantId'] = $merchantId;
  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, $merchantId);
  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['merchantId'] = $merchantId;
  121. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  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 shish 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(['style' => AdminClass::STYLE_HD, 'mobile' => $mobile]);
  150. if (password_verify($password, $admin['password']) == false) {
  151. util::fail('密码错误');
  152. }
  153. $adminId = $admin['id'];
  154. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId]);
  155. $merchantId = isset($shopAdmin['merchantId']) ? $shopAdmin['merchantId'] : 0;
  156. $shopId = isset($shopAdmin['shopId']) ? $shopAdmin['shopId'] : 0;
  157. if (empty($shopId)) {
  158. util::fail('您不是管理员');
  159. }
  160. //获取token
  161. $token = jwt::getNewToken($adminId);
  162. util::success(['token' => $token, 'account' => $merchantId, 'shopId' => $shopId]);
  163. }
  164. //静默获取小程序用户信息
  165. public function actionMiniInfo()
  166. {
  167. //花店平台
  168. Yii::$app->params['ptStyle'] = 1;
  169. $code = Yii::$app->request->get('code', '');
  170. if (empty($code)) {
  171. util::fail('没有CODE信息');
  172. }
  173. $wxMiniBase = WxOpenService::getWxMiniBase();
  174. $appId = $wxMiniBase['miniAppId'];
  175. $appSecret = $wxMiniBase['miniAppSecret'];
  176. if (empty($appSecret)) {
  177. util::fail('没有找到小程序的密钥');
  178. }
  179. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  180. $curl = new curl\Curl();
  181. $result = $curl->get($url);
  182. $arr = Json::decode($result);
  183. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  184. $openid = isset($arr['openid']) ? $arr['openid'] : '';
  185. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $openid;
  186. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  187. if (empty($openid)) {
  188. Yii::info(json_encode($arr));
  189. util::fail('没有获取到小程序openId');
  190. }
  191. //用户来源
  192. $userSource = UserClass::$userSourceId['mini']['name'];
  193. $admin = AdminService::getByMiniOpenId($openid);
  194. if (empty($admin)) {
  195. $adminInfo = ['miniOpenId' => $openid];
  196. $admin = AdminService::replaceAdmin($adminInfo, $userSource);
  197. $adminId = $admin['id'];
  198. } else {
  199. $adminId = $admin['id'];
  200. }
  201. $shopId = $admin['currentShopId'] ?? 0;
  202. $shopAdminId = 0;
  203. $super = 0;
  204. if (!empty($shopId)) {
  205. $shopAdmin = ShopAdminClass::getByCondition(['shopId' => $shopId, 'adminId' => $adminId]);
  206. $shopAdminId = $shopAdmin['id'] ?? 0;
  207. $super = $shopAdmin['super'] ?? 0;
  208. }
  209. //头像
  210. $prefix = imgUtil::getPrefix();
  211. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  212. $admin['avatar'] = $avatar;
  213. $token = jwt::getNewToken($adminId);
  214. util::success(['token' => $token, 'admin' => $admin, 'shopId' => $shopId, 'shopAdminId' => $shopAdminId, 'super' => $super]);
  215. }
  216. }