AuthController.php 29 KB

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