AuthController.php 27 KB

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