AuthController.php 29 KB

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