AuthController.php 11 KB

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