AuthController.php 9.5 KB

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