AuthController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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\noticeUtil;
  15. use common\components\util;
  16. use Yii;
  17. use common\components\stringUtil;
  18. use biz\sj\services\MerchantService;
  19. use bizHd\user\services\UserService;
  20. use common\services\xhMerchantService;
  21. use common\components\wxUtil;
  22. use linslin\yii2\curl;
  23. use yii\helpers\Json;
  24. /**
  25. * 用户登陆
  26. */
  27. class AuthController extends PublicController
  28. {
  29. //获取手机号 ssh 20250217
  30. public function actionGetMobile()
  31. {
  32. $post = Yii::$app->request->post();
  33. $iv = $post['iv'];
  34. $encryptedData = $post['encryptedData'];
  35. $merchant = WxOpenService::getWxMiniBase();
  36. $appId = $merchant['miniAppId'];
  37. $miniOpenId = $post['miniOpenId'] ?? '';
  38. if (empty($miniOpenId)) {
  39. util::success(['hasNoOpenId' => 1], '登录失败,没有miniOpenId');
  40. }
  41. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  42. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  43. if (empty($sessionKey)) {
  44. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  45. //noticeUtil::push($cacheKey . " 登录失败!!sessionKey空的", '15280215347');
  46. util::success(['hasNoOpenId' => 1], '登录失败!');
  47. }
  48. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  49. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  50. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  51. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  52. if ($errCode != 0) {
  53. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  54. //noticeUtil::push($cacheKey . " 登录失败!!!!sessionKey异常", '15280215347');
  55. util::success(['hasNoOpenId' => 1], '登录失败...');
  56. }
  57. $arr = Json::decode($result);
  58. $phoneNumber = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  59. if (empty($phoneNumber)) {
  60. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  61. //noticeUtil::push($cacheKey . " 登录失败了!获取手机号失败", '15280215347');
  62. util::success(['hasNoOpenId' => 1], '登录失败了哦');
  63. }
  64. $cacheKey = 'getMobile_'.$phoneNumber;
  65. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 1]);
  66. util::success(['mobile' => $phoneNumber]);
  67. }
  68. //微信手机号一键登录 ssh 20210121
  69. public function actionWxMobileLogin()
  70. {
  71. $post = Yii::$app->request->post();
  72. $iv = $post['iv'];
  73. $encryptedData = $post['encryptedData'];
  74. $merchant = WxOpenService::getWxMiniBase();
  75. $appId = $merchant['miniAppId'];
  76. $miniOpenId = $post['miniOpenId'] ?? '';
  77. if (empty($miniOpenId)) {
  78. util::success(['hasNoOpenId' => 1], '登录失败,没有miniOpenId');
  79. }
  80. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  81. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  82. if (empty($sessionKey)) {
  83. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  84. //noticeUtil::push($cacheKey . " 登录失败!!sessionKey空的", '15280215347');
  85. util::success(['hasNoOpenId' => 1], '登录失败!');
  86. }
  87. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  88. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  89. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  90. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  91. if ($errCode != 0) {
  92. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  93. //noticeUtil::push($cacheKey . " 登录失败!!!!sessionKey异常", '15280215347');
  94. util::success(['hasNoOpenId' => 1], '登录失败...');
  95. }
  96. $arr = Json::decode($result);
  97. $phoneNumber = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  98. if (empty($phoneNumber)) {
  99. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  100. //noticeUtil::push($cacheKey . " 登录失败了!获取手机号失败", '15280215347');
  101. util::success(['hasNoOpenId' => 1], '登录失败了哦');
  102. }
  103. $admin = AdminService::getByCondition(['mobile' => $phoneNumber]);
  104. if (empty($admin)) {
  105. util::fail('请先注册');
  106. }
  107. $adminId = $admin['id'];
  108. AdminClass::updateById($adminId, ['miniOpenId' => $miniOpenId]);
  109. $admin['miniOpenId'] = $miniOpenId;
  110. $openShop = $admin['openShop'] ?? 1;
  111. $currentShopId = $admin['currentShopId'] ?? 0;
  112. if (empty($currentShopId)) {
  113. if ($openShop == 2) {
  114. util::fail('审核中');
  115. }
  116. util::fail('请先注册');
  117. }
  118. $currentShop = ShopClass::getById($currentShopId, true);
  119. if (empty($currentShop)) {
  120. util::fail('没有找到门店36');
  121. }
  122. $pfShopId = $currentShop->pfShopId ?? 0;
  123. $onlyCg = $currentShop->onlyCg ?? 1;
  124. $mainId = $currentShop->mainId ?? 0;
  125. $shareLogo = imgUtil::groupImg('buy_logo.jpg');
  126. if (isset($currentShop->shareLogo) && !empty($currentShop->shareLogo)) {
  127. $shareLogo = imgUtil::groupImg($currentShop->shareLogo);
  128. }
  129. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'mainId' => $mainId], true);
  130. if (empty($shopAdmin)) {
  131. util::fail('无法操作');
  132. }
  133. if ($shopAdmin->status == 0) {
  134. util::fail("您的账号已被冻结");
  135. }
  136. $token = jwt::getNewToken($adminId);
  137. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  138. $shopAdmin->save();
  139. $shopAdminId = $shopAdmin->id ?? 0;
  140. //是否有切换门店的权限
  141. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  142. $prefix = imgUtil::getPrefix();
  143. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  144. $admin['avatar'] = $avatar;
  145. $showDemo = 1;
  146. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  147. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  148. if ($hdUpgrading == 1) {
  149. util::fail('系统升级中,稍后再试');
  150. }
  151. $apiHost = Yii::$app->params['hdHost'];
  152. $imgUploadApi = $apiHost . '/upload/save-file';
  153. //惠雅鲜花员工不能看收入情况
  154. if (in_array($adminId, [2366, 2812, 4004])) {
  155. $shopAdmin->super = 0;
  156. }
  157. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  158. util::success([
  159. 'token' => $token,
  160. 'admin' => $admin,
  161. 'shopId' => $currentShopId,
  162. 'shopAdminId' => $shopAdminId,
  163. 'switchShop' => $switchShop,
  164. 'pfShopId' => $pfShopId,
  165. 'onlyCg' => $onlyCg,
  166. 'shareLogo' => $shareLogo,
  167. 'showDemo' => $showDemo,
  168. 'imgUploadApi' => $imgUploadApi,
  169. //有小程序新版本提示更新
  170. 'update' => $remind,
  171. 'staff' => $shopAdmin,
  172. ]);
  173. }
  174. //app登陆 ssh 20211219
  175. public function actionAppLogin()
  176. {
  177. $getParams = Yii::$app->request->get();
  178. $postParams = Yii::$app->request->post();
  179. $allParams = array_merge($getParams, $postParams);
  180. $mobile = $allParams['mobile'] ?? 0;
  181. if (!stringUtil::isMobile($mobile)) {
  182. util::fail('请填写正确手机号');
  183. }
  184. $password = !empty($allParams['password']) ? $allParams['password'] : '';
  185. if (empty($password)) {
  186. util::fail('请输入密码');
  187. }
  188. $admin = AdminService::getByCondition(['mobile' => $mobile]);
  189. if (empty($admin)) {
  190. util::fail('请先注册...');
  191. }
  192. if (!password_verify($password, $admin['password'])) {
  193. util::fail('密码错误');
  194. }
  195. $openShop = $admin['openShop'] ?? 1;
  196. $currentShopId = $admin['currentShopId'] ?? 0;
  197. if (empty($currentShopId)) {
  198. if ($openShop == 2) {
  199. util::fail('审核中');
  200. }
  201. util::fail('请先注册');
  202. }
  203. $currentShop = \bizHd\shop\classes\ShopClass::getById($currentShopId, true);
  204. if (empty($currentShop)) {
  205. util::fail('没有找到门店37');
  206. }
  207. $mainId = $currentShop->mainId ?? 0;
  208. $pfShopId = $currentShop->pfShopId ?? 0;
  209. $onlyCg = $currentShop->onlyCg ?? 1;
  210. $shareLogo = imgUtil::groupImg('buy_logo.jpg');
  211. if (isset($currentShop->shareLogo) && !empty($currentShop->shareLogo)) {
  212. $shareLogo = imgUtil::groupImg($currentShop->shareLogo);
  213. }
  214. $adminId = $admin['id'];
  215. $shopAdmin = ShopAdminService::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  216. if (empty($shopAdmin)) {
  217. util::fail('无法操作');
  218. }
  219. if ($shopAdmin->status == 0) {
  220. util::fail("您的账号已被冻结");
  221. }
  222. if ($shopAdmin->delStatus == 1) {
  223. util::fail("您的账号已删除");
  224. }
  225. $token = jwt::getNewToken($adminId);
  226. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  227. $shopAdmin->save();
  228. $shopAdminId = $shopAdmin->id ?? 0;
  229. //是否有切换门店的权限
  230. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  231. $prefix = imgUtil::getPrefix();
  232. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  233. $admin['avatar'] = $avatar;
  234. $showDemo = 1;
  235. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  236. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  237. if ($hdUpgrading == 1) {
  238. util::fail('系统升级中,稍后再试');
  239. }
  240. $apiHost = Yii::$app->params['hdHost'];
  241. $imgUploadApi = $apiHost . '/upload/save-file';
  242. //惠雅鲜花员工不能看收入情况
  243. if (in_array($adminId, [2366, 2812, 4004])) {
  244. $shopAdmin->super = 0;
  245. }
  246. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  247. util::success([
  248. 'token' => $token,
  249. 'admin' => $admin,
  250. 'shopId' => $currentShopId,
  251. 'shopAdminId' => $shopAdminId,
  252. 'switchShop' => $switchShop,
  253. 'pfShopId' => $pfShopId,
  254. 'onlyCg' => $onlyCg,
  255. 'shareLogo' => $shareLogo,
  256. 'imgUploadApi' => $imgUploadApi,
  257. 'showDemo' => $showDemo,
  258. //有小程序新版本提示更新
  259. 'update' => $remind,
  260. 'staff' => $shopAdmin,
  261. ]);
  262. }
  263. //本机号码一键登录 ssh 20210117
  264. public function actionPhoneLogin()
  265. {
  266. $get = Yii::$app->request->get();
  267. $access_token = $get['access_token'] ?? '';
  268. $openid = $get['openid'] ?? '';
  269. if (empty($access_token) || empty($openid)) {
  270. util::fail('参数错误');
  271. }
  272. //密钥,需要和uniCloud里的一致
  273. $secret = 'XHB2021198819640123';
  274. $params = ['access_token' => $access_token, 'openid' => $openid];
  275. $stringSignTemp = '';
  276. foreach ($params as $k => $v) {
  277. $stringSignTemp .= $k . '=' . $v . '&';
  278. }
  279. $stringSignTemp = rtrim($stringSignTemp, '&');
  280. $sign = hash_hmac('sha256', $stringSignTemp, $secret);
  281. $url = Yii::$app->params['hdUniCloudHost'] . "/getMobileNumber?access_token=" . $access_token . "&sign=" . $sign . "&" . $stringSignTemp;
  282. $curl = new curl\Curl();
  283. $respond = $curl->get($url);
  284. Yii::info($respond);
  285. $result = json_decode($respond, true);
  286. if (isset($result['code']) == false || $result['code'] != 1) {
  287. util::fail('获取手机号失败');
  288. }
  289. $phoneNumber = $result['data']['phoneNumber'] ?? '';
  290. if (empty($phoneNumber)) {
  291. util::fail('没有获取到手机号');
  292. }
  293. $admin = AdminService::getByCondition(['mobile' => $phoneNumber]);
  294. if (empty($admin)) {
  295. util::fail('请先注册');
  296. }
  297. $openShop = $admin['openShop'] ?? 1;
  298. $currentShopId = $admin['currentShopId'] ?? 0;
  299. if (empty($currentShopId)) {
  300. if ($openShop == 2) {
  301. util::fail('审核中');
  302. }
  303. util::fail('请先注册');
  304. }
  305. $currentShop = ShopClass::getById($currentShopId, true);
  306. if (empty($currentShop)) {
  307. util::fail('登录失败,没有找到门店38');
  308. }
  309. $pfShopId = $currentShop->pfShopId ?? 0;
  310. $onlyCg = $currentShop->onlyCg ?? 1;
  311. $shareLogo = imgUtil::groupImg('buy_logo.jpg');
  312. if (isset($currentShop->shareLogo) && !empty($currentShop->shareLogo)) {
  313. $shareLogo = imgUtil::groupImg($currentShop->shareLogo);
  314. }
  315. $adminId = $admin['id'];
  316. $mainId = $currentShop->mainId ?? 0;
  317. $shopAdmin = ShopAdminService::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  318. if (empty($shopAdmin)) {
  319. util::fail('无法操作');
  320. }
  321. if ($shopAdmin->status == 0) {
  322. util::fail("您的账号已被冻结");
  323. }
  324. if ($shopAdmin->delStatus == 1) {
  325. util::fail("您的账号已删除");
  326. }
  327. $token = jwt::getNewToken($adminId);
  328. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  329. $shopAdmin->save();
  330. $shopAdminId = $shopAdmin->id ?? 0;
  331. //是否有切换门店的权限
  332. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  333. $prefix = imgUtil::getPrefix();
  334. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  335. $admin['avatar'] = $avatar;
  336. $showDemo = 1;
  337. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  338. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  339. if ($hdUpgrading == 1) {
  340. util::fail('系统升级中,稍后再试');
  341. }
  342. $apiHost = Yii::$app->params['hdHost'];
  343. $imgUploadApi = $apiHost . '/upload/save-file';
  344. //惠雅鲜花员工不能看收入情况
  345. if (in_array($adminId, [2366, 2812, 4004])) {
  346. $shopAdmin->super = 0;
  347. }
  348. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  349. util::success([
  350. 'token' => $token,
  351. 'admin' => $admin,
  352. 'shopId' => $currentShopId,
  353. 'shopAdminId' => $shopAdminId,
  354. 'pfShopId' => $pfShopId,
  355. 'onlyCg' => $onlyCg,
  356. 'shareLogo' => $shareLogo,
  357. 'switchShop' => $switchShop,
  358. 'showDemo' => $showDemo,
  359. 'imgUploadApi' => $imgUploadApi,
  360. //有小程序新版本提示更新
  361. 'update' => $remind,
  362. 'staff' => $shopAdmin,
  363. ]);
  364. }
  365. //准备授权 ssh 2019.11.19
  366. public function actionPrepare()
  367. {
  368. $get = Yii::$app->request->get();
  369. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  370. if (empty($url)) {
  371. util::fail('没有网址');
  372. }
  373. $account = httpUtil::getAccount($url);
  374. if (empty($account)) {
  375. util::fail('没有商家帐号');
  376. }
  377. $merchant = MerchantService::getById($account);
  378. if (empty($merchant)) {
  379. util::fail('商家无效');
  380. }
  381. /**
  382. * authScope 参数的说明
  383. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  384. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  385. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  386. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  387. */
  388. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  389. $urlEncode = stringUtil::urlSafeB64Encode($url);
  390. //微信授权获取code ssh 2019.11.24
  391. $isOpen = 1;
  392. wxUtil::getCode($urlEncode, $merchant, $authScope, $isOpen);
  393. util::end();
  394. }
  395. //微信授权获取用户信息 ssh 2019.11.20
  396. public function actionGetUserInfo()
  397. {
  398. $get = Yii::$app->request->get();
  399. $code = isset($get['code']) ? $get['code'] : '';
  400. if (empty($code)) {
  401. util::fail('没有获取用户code');
  402. }
  403. if (isset($get['state']) && $get['state'] == 'authdeny') {
  404. util::fail('auth fail');
  405. }
  406. $urlEncode = $get['url'];
  407. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  408. $account = httpUtil::getAccount($url);
  409. if (empty($account)) {
  410. util::stop('商店ID无效!!');
  411. }
  412. $merchant = xhMerchantService::getByAccount($account);
  413. if (empty($merchant)) {
  414. util::stop('商店无效');
  415. }
  416. $sjId = $merchant['id'];
  417. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  418. if (empty($authScope)) {
  419. util::stop('没有授权类型');
  420. }
  421. $data = wxUtil::getOpenId($code, $merchant, 1);
  422. if ($data === false) {
  423. //微信授权获取code ssh 2019.11.24
  424. wxUtil::getCode($urlEncode, $merchant, $authScope, 1);
  425. util::end();
  426. }
  427. $openId = $data['openid'];
  428. $access_token = $data['access_token'];
  429. $user = UserService::getByOpenId($openId);
  430. //用户来源
  431. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  432. $source = $userSource['name'];
  433. if (empty($user)) {
  434. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  435. $originalInfo = [];
  436. $originalInfo['openId'] = $openId;
  437. $originalInfo['sjId'] = $sjId;
  438. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  439. if ($authScope == 'snsapi_userinfo') {
  440. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  441. $originalInfo = array_merge($baseInfo, $originalInfo);
  442. $originalInfo['isFull'] = 1;
  443. $originalInfo['unionId'] = $baseInfo['unionid'];
  444. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  445. $originalInfo['nickName'] = $baseInfo['nickName'];
  446. }
  447. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  448. } else {
  449. if ($user['isFull'] == 0) {
  450. if ($authScope == 'snsapi_userinfo') {
  451. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  452. $originalInfo = $baseInfo;
  453. $originalInfo['isFull'] = 1;
  454. $originalInfo['unionId'] = $baseInfo['unionid'];
  455. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  456. $originalInfo['openId'] = $baseInfo['openid'];
  457. $originalInfo['nickName'] = $baseInfo['nickName'];
  458. $originalInfo['sjId'] = $sjId;
  459. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  460. }
  461. }
  462. }
  463. //这个的基类没有设置统一的全局变量,这里进行设置一次
  464. $userId = $user['id'];
  465. $admin = AdminService::getByCondition(['userId' => $userId]);
  466. if (empty($admin)) {
  467. util::fail('您无法访问');
  468. }
  469. $adminId = $admin['id'];
  470. //获取token
  471. $token = jwt::getNewToken($adminId);
  472. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  473. $this->redirect($url);
  474. }
  475. //登陆并拿到token ssh 2019.11.23
  476. public function actionLogin()
  477. {
  478. $get = Yii::$app->request->get();
  479. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  480. if (stringUtil::isMobile($mobile) == false) {
  481. util::fail('请填写正常的手机号');
  482. }
  483. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  484. if (empty($password)) {
  485. util::fail('请输入密码');
  486. }
  487. $admin = AdminService::getByCondition(['mobile' => $mobile]);
  488. if (empty($admin)) {
  489. util::fail('请先注册哦。');
  490. }
  491. if (password_verify($password, $admin['password']) == false) {
  492. util::fail('密码错误');
  493. }
  494. $adminId = $admin['id'];
  495. $openShop = $admin['openShop'] ?? 1;
  496. $currentShopId = $admin['currentShopId'] ?? 0;
  497. if (empty($currentShopId)) {
  498. if ($openShop == 2) {
  499. util::fail('审核中');
  500. }
  501. util::fail('请先注册');
  502. }
  503. $currentShop = ShopClass::getById($currentShopId, true);
  504. if (empty($currentShop)) {
  505. util::fail('没有找到门店39');
  506. }
  507. $pfShopId = $currentShop->pfShopId ?? 0;
  508. if (!empty($pfShopId)) {
  509. //util::fail('批发店请不要在零售端登录');
  510. }
  511. $sjId = $currentShop->sjId ?? 0;
  512. $mainId = $currentShop->mainId ?? 0;
  513. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'mainId' => $mainId], true);
  514. if (empty($shopAdmin)) {
  515. util::fail('无法操作');
  516. }
  517. if ($shopAdmin->status == 0) {
  518. util::fail("您的账号已被冻结");
  519. }
  520. $token = jwt::getNewToken($adminId);
  521. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  522. $shopAdmin->save();
  523. //是否有切换门店的权限
  524. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  525. $prefix = imgUtil::getPrefix();
  526. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  527. $admin['avatar'] = $avatar;
  528. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  529. if ($hdUpgrading == 1) {
  530. util::fail('系统升级中,稍后再试');
  531. }
  532. $apiHost = Yii::$app->params['hdHost'];
  533. $imgUploadApi = $apiHost . '/upload/save-file';
  534. util::success(['token' => $token, 'account' => $sjId, 'shopId' => $currentShopId, 'imgUploadApi' => $imgUploadApi, 'switchShop' => $switchShop,]);
  535. }
  536. //静默获取小程序用户信息
  537. public function actionMiniInfo()
  538. {
  539. //花店平台
  540. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  541. $code = Yii::$app->request->get('code', '');
  542. $wxMiniBase = WxOpenService::getWxMiniBase();
  543. $appId = $wxMiniBase['miniAppId'];
  544. $appSecret = $wxMiniBase['miniAppSecret'];
  545. if (empty($appSecret)) {
  546. Yii::info('没有找到小程序的密钥');
  547. //没有管理店铺不允许登录
  548. util::success(['token' => '']);
  549. }
  550. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  551. $curl = new curl\Curl();
  552. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  553. $result = $curl->get($url);
  554. $arr = Json::decode($result);
  555. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  556. $openid = $arr['openid'] ?? '';
  557. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $openid;
  558. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  559. if (empty($openid)) {
  560. Yii::info(json_encode($arr));
  561. //没有管理店铺不允许登录
  562. util::success(['token' => '', 'currentMiniOpenId' => $openid,'sn'=>1]);
  563. }
  564. $admin = AdminService::getByCondition(['miniOpenId' => $openid]);
  565. if (empty($admin)) {
  566. //没有管理店铺不允许登录
  567. util::success(['token' => '', 'currentMiniOpenId' => $openid,'sn'=>2]);
  568. }
  569. $adminId = $admin['id'];
  570. //切换账号,多个地方要同步修改,关键词 change_account
  571. // if (getenv('YII_ENV') == 'production') {
  572. // if ($adminId == 4) {
  573. // $adminId = 28276;
  574. // $adminId = 27522;
  575. // $admin = AdminClass::getById($adminId);
  576. // }
  577. // } else {
  578. // if ($adminId == 919) {
  579. // $adminId = 1109;
  580. // $admin = AdminClass::getById($adminId);
  581. // }
  582. // }
  583. $currentShopId = $admin['currentShopId'] ?? 0;
  584. if (empty($currentShopId)) {
  585. //没有管理店铺不允许登录
  586. util::success(['token' => '', 'currentMiniOpenId' => $openid,'sn'=>3]);
  587. }
  588. $currentShop = ShopClass::getById($currentShopId, true);
  589. if (empty($currentShop)) {
  590. //没有管理店铺不允许登录
  591. util::success(['token' => '', 'currentMiniOpenId' => $openid,'sn'=>4]);
  592. }
  593. $mainId = $currentShop->mainId ?? 0;
  594. $pfShopId = $currentShop->pfShopId ?? 0;
  595. $onlyCg = $currentShop->onlyCg ?? 1;
  596. $shareLogo = imgUtil::groupImg('buy_logo.jpg');
  597. if (isset($currentShop->shareLogo) && !empty($currentShop->shareLogo)) {
  598. $shareLogo = imgUtil::groupImg($currentShop->shareLogo);
  599. }
  600. $shopAdmin = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  601. if (empty($shopAdmin)) {
  602. //没有管理店铺不允许登录
  603. util::success(['token' => '', 'currentMiniOpenId' => $openid,'sn'=>5]);
  604. }
  605. if (isset($shopAdmin->status) == false || $shopAdmin->status == 0) {
  606. //账号冻结不再自动登录
  607. util::success(['token' => '', 'currentMiniOpenId' => $openid,'sn'=>6]);
  608. }
  609. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  610. $shopAdmin->save();
  611. $shopAdminId = $shopAdmin->id ?? 0;
  612. //是否有切换门店的权限
  613. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  614. $prefix = imgUtil::getPrefix();
  615. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  616. $admin['avatar'] = $avatar;
  617. $token = jwt::getNewToken($adminId);
  618. $showDemo = 1;
  619. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  620. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  621. if ($hdUpgrading == 1) {
  622. util::fail('系统升级中,稍后再试');
  623. }
  624. $apiHost = Yii::$app->params['hdHost'];
  625. $imgUploadApi = $apiHost . '/upload/save-file';
  626. //惠雅鲜花员工不能看收入情况
  627. if (in_array($adminId, [2366, 2812, 4004])) {
  628. $shopAdmin->super = 0;
  629. }
  630. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  631. util::success([
  632. 'token' => $token,
  633. 'admin' => $admin,
  634. 'shopId' => $currentShopId,
  635. 'shopAdminId' => $shopAdminId,
  636. 'switchShop' => $switchShop,
  637. 'pfShopId' => $pfShopId,
  638. 'onlyCg' => $onlyCg,
  639. 'shareLogo' => $shareLogo,
  640. 'showDemo' => $showDemo,
  641. 'imgUploadApi' => $imgUploadApi,
  642. //有小程序新版本提示更新
  643. 'update' => $remind,
  644. 'currentMiniOpenId' => $openid,
  645. 'staff' => $shopAdmin,
  646. ]);
  647. }
  648. //获取收款码的二维码图片 ssh 20210602
  649. public function actionGatheringImgUrl()
  650. {
  651. $id = Yii::$app->request->get('id', 0);
  652. $shop = ShopClass::getById($id, true);
  653. if (empty($shop)) {
  654. util::fail('没有找到门店40');
  655. }
  656. $sjId = $shop->sjId;
  657. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  658. $fileResource = file_get_contents($imgUrl);
  659. header('Content-type: image/jpeg');
  660. echo $fileResource;
  661. }
  662. //最新版本 ssh 20220211
  663. public function actionGetApkVersion()
  664. {
  665. $version = getenv('HD_NEW_APK_VERSION') == false ? '1.0.0' : getenv('HD_NEW_APK_VERSION');
  666. $mustUpdate = getenv('HD_NEW_APK_VERSION_MUST_UPDATE') == false ? 0 : getenv('HD_NEW_APK_VERSION_MUST_UPDATE');
  667. util::success(['version' => $version, 'mustUpdate' => $mustUpdate]);
  668. }
  669. //收银台apk更新 ssh 20220310
  670. public function actionGetPadApkVersion()
  671. {
  672. $version = getenv('HD_NEW_PAD_APK_VERSION') == false ? '1.0.0' : getenv('HD_NEW_PAD_APK_VERSION');
  673. $mustUpdate = getenv('HD_NEW_PAD_APK_VERSION_MUST_UPDATE') == false ? 0 : getenv('HD_NEW_PAD_APK_VERSION_MUST_UPDATE');
  674. util::success(['version' => $version, 'mustUpdate' => $mustUpdate]);
  675. }
  676. }