AuthController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\shop\classes\ShopClass;
  5. use bizHd\admin\classes\ShopAdminClass;
  6. use bizHd\admin\services\ShopAdminService;
  7. use bizHd\admin\services\AdminService;
  8. use bizHd\user\classes\UserClass;
  9. use bizHd\wx\services\WxOpenService;
  10. use common\components\dict;
  11. use common\components\httpUtil;
  12. use common\components\imgUtil;
  13. use common\components\jwt;
  14. use common\components\util;
  15. use Yii;
  16. use common\components\stringUtil;
  17. use biz\sj\services\MerchantService;
  18. use bizHd\user\services\UserService;
  19. use common\services\xhMerchantService;
  20. use common\components\wxUtil;
  21. use linslin\yii2\curl;
  22. use yii\helpers\Json;
  23. /**
  24. * 用户登陆
  25. */
  26. class AuthController extends PublicController
  27. {
  28. //本机号码一键登录 shish 20210117
  29. public function actionPhoneLogin()
  30. {
  31. $get = Yii::$app->request->get();
  32. $access_token = $get['access_token'] ?? '';
  33. $openid = $get['openid'] ?? '';
  34. if (empty($access_token) || empty($openid)) {
  35. util::fail('参数错误');
  36. }
  37. //密钥,需要和uniCloud里的一致
  38. $secret = 'XHB2021198819640123';
  39. $params = ['access_token' => $access_token, 'openid' => $openid];
  40. $stringSignTemp = '';
  41. foreach ($params as $k => $v) {
  42. $stringSignTemp .= $k . '=' . $v . '&';
  43. }
  44. $stringSignTemp = rtrim($stringSignTemp, '&');
  45. $sign = hash_hmac('sha256', $stringSignTemp, $secret);
  46. $url = Yii::$app->params['hdUniCloudHost'] . "/getMobileNumber?access_token=" . $access_token . "&sign=" . $sign . "&" . $stringSignTemp;
  47. $curl = new curl\Curl();
  48. $respond = $curl->get($url);
  49. Yii::info($respond);
  50. $result = json_decode($respond, true);
  51. if (isset($result['code']) == false || $result['code'] != 1) {
  52. util::fail('获取手机号失败');
  53. }
  54. $phoneNumber = $result['data']['phoneNumber'] ?? '';
  55. if (empty($phoneNumber)) {
  56. util::fail('没有获取到手机号');
  57. }
  58. //参考供应商的代码
  59. }
  60. //准备授权 ssh 2019.11.19
  61. public function actionPrepare()
  62. {
  63. $get = Yii::$app->request->get();
  64. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  65. if (empty($url)) {
  66. util::fail('没有网址');
  67. }
  68. $account = httpUtil::getAccount($url);
  69. if (empty($account)) {
  70. util::fail('没有商家帐号');
  71. }
  72. $merchant = MerchantService::getById($account);
  73. if (empty($merchant)) {
  74. util::fail('商家无效');
  75. }
  76. /**
  77. * authScope 参数的说明
  78. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  79. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  80. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  81. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  82. */
  83. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  84. $urlEncode = stringUtil::urlSafeB64Encode($url);
  85. //微信授权获取code ssh 2019.11.24
  86. $isOpen = 1;
  87. wxUtil::getCode($urlEncode, $merchant, $authScope, $isOpen);
  88. util::end();
  89. }
  90. //微信授权获取用户信息 ssh 2019.11.20
  91. public function actionGetUserInfo()
  92. {
  93. $get = Yii::$app->request->get();
  94. $code = isset($get['code']) ? $get['code'] : '';
  95. if (empty($code)) {
  96. util::fail('没有获取用户code');
  97. }
  98. if (isset($get['state']) && $get['state'] == 'authdeny') {
  99. util::fail('auth fail');
  100. }
  101. $urlEncode = $get['url'];
  102. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  103. $account = httpUtil::getAccount($url);
  104. if (empty($account)) {
  105. util::stop('商店ID无效!!');
  106. }
  107. $merchant = xhMerchantService::getByAccount($account);
  108. if (empty($merchant)) {
  109. util::stop('商店无效');
  110. }
  111. $sjId = $merchant['id'];
  112. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  113. if (empty($authScope)) {
  114. util::stop('没有授权类型');
  115. }
  116. $data = wxUtil::getOpenId($code, $merchant, 1);
  117. if ($data === false) {
  118. //微信授权获取code ssh 2019.11.24
  119. wxUtil::getCode($urlEncode, $merchant, $authScope, 1);
  120. util::end();
  121. }
  122. $openId = $data['openid'];
  123. $access_token = $data['access_token'];
  124. $user = UserService::getByOpenId($openId);
  125. //用户来源
  126. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  127. $source = $userSource['name'];
  128. if (empty($user)) {
  129. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  130. $originalInfo = [];
  131. $originalInfo['openId'] = $openId;
  132. $originalInfo['sjId'] = $sjId;
  133. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  134. if ($authScope == 'snsapi_userinfo') {
  135. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  136. $originalInfo = array_merge($baseInfo, $originalInfo);
  137. $originalInfo['isFull'] = 1;
  138. $originalInfo['unionId'] = $baseInfo['unionid'];
  139. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  140. $originalInfo['nickName'] = $baseInfo['nickName'];
  141. }
  142. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  143. } else {
  144. if ($user['isFull'] == 0) {
  145. if ($authScope == 'snsapi_userinfo') {
  146. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  147. $originalInfo = $baseInfo;
  148. $originalInfo['isFull'] = 1;
  149. $originalInfo['unionId'] = $baseInfo['unionid'];
  150. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  151. $originalInfo['openId'] = $baseInfo['openid'];
  152. $originalInfo['nickName'] = $baseInfo['nickName'];
  153. $originalInfo['sjId'] = $sjId;
  154. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  155. }
  156. }
  157. }
  158. //这个的基类没有设置统一的全局变量,这里进行设置一次
  159. $userId = $user['id'];
  160. $admin = AdminService::getByCondition(['userId' => $userId]);
  161. if (empty($admin)) {
  162. util::fail('您没有权限访问');
  163. }
  164. $adminId = $admin['id'];
  165. //获取token
  166. $token = jwt::getNewToken($adminId);
  167. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  168. $this->redirect($url);
  169. }
  170. //登陆并拿到token ssh 2019.11.23
  171. public function actionLogin()
  172. {
  173. $get = Yii::$app->request->get();
  174. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  175. if (stringUtil::isMobile($mobile) == false) {
  176. util::fail('请填写正常的手机号');
  177. }
  178. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  179. if (empty($password)) {
  180. util::fail('请输入密码');
  181. }
  182. $admin = AdminService::getByCondition(['mobile' => $mobile, 'style' => AdminClass::STYLE_HD]);
  183. if (password_verify($password, $admin['password']) == false) {
  184. util::fail('密码错误');
  185. }
  186. $adminId = $admin['id'];
  187. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'delStatus' => 0]);
  188. $sjId = isset($shopAdmin['sjId']) ? $shopAdmin['sjId'] : 0;
  189. $shopId = $shopAdmin['shopId'] ?? 0;
  190. if (empty($shopId)) {
  191. util::fail('您不是管理员');
  192. }
  193. //获取token
  194. $token = jwt::getNewToken($adminId);
  195. util::success(['token' => $token, 'account' => $sjId, 'shopId' => $shopId]);
  196. }
  197. //静默获取小程序用户信息
  198. public function actionMiniInfo()
  199. {
  200. //花店平台
  201. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  202. $code = Yii::$app->request->get('code', '');
  203. $wxMiniBase = WxOpenService::getWxMiniBase();
  204. $appId = $wxMiniBase['miniAppId'];
  205. $appSecret = $wxMiniBase['miniAppSecret'];
  206. if (empty($appSecret)) {
  207. Yii::info('没有找到小程序的密钥');
  208. //没有管理店铺不允许登录
  209. util::success(['token' => '']);
  210. }
  211. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  212. $curl = new curl\Curl();
  213. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  214. $result = $curl->get($url);
  215. $arr = Json::decode($result);
  216. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  217. $openid = $arr['openid'] ?? '';
  218. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $openid;
  219. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  220. if (empty($openid)) {
  221. Yii::info(json_encode($arr));
  222. //没有管理店铺不允许登录
  223. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  224. }
  225. $admin = AdminService::getByMiniOpenId($openid);
  226. if (empty($admin)) {
  227. //没有管理店铺不允许登录
  228. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  229. }
  230. $adminId = $admin['id'];
  231. $currentShopId = $admin['currentShopId'] ?? 0;
  232. if (empty($currentShopId)) {
  233. //没有管理店铺不允许登录
  234. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  235. }
  236. $shopAdmin = ShopAdminClass::getByCondition(['shopId' => $currentShopId, 'adminId' => $adminId], true);
  237. if (empty($shopAdmin)) {
  238. //没有管理店铺不允许登录
  239. util::success(['token' => '', 'currentMiniOpenId' => $openid]);
  240. }
  241. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  242. $shopAdmin->save();
  243. $shopAdminId = $shopAdmin->id ?? 0;
  244. //是否有切换门店的权限
  245. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin->attributes);
  246. $prefix = imgUtil::getPrefix();
  247. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  248. $admin['avatar'] = $avatar;
  249. $token = jwt::getNewToken($adminId);
  250. $showDemo = 1;
  251. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  252. util::success([
  253. 'token' => $token,
  254. 'admin' => $admin,
  255. 'shopId' => $currentShopId,
  256. 'shopAdminId' => $shopAdminId,
  257. 'switchShop' => $switchShop,
  258. 'showDemo' => $showDemo,
  259. //有小程序新版本提示更新
  260. 'update' => $remind,
  261. ]);
  262. }
  263. //获取收款码的二维码图片 shish 20210602
  264. public function actionGatheringImgUrl()
  265. {
  266. $id = Yii::$app->request->get('id', 0);
  267. $shop = ShopClass::getById($id, true);
  268. if (empty($shop)) {
  269. util::fail('没有找到门店');
  270. }
  271. $sjId = $shop->sjId;
  272. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  273. $fileResource = file_get_contents($imgUrl);
  274. header('Content-type: image/jpeg');
  275. echo $fileResource;
  276. }
  277. }