AuthController.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\pt\classes\AgreementClass;
  5. use biz\shop\classes\ShopClass;
  6. use bizGhs\item\classes\LabelClass;
  7. use bizHd\admin\classes\ShopAdminClass;
  8. use bizGhs\shop\services\ShopAdminService;
  9. use bizGhs\admin\services\AdminService;
  10. use biz\wx\classes\WxOpenClass;
  11. use common\components\dict;
  12. use common\components\httpUtil;
  13. use common\components\imgUtil;
  14. use common\components\jwt;
  15. use common\components\noticeUtil;
  16. use common\components\sms;
  17. use common\components\util;
  18. use Yii;
  19. use common\components\stringUtil;
  20. use biz\sj\services\MerchantService;
  21. use bizHd\user\services\UserService;
  22. use common\services\xhMerchantService;
  23. use common\components\wxUtil;
  24. use linslin\yii2\curl;
  25. use yii\helpers\Json;
  26. //用户登陆
  27. class AuthController extends PublicController
  28. {
  29. //小程序获取手机号并登录 (新接口) ssh 20250728
  30. public function actionGetMiniMobile()
  31. {
  32. $post = Yii::$app->request->post();
  33. $code = $post['code'] ?? '';
  34. $miniOpenId = $post['miniOpenId'] ?? '';
  35. if (empty($code)) {
  36. util::fail('参数错误:缺少code');
  37. }
  38. // 使用 miniUtil 封装好的方法获取手机号 (ptStyle=2 表示供货商端)
  39. $merchant = WxOpenClass::getGhsWxInfo();
  40. $res = \common\components\miniUtil::getPhoneNumber($merchant, $code, 2);
  41. if (isset($res['errcode']) && $res['errcode'] == 0) {
  42. $phoneNumber = $res['phone_info']['purePhoneNumber'] ?? '';
  43. if (empty($phoneNumber)) {
  44. util::fail('获取手机号内容为空');
  45. }
  46. // 登录逻辑 (同步自 actionWxMobileLogin)
  47. $admin = AdminClass::getByCondition(['mobile' => $phoneNumber]);
  48. if (empty($admin)) {
  49. util::fail('请先注册或请店长添加自己为员工');
  50. }
  51. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  52. $openShop = $admin['openGhsShop'] ?? 1;
  53. if (empty($currentShopId)) {
  54. if ($openShop == 2) {
  55. util::fail('审核中');
  56. }
  57. util::fail('请先注册..');
  58. }
  59. $currentShop = ShopClass::getById($currentShopId, true);
  60. if (empty($currentShop)) {
  61. util::fail('没有找到门店69');
  62. }
  63. $mainId = $currentShop->mainId ?? 0;
  64. $adminId = $admin['id'] ?? 0;
  65. if (isset($admin['ghsMiniOpenId']) && empty($admin['ghsMiniOpenId'])) {
  66. AdminClass::updateById($adminId, ['ghsMiniOpenId' => $miniOpenId]);
  67. }
  68. $admin['miniOpenId'] = $miniOpenId;
  69. //1没有开店 2已申请待审核 3已开店
  70. $openShop = $admin['openGhsShop'] ?? 1;
  71. if ($openShop == 2) {
  72. util::fail('帐号审核中');
  73. }
  74. $shopAdmin = ShopAdminClass::getByCondition(['adminId' => $adminId, 'mainId' => $mainId], true);
  75. if (empty($shopAdmin)) {
  76. util::fail('请先注册...');
  77. }
  78. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  79. $shopAdmin->save();
  80. $shopAdminId = $shopAdmin->id ?? 0;
  81. //是否有切换门店的权限
  82. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  83. $admin['avatar'] = imgUtil::getPrefix() . '/retail/default-img.png';
  84. $token = jwt::getNewToken($adminId);
  85. $showDemo = 1;
  86. $remind = getenv('GHS_UPDATE_REMIND') == false ? 0 : getenv('GHS_UPDATE_REMIND');
  87. $shop = ShopClass::getById($currentShopId, true);
  88. $skCustomId = $shop->skCustomId ?? 0;
  89. $apiHost = Yii::$app->params['ghsHost'];
  90. $imgUploadApi = $apiHost . '/upload/save-file';
  91. //使用手册
  92. $cacheKey = 'close_book_' . $shopAdminId;
  93. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  94. $showBook = !empty($hasClose) ? 0 : 1;
  95. $cacheKey = 'has_hit_navigate_' . $shopAdminId;
  96. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  97. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  98. $ghsUpgrading = getenv('GHS_UPGRADING') == false ? 0 : getenv('GHS_UPGRADING');
  99. if ($ghsUpgrading == 1) {
  100. $allowShopAdminIdsStr = getenv('GHS_UPGRADE_ALLOW_SHOP_ADMIN_IDS') ?: '';
  101. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  102. if (!in_array($shopAdminId, $allowShopAdminIds)) {
  103. util::fail('系统升级中,稍后再试');
  104. }
  105. }
  106. $lookAllShop = 0;
  107. if (getenv('YII_ENV') == 'production') {
  108. $couldLookAllShop = dict::getDict('couldLookAllShop');
  109. if (in_array($adminId, $couldLookAllShop)) {
  110. $lookAllShop = 1;
  111. }
  112. } else {
  113. $lookAllShop = 1;
  114. }
  115. //惠雅鲜花员工不能看收入情况
  116. if (in_array($adminId, [2366, 2812, 4004])) {
  117. $shopAdmin->super = 0;
  118. }
  119. $labelList = LabelClass::getMyUnUseLabelList($mainId);
  120. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  121. util::success([
  122. 'token' => $token,
  123. 'admin' => $admin,
  124. 'shopAdminId' => $shopAdminId,
  125. 'shopId' => $currentShopId,
  126. 'switchShop' => $switchShop,
  127. 'openShop' => $openShop,
  128. 'showDemo' => $showDemo,
  129. //有小程序新版本提示更新
  130. 'update' => $remind,
  131. 'skCustomId' => $skCustomId,
  132. 'apiHost' => $apiHost,
  133. 'imgUploadApi' => $imgUploadApi,
  134. 'showBook' => $showBook,
  135. 'hasHitNavigate' => $hasHitNavigate,
  136. 'staff' => $shopAdmin,
  137. 'lookAllShop' => $lookAllShop,
  138. 'labelList' => $labelList,
  139. ]);
  140. } else {
  141. $errMsg = $res['errmsg'] ?? '获取手机号失败';
  142. $errCode = $res['errcode'] ?? -1;
  143. Yii::info("小程序新接口获取手机号失败 (GHS):code={$errCode}, msg={$errMsg}");
  144. noticeUtil::push("供货商小程序新接口获取手机号失败:code={$errCode}, msg={$errMsg}", '15280215347');
  145. util::fail("获取手机号失败:{$errMsg}({$errCode})");
  146. }
  147. }
  148. //获取手机号
  149. public function actionGetMobile()
  150. {
  151. $post = Yii::$app->request->post();
  152. $iv = $post['iv'];
  153. $encryptedData = $post['encryptedData'];
  154. $merchant = WxOpenClass::getGhsWxInfo();
  155. $appId = $merchant['miniAppId'];
  156. $miniOpenId = $post['miniOpenId'] ?? '';
  157. if (empty($miniOpenId)) {
  158. util::success(['hasNoOpenId' => 1], '登录失败,没有miniOpenId');
  159. }
  160. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  161. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  162. if (empty($sessionKey)) {
  163. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  164. util::success(['hasNoOpenId' => 1], '登录失败!');
  165. }
  166. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  167. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  168. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  169. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  170. if ($errCode != 0) {
  171. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  172. util::success(['hasNoOpenId' => 1], '登录失败...');
  173. }
  174. $arr = Json::decode($result);
  175. $phoneNumber = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  176. if (empty($phoneNumber)) {
  177. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  178. util::success(['hasNoOpenId' => 1], '登录失败了哦');
  179. }
  180. $cacheKey = 'getMobile_' . $phoneNumber;
  181. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 1]);
  182. util::success(['mobile' => $phoneNumber]);
  183. }
  184. //获取微信昵称
  185. public function actionGetNickname()
  186. {
  187. $post = Yii::$app->request->post();
  188. $iv = $post['iv'] ?? '';
  189. $encryptedData = $post['encryptedData'] ?? '';
  190. $merchant = WxOpenClass::getGhsWxInfo();
  191. $appId = $merchant['miniAppId'];
  192. $miniOpenId = $post['miniOpenId'] ?? '';
  193. if (empty($miniOpenId)) {
  194. util::fail('获取失败,请重新打开小程序');
  195. }
  196. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  197. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  198. if (empty($sessionKey)) {
  199. util::fail('获取失败,请重新打开小程序');
  200. }
  201. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  202. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  203. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  204. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  205. if ($errCode != 0) {
  206. util::fail('获取微信昵称失败');
  207. }
  208. $arr = Json::decode($result);
  209. $nickName = $arr['nickName'] ?? '';
  210. if (empty($nickName)) {
  211. util::fail('没获取到微信昵称');
  212. }
  213. util::success(['name' => $nickName, 'nickName' => $nickName]);
  214. }
  215. //微信手机号一键登录 ssh 20210121
  216. public function actionWxMobileLogin()
  217. {
  218. $post = Yii::$app->request->post();
  219. $iv = $post['iv'];
  220. $encryptedData = $post['encryptedData'];
  221. $merchant = WxOpenClass::getGhsWxInfo();
  222. $appId = $merchant['miniAppId'];
  223. $miniOpenId = $post['miniOpenId'] ?? '';
  224. if (empty($miniOpenId)) {
  225. util::success(['hasNoOpenId' => 1], '登录失败,没有miniOpenId');
  226. }
  227. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  228. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  229. if (empty($sessionKey)) {
  230. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  231. //noticeUtil::push($cacheKey . " 登录失败!!sessionKey空的", '15280215347');
  232. util::success(['hasNoOpenId' => 1], '登录失败。');
  233. }
  234. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  235. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  236. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  237. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  238. if ($errCode != 0) {
  239. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  240. //noticeUtil::push($cacheKey . " 批发商 登录失败!!!!sessionKey异常", '15280215347');
  241. util::success(['hasNoOpenId' => 1], '登录失败!');
  242. }
  243. $arr = Json::decode($result);
  244. $phoneNumber = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  245. if (empty($phoneNumber)) {
  246. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  247. noticeUtil::push($cacheKey . "批发商 登录失败了!获取手机号失败", '15280215347');
  248. util::success(['hasNoOpenId' => 1], '登录失败了哦');
  249. }
  250. $admin = AdminClass::getByCondition(['mobile' => $phoneNumber]);
  251. if (empty($admin)) {
  252. util::fail('请先注册或请店长添加自己为员工');
  253. }
  254. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  255. $openShop = $admin['openGhsShop'] ?? 1;
  256. if (empty($currentShopId)) {
  257. if ($openShop == 2) {
  258. util::fail('审核中');
  259. }
  260. util::fail('请先注册..');
  261. }
  262. $currentShop = ShopClass::getById($currentShopId, true);
  263. if (empty($currentShop)) {
  264. util::fail('没有找到门店69');
  265. }
  266. $mainId = $currentShop->mainId ?? 0;
  267. $adminId = $admin['id'] ?? 0;
  268. if (isset($admin['ghsMiniOpenId']) && empty($admin['ghsMiniOpenId'])) {
  269. AdminClass::updateById($adminId, ['ghsMiniOpenId' => $miniOpenId]);
  270. }
  271. $admin['miniOpenId'] = $miniOpenId;
  272. //1没有开店 2已申请待审核 3已开店
  273. $openShop = $admin['openGhsShop'] ?? 1;
  274. if ($openShop == 2) {
  275. util::fail('帐号审核中');
  276. }
  277. $shopAdmin = ShopAdminClass::getByCondition(['adminId' => $adminId, 'mainId' => $mainId], true);
  278. if (empty($shopAdmin)) {
  279. util::fail('请先注册...');
  280. }
  281. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  282. $shopAdmin->save();
  283. $shopAdminId = $shopAdmin->id ?? 0;
  284. //是否有切换门店的权限
  285. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  286. $admin['avatar'] = imgUtil::getPrefix() . '/retail/default-img.png';
  287. $token = jwt::getNewToken($adminId);
  288. $showDemo = 1;
  289. $remind = getenv('GHS_UPDATE_REMIND') == false ? 0 : getenv('GHS_UPDATE_REMIND');
  290. $shop = ShopClass::getById($currentShopId, true);
  291. $skCustomId = $shop->skCustomId ?? 0;
  292. $apiHost = Yii::$app->params['ghsHost'];
  293. $imgUploadApi = $apiHost . '/upload/save-file';
  294. //使用手册
  295. $cacheKey = 'close_book_' . $shopAdminId;
  296. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  297. $showBook = !empty($hasClose) ? 0 : 1;
  298. $cacheKey = 'has_hit_navigate_' . $shopAdminId;
  299. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  300. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  301. $ghsUpgrading = getenv('GHS_UPGRADING') == false ? 0 : getenv('GHS_UPGRADING');
  302. if ($ghsUpgrading == 1) {
  303. $allowShopAdminIdsStr = getenv('GHS_UPGRADE_ALLOW_SHOP_ADMIN_IDS') ?: '';
  304. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  305. if (!in_array($shopAdminId, $allowShopAdminIds)) {
  306. util::fail('系统升级中,稍后再试');
  307. }
  308. }
  309. $lookAllShop = 0;
  310. if (getenv('YII_ENV') == 'production') {
  311. $couldLookAllShop = dict::getDict('couldLookAllShop');
  312. if (in_array($adminId, $couldLookAllShop)) {
  313. $lookAllShop = 1;
  314. }
  315. } else {
  316. $lookAllShop = 1;
  317. }
  318. //惠雅鲜花员工不能看收入情况
  319. if (in_array($adminId, [2366, 2812, 4004])) {
  320. $shopAdmin->super = 0;
  321. }
  322. $labelList = LabelClass::getMyUnUseLabelList($mainId);
  323. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  324. util::success([
  325. 'token' => $token,
  326. 'admin' => $admin,
  327. 'shopAdminId' => $shopAdminId,
  328. 'shopId' => $currentShopId,
  329. 'switchShop' => $switchShop,
  330. 'openShop' => $openShop,
  331. 'showDemo' => $showDemo,
  332. //有小程序新版本提示更新
  333. 'update' => $remind,
  334. 'skCustomId' => $skCustomId,
  335. 'apiHost' => $apiHost,
  336. 'imgUploadApi' => $imgUploadApi,
  337. 'showBook' => $showBook,
  338. 'hasHitNavigate' => $hasHitNavigate,
  339. 'staff' => $shopAdmin,
  340. 'lookAllShop' => $lookAllShop,
  341. 'labelList' => $labelList,
  342. ]);
  343. }
  344. //本机号码一键登录 ssh 20210117
  345. public function actionPhoneLogin()
  346. {
  347. $get = Yii::$app->request->get();
  348. $access_token = $get['access_token'] ?? '';
  349. $openid = $get['openid'] ?? '';
  350. if (empty($access_token) || empty($openid)) {
  351. util::fail('参数错误');
  352. }
  353. //密钥,需要和uniCloud里的一致
  354. $secret = 'XHB2021198819640123';
  355. $params = ['access_token' => $access_token, 'openid' => $openid];
  356. $stringSignTemp = '';
  357. foreach ($params as $k => $v) {
  358. $stringSignTemp .= $k . '=' . $v . '&';
  359. }
  360. $stringSignTemp = rtrim($stringSignTemp, '&');
  361. $sign = hash_hmac('sha256', $stringSignTemp, $secret);
  362. $url = Yii::$app->params['ghsUniCloudHost'] . "/getMobileNumber?access_token=" . $access_token . "&sign=" . $sign . "&" . $stringSignTemp;
  363. $curl = new curl\Curl();
  364. $respond = $curl->get($url);
  365. Yii::info($respond);
  366. $result = json_decode($respond, true);
  367. if (isset($result['code']) == false || $result['code'] != 1) {
  368. util::fail('获取手机号失败');
  369. }
  370. $phoneNumber = $result['data']['phoneNumber'] ?? '';
  371. if (empty($phoneNumber)) {
  372. util::fail('没有获取到手机号');
  373. }
  374. $ptStyle = dict::getDict('ptStyle', 'ghs');
  375. $admin = AdminClass::getByCondition(['mobile' => $phoneNumber, 'style' => $ptStyle]);
  376. if (empty($admin)) {
  377. $adminInfo = [
  378. 'name' => $phoneNumber,
  379. 'mobile' => $phoneNumber,
  380. 'style' => $ptStyle,
  381. ];
  382. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  383. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($adminInfo, $fromApp);
  384. }
  385. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  386. $openShop = $admin['openGhsShop'] ?? 1;
  387. if (empty($currentShopId)) {
  388. if ($openShop == 2) {
  389. util::fail('审核中');
  390. }
  391. util::fail('请先注册');
  392. }
  393. $adminId = $admin['id'] ?? 0;
  394. //1没有开店 2已申请待审核 3已开店
  395. $openShop = $admin['openGhsShop'] ?? 1;
  396. if ($openShop == 2) {
  397. util::fail('帐号审核中');
  398. }
  399. $currentShop = ShopClass::getById($currentShopId, true);
  400. if (empty($currentShop)) {
  401. util::fail('没有找到门店70');
  402. }
  403. $mainId = $currentShop->mainId ?? 0;
  404. $shopAdmin = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  405. if (empty($shopAdmin)) {
  406. util::fail('请先注册');
  407. }
  408. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  409. $shopAdmin->save();
  410. $shopAdminId = $shopAdmin->id ?? 0;
  411. //是否有切换门店的权限
  412. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  413. $admin['avatar'] = imgUtil::getPrefix() . '/retail/default-img.png';
  414. $token = jwt::getNewToken($adminId);
  415. $showDemo = 1;
  416. $remind = getenv('GHS_UPDATE_REMIND') == false ? 0 : getenv('GHS_UPDATE_REMIND');
  417. $shop = ShopClass::getById($currentShopId, true);
  418. $skCustomId = $shop->skCustomId ?? 0;
  419. $apiHost = Yii::$app->params['ghsHost'];
  420. $imgUploadApi = $apiHost . '/upload/save-file';
  421. //使用手册
  422. $cacheKey = 'close_book_' . $shopAdminId;
  423. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  424. $showBook = !empty($hasClose) ? 0 : 1;
  425. $cacheKey = 'has_hit_navigate_' . $shopAdminId;
  426. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  427. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  428. $ghsUpgrading = getenv('GHS_UPGRADING') == false ? 0 : getenv('GHS_UPGRADING');
  429. if ($ghsUpgrading == 1) {
  430. $allowShopAdminIdsStr = getenv('GHS_UPGRADE_ALLOW_SHOP_ADMIN_IDS') ?: '';
  431. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  432. if (!in_array($shopAdminId, $allowShopAdminIds)) {
  433. util::fail('系统升级中,稍后再试');
  434. }
  435. }
  436. $lookAllShop = 0;
  437. if (getenv('YII_ENV') == 'production') {
  438. $couldLookAllShop = dict::getDict('couldLookAllShop');
  439. if (in_array($adminId, $couldLookAllShop)) {
  440. $lookAllShop = 1;
  441. }
  442. } else {
  443. $lookAllShop = 1;
  444. }
  445. //惠雅鲜花员工不能看收入情况
  446. if (in_array($adminId, [2366, 2812, 4004])) {
  447. $shopAdmin->super = 0;
  448. }
  449. $labelList = LabelClass::getMyUnUseLabelList($mainId);
  450. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  451. util::success([
  452. 'token' => $token,
  453. 'admin' => $admin,
  454. 'shopAdminId' => $shopAdminId,
  455. 'shopId' => $currentShopId,
  456. 'switchShop' => $switchShop,
  457. 'openShop' => $openShop,
  458. 'showDemo' => $showDemo,
  459. //有小程序新版本提示更新
  460. 'update' => $remind,
  461. 'skCustomId' => $skCustomId,
  462. 'apiHost' => $apiHost,
  463. 'imgUploadApi' => $imgUploadApi,
  464. 'showBook' => $showBook,
  465. 'hasHitNavigate' => $hasHitNavigate,
  466. 'staff' => $shopAdmin,
  467. 'lookAllShop' => $lookAllShop,
  468. 'labelList' => $labelList,
  469. ]);
  470. util::success(['admin' => $admin]);
  471. }
  472. //准备授权 ssh 2019.11.19
  473. public function actionPrepare()
  474. {
  475. $get = Yii::$app->request->get();
  476. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  477. if (empty($url)) {
  478. util::fail('没有网址');
  479. }
  480. $account = httpUtil::getAccount($url);
  481. if (empty($account)) {
  482. util::fail('没有商家帐号');
  483. }
  484. $merchant = MerchantService::getById($account);
  485. if (empty($merchant)) {
  486. util::fail('商家无效');
  487. }
  488. /**
  489. * authScope 参数的说明
  490. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  491. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  492. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  493. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  494. */
  495. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  496. $urlEncode = stringUtil::urlSafeB64Encode($url);
  497. //微信授权获取code ssh 2019.11.24
  498. $isOpen = 2;
  499. wxUtil::getCode($urlEncode, $merchant, $authScope, $isOpen);
  500. util::end();
  501. }
  502. //微信授权获取用户信息 ssh 2019.11.20
  503. public function actionGetUserInfo()
  504. {
  505. $get = Yii::$app->request->get();
  506. $code = isset($get['code']) ? $get['code'] : '';
  507. if (empty($code)) {
  508. util::fail('没有获取用户code');
  509. }
  510. if (isset($get['state']) && $get['state'] == 'authdeny') {
  511. util::fail('auth fail');
  512. }
  513. $urlEncode = $get['url'];
  514. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  515. $account = httpUtil::getAccount($url);
  516. if (empty($account)) {
  517. util::stop('商店ID无效!!');
  518. }
  519. $merchant = xhMerchantService::getByAccount($account);
  520. if (empty($merchant)) {
  521. util::stop('商店无效');
  522. }
  523. $sjId = $merchant['id'];
  524. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  525. if (empty($authScope)) {
  526. util::stop('没有授权类型');
  527. }
  528. $data = wxUtil::getOpenId($code, $merchant, 2);
  529. if ($data === false) {
  530. //微信授权获取code ssh 2019.11.24
  531. wxUtil::getCode($urlEncode, $merchant, $authScope, 2);
  532. util::end();
  533. }
  534. $openId = $data['openid'];
  535. $access_token = $data['access_token'];
  536. $user = UserService::getByOpenId($openId);
  537. //用户来源
  538. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  539. $source = $userSource['name'];
  540. if (empty($user)) {
  541. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  542. $originalInfo = [];
  543. $originalInfo['openId'] = $openId;
  544. $originalInfo['sjId'] = $sjId;
  545. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  546. if ($authScope == 'snsapi_userinfo') {
  547. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  548. $originalInfo = array_merge($baseInfo, $originalInfo);
  549. $originalInfo['isFull'] = 1;
  550. $originalInfo['unionId'] = $baseInfo['unionid'];
  551. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  552. $originalInfo['nickName'] = $baseInfo['nickName'];
  553. }
  554. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  555. } else {
  556. if ($user['isFull'] == 0) {
  557. if ($authScope == 'snsapi_userinfo') {
  558. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  559. $originalInfo = $baseInfo;
  560. $originalInfo['isFull'] = 1;
  561. $originalInfo['unionId'] = $baseInfo['unionid'];
  562. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  563. $originalInfo['openId'] = $baseInfo['openid'];
  564. $originalInfo['nickName'] = $baseInfo['nickName'];
  565. $originalInfo['sjId'] = $sjId;
  566. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  567. }
  568. }
  569. }
  570. //这个的基类没有设置统一的全局变量,这里进行设置一次
  571. $userId = $user['id'];
  572. $admin = AdminService::getByCondition(['userId' => $userId]);
  573. if (empty($admin)) {
  574. util::fail('您无法访问');
  575. }
  576. $adminId = $admin['id'];
  577. //获取token
  578. $token = jwt::getNewToken($adminId);
  579. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  580. $this->redirect($url);
  581. }
  582. //登陆并拿到token ssh 2019.11.23
  583. public function actionLogin()
  584. {
  585. $get = Yii::$app->request->get();
  586. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  587. if (stringUtil::isMobile($mobile) == false) {
  588. util::fail('请填写正常的手机号');
  589. }
  590. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  591. if (empty($password)) {
  592. util::fail('请输入密码');
  593. }
  594. $admin = AdminService::getByCondition(['mobile' => $mobile]);
  595. if (empty($admin)) {
  596. util::fail('请先注册吧');
  597. }
  598. if (password_verify($password, $admin['password']) == false) {
  599. util::fail('密码错误');
  600. }
  601. $adminId = $admin['id'];
  602. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'delStatus' => 0]);
  603. $sjId = $shopAdmin['sjId'] ?? 0;
  604. $shopId = $admin['currentGhsShopId'] ?? 0;
  605. if (empty($shopId)) {
  606. util::fail('没有权限登录');
  607. }
  608. //获取token
  609. $token = jwt::getNewToken($adminId);
  610. util::success(['token' => $token, 'account' => $sjId, 'shopId' => $shopId]);
  611. }
  612. /**
  613. * 静默获取小程序用户信息
  614. * 职责:通过微信小程序 code 换取 openid 和 session_key 并自动登录供货商端
  615. * 入参:GET 传参 code (微信临时登录凭证)
  616. * 返回:登录成功返回 token、用户信息、店铺 ID 等;失败返回空 token
  617. * 副作用:会更新管理员的最后登录时间,并将 session_key 写入 Redis 缓存
  618. */
  619. public function actionMiniInfo()
  620. {
  621. //供货商平台
  622. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  623. $code = Yii::$app->request->get('code', '');
  624. $wxMiniBase = WxOpenClass::getGhsWxInfo();
  625. $appId = $wxMiniBase['miniAppId'];
  626. $appSecret = $wxMiniBase['miniAppSecret'];
  627. if (empty($appSecret)) {
  628. //没有管理店铺不允许登录
  629. util::success(['token' => '', 'number' => 1]);
  630. }
  631. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  632. $curl = new curl\Curl();
  633. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  634. // 复杂分支/关键逻辑:设置 cURL 连接和执行超时,并强制使用 IPv4,防止因微信接口卡顿或 DNS 解析慢拖垮 PHP-FPM 进程
  635. $curl->setOption(CURLOPT_CONNECTTIMEOUT, 2); // 连接超时限制为 2 秒,避免网络握手长时间卡死
  636. $curl->setOption(CURLOPT_TIMEOUT, 3); // 总执行时间限制为 3 秒,避免请求挂起时间过长
  637. $curl->setOption(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // 强制 IPv4 解析,防止 IPv6 解析超时重试
  638. $result = $curl->get($url);
  639. $arr = Json::decode($result);
  640. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  641. $openid = $arr['openid'] ?? '';
  642. if (empty($openid)) {
  643. //没有管理店铺不允许登录
  644. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'number' => 2]);
  645. }
  646. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $openid;
  647. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  648. $admin = AdminClass::getByCondition(['ghsMiniOpenId' => $openid]);
  649. if (empty($admin)) {
  650. //没有管理店铺不允许登录
  651. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'number' => 3]);
  652. }
  653. $adminId = $admin['id'];
  654. //切换账号,多个地方要同步修改,关键词 change_account
  655. // if (getenv('YII_ENV') == 'production') {
  656. // if ($adminId == 4) {
  657. // $adminId = 16020;
  658. // $admin = AdminClass::getById($adminId);
  659. // }
  660. // } else {
  661. // if ($adminId == 919) {
  662. // $adminId = 1109;
  663. // $admin = AdminClass::getById($adminId);
  664. // }
  665. // }
  666. $openShop = $admin['openGhsShop'] ?? 1;
  667. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  668. if (empty($currentShopId)) {
  669. //没有管理店铺不允许登录
  670. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'number' => 4]);
  671. }
  672. $currentShop = ShopClass::getById($currentShopId, true);
  673. if (empty($currentShop)) {
  674. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'number' => 4]);
  675. }
  676. $mainId = $currentShop->mainId ?? 0;
  677. $shopAdmin = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  678. if (empty($shopAdmin)) {
  679. //没有管理店铺不允许登录
  680. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'number' => 5]);
  681. }
  682. $shopAdminId = $shopAdmin->id ?? 0;
  683. //是否有切换门店的权限
  684. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  685. $admin['avatar'] = imgUtil::getPrefix() . '/retail/default-img.png';
  686. $token = jwt::getNewToken($adminId);
  687. $showDemo = 1;
  688. $remind = getenv('GHS_UPDATE_REMIND') == false ? 0 : getenv('GHS_UPDATE_REMIND');
  689. $shop = ShopClass::getById($currentShopId, true);
  690. $skCustomId = $shop->skCustomId ?? 0;
  691. $mainId = $shop->mainId ?? 0;
  692. $apiHost = Yii::$app->params['ghsHost'];
  693. $imgUploadApi = $apiHost . '/upload/save-file';
  694. //使用手册
  695. $cacheKey = 'close_book_' . $shopAdminId;
  696. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  697. $showBook = !empty($hasClose) ? 0 : 1;
  698. $cacheKey = 'has_hit_navigate_' . $shopAdminId;
  699. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  700. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  701. $ghsUpgrading = getenv('GHS_UPGRADING') == false ? 0 : getenv('GHS_UPGRADING');
  702. if ($ghsUpgrading == 1) {
  703. $allowShopAdminIdsStr = getenv('GHS_UPGRADE_ALLOW_SHOP_ADMIN_IDS') ?: '';
  704. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  705. if (!in_array($shopAdminId, $allowShopAdminIds)) {
  706. util::fail('系统升级中,稍后再试');
  707. }
  708. }
  709. $lookAllShop = 0;
  710. if (getenv('YII_ENV') == 'production') {
  711. $couldLookAllShop = dict::getDict('couldLookAllShop');
  712. if (in_array($adminId, $couldLookAllShop)) {
  713. $lookAllShop = 1;
  714. }
  715. } else {
  716. $lookAllShop = 1;
  717. }
  718. //惠雅鲜花员工不能看收入情况
  719. if (in_array($adminId, [2366, 2812, 4004])) {
  720. $shopAdmin->super = 0;
  721. }
  722. $labelList = LabelClass::getMyUnUseLabelList($mainId);
  723. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  724. util::success([
  725. 'token' => $token,
  726. 'admin' => $admin,
  727. 'shopAdminId' => $shopAdminId,
  728. 'shopId' => $currentShopId,
  729. 'switchShop' => $switchShop,
  730. 'openShop' => $openShop,
  731. 'showDemo' => $showDemo,
  732. //有小程序新版本提示更新
  733. 'update' => $remind,
  734. 'skCustomId' => $skCustomId,
  735. 'apiHost' => $apiHost,
  736. 'imgUploadApi' => $imgUploadApi,
  737. 'showBook' => $showBook,
  738. 'hasHitNavigate' => $hasHitNavigate,
  739. 'staff' => $shopAdmin,
  740. 'lookAllShop' => $lookAllShop,
  741. 'labelList' => $labelList,
  742. 'currentMiniOpenId' => $openid
  743. ]);
  744. }
  745. //app登陆 ssh 20211219
  746. public function actionAppLogin()
  747. {
  748. $getParams = Yii::$app->request->get();
  749. $postParams = Yii::$app->request->post();
  750. $allParams = array_merge($getParams, $postParams);
  751. $mobile = $allParams['mobile'] ?? 0;
  752. if (!stringUtil::isMobile($mobile)) {
  753. util::fail('请填写正确手机号');
  754. }
  755. $password = !empty($allParams['password']) ? $allParams['password'] : '';
  756. if (empty($password)) {
  757. util::fail('请输入密码');
  758. }
  759. $admin = AdminService::getByCondition(['mobile' => $mobile], true);
  760. if (empty($admin)) {
  761. util::fail('请先注册哦...');
  762. }
  763. if (!password_verify($password, $admin->password)) {
  764. util::fail('密码错误');
  765. }
  766. $openShop = $admin->openGhsShop ?? 1;
  767. $currentShopId = $admin->currentGhsShopId ?? 0;
  768. if (empty($currentShopId)) {
  769. if ($openShop == 2) {
  770. util::fail('审核中');
  771. }
  772. util::fail('请先注册');
  773. }
  774. $currentShop = \bizGhs\shop\classes\ShopClass::getById($currentShopId, true);
  775. if (empty($currentShop)) {
  776. util::fail('没有找到门店71');
  777. }
  778. $mainId = $currentShop->mainId ?? 0;
  779. $adminId = $admin->id;
  780. $shopAdmin = ShopAdminService::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  781. if (empty($shopAdmin)) {
  782. util::fail('您没有权限');
  783. }
  784. if ($shopAdmin->delStatus == 1) {
  785. util::fail('您没有权限哦');
  786. }
  787. if ($shopAdmin->status == 0) {
  788. util::fail("您的账号已被冻结");
  789. }
  790. $token = jwt::getNewToken($adminId);
  791. $shopAdminId = $shopAdmin->id ?? 0;
  792. //是否有切换门店的权限
  793. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  794. //1没有开店 2已申请待审核 3已开店
  795. $openShop = $admin['openGhsShop'] ?? 1;
  796. $showDemo = 1;
  797. $shop = ShopClass::getById($currentShopId, true);
  798. $skCustomId = $shop->skCustomId ?? 0;
  799. $apiHost = Yii::$app->params['ghsHost'];
  800. $imgUploadApi = $apiHost . '/upload/save-file';
  801. //使用手册
  802. $cacheKey = 'close_book_' . $shopAdminId;
  803. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  804. $showBook = !empty($hasClose) ? 0 : 1;
  805. $cacheKey = 'has_hit_navigate_' . $shopAdminId;
  806. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  807. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  808. $ghsUpgrading = getenv('GHS_UPGRADING') == false ? 0 : getenv('GHS_UPGRADING');
  809. if ($ghsUpgrading == 1) {
  810. $allowShopAdminIdsStr = getenv('GHS_UPGRADE_ALLOW_SHOP_ADMIN_IDS') ?: '';
  811. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  812. if (!in_array($shopAdminId, $allowShopAdminIds)) {
  813. util::fail('系统升级中,稍后再试');
  814. }
  815. }
  816. $lookAllShop = 0;
  817. if (getenv('YII_ENV') == 'production') {
  818. $couldLookAllShop = dict::getDict('couldLookAllShop');
  819. if (in_array($adminId, $couldLookAllShop)) {
  820. $lookAllShop = 1;
  821. }
  822. } else {
  823. $lookAllShop = 1;
  824. }
  825. //惠雅鲜花员工不能看收入情况
  826. if (in_array($adminId, [2366, 2812, 4004])) {
  827. $shopAdmin->super = 0;
  828. }
  829. $labelList = LabelClass::getMyUnUseLabelList($mainId);
  830. //把设备状态更新为登录
  831. if (isset($allParams['deviceId']) && $allParams['deviceId'] != '') {
  832. $device = \bizGhs\device\classes\GhsDeviceClass::getByCondition(['shopId'=>$currentShopId, 'deviceId'=>$allParams['deviceId']], true);
  833. if ($device) {
  834. $device->status = 1;
  835. $device->save();
  836. }
  837. }
  838. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  839. util::success([
  840. 'token' => $token,
  841. 'admin' => $admin,
  842. 'shopAdminId' => $shopAdminId,
  843. 'shopId' => $currentShopId,
  844. 'switchShop' => $switchShop,
  845. 'openShop' => $openShop,
  846. 'showDemo' => $showDemo,
  847. //有小程序新版本提示更新
  848. 'update' => 0,
  849. 'skCustomId' => $skCustomId,
  850. 'apiHost' => $apiHost,
  851. 'imgUploadApi' => $imgUploadApi,
  852. 'showBook' => $showBook,
  853. 'hasHitNavigate' => $hasHitNavigate,
  854. 'staff' => $shopAdmin,
  855. 'lookAllShop' => $lookAllShop,
  856. 'labelList' => $labelList,
  857. ]);
  858. }
  859. //最新版本 ssh 20220211
  860. public function actionGetApkVersion()
  861. {
  862. $version = getenv('GHS_NEW_APK_VERSION') == false ? '100' : getenv('GHS_NEW_APK_VERSION');
  863. $mustUpdate = getenv('GHS_NEW_APK_VERSION_MUST_UPDATE') == false ? 0 : getenv('GHS_NEW_APK_VERSION_MUST_UPDATE');
  864. $updateText = '零售系统上线升级';
  865. util::success(['version' => $version, 'mustUpdate' => $mustUpdate, 'updateText' => $updateText]);
  866. }
  867. //收银台apk更新 ssh 20220310
  868. public function actionGetPadApkVersion()
  869. {
  870. $version = getenv('GHS_NEW_PAD_APK_VERSION') == false ? '100' : getenv('GHS_NEW_PAD_APK_VERSION');
  871. $mustUpdate = getenv('GHS_NEW_PAD_APK_VERSION_MUST_UPDATE') == false ? 0 : getenv('GHS_NEW_PAD_APK_VERSION_MUST_UPDATE');
  872. util::success(['version' => $version, 'mustUpdate' => $mustUpdate]);
  873. }
  874. //注册使用协议 ssh 20220327
  875. public function actionRegister()
  876. {
  877. $html = AgreementClass::xhbRegister(1);
  878. util::success(['html' => $html]);
  879. }
  880. //隐私政策 ssh 20220327
  881. public function actionPrivacy()
  882. {
  883. $html = AgreementClass::xhbPrivacy(1);
  884. util::success(['html' => $html]);
  885. }
  886. /**
  887. * 【用途】发送登录短信验证码
  888. * 【为什么】供短信登录功能获取验证码,并包含防刷安全限制(单手机号60秒锁、每日上限5条)
  889. */
  890. public function actionSendLoginSms()
  891. {
  892. $getParams = Yii::$app->request->get();
  893. $postParams = Yii::$app->request->post();
  894. $allParams = array_merge($getParams, $postParams);
  895. $mobile = $allParams['mobile'] ?? '';
  896. if (!stringUtil::isMobile($mobile)) {
  897. util::fail('请填写正确手机号');
  898. }
  899. // 1. 安全检查:单手机号 60 秒防刷
  900. $lockKey = 'LOGIN_SMS_LOCK_' . $mobile;
  901. if (Yii::$app->redis->executeCommand('GET', [$lockKey])) {
  902. util::fail('发送过于频繁,请稍后再试');
  903. }
  904. // 2. 安全检查:单手机号每日上限 5 条
  905. $dayKey = 'LOGIN_SMS_COUNT_' . date('Ymd') . '_' . $mobile;
  906. $sendCount = (int)Yii::$app->redis->executeCommand('GET', [$dayKey]);
  907. if ($sendCount >= 5) {
  908. util::fail('该手机号今日获取验证码已达上限');
  909. }
  910. // 3. 生成 6 位随机验证码并保存至 Redis(有效期 5 分钟)
  911. $code = (string)rand(100000, 999999);
  912. $codeCacheKey = 'LOGIN_SMS_CODE_' . $mobile;
  913. Yii::$app->redis->executeCommand('SETEX', [$codeCacheKey, 300, $code]);
  914. // 4. 调用系统自带的限流发送组件 (内置了 IP 60秒锁和每日15条限制)
  915. $minute = 5;
  916. sms::send($mobile . ',' . $code . ',' . $minute, '登录验证码:{$var},{$var}分钟内有效');
  917. // 5. 写入单手机号限制锁
  918. Yii::$app->redis->executeCommand('SETEX', [$lockKey, 60, '1']);
  919. Yii::$app->redis->executeCommand('SETEX', [$dayKey, 86400, $sendCount + 1]);
  920. util::complete('验证码发送成功');
  921. }
  922. /**
  923. * 【用途】短信验证码登录
  924. * 【为什么】提供免密码快捷登录方式,验证通过后自动签发 JWT Token 并返回商家后台所需的所有初始化数据
  925. */
  926. public function actionSmsLogin()
  927. {
  928. $getParams = Yii::$app->request->get();
  929. $postParams = Yii::$app->request->post();
  930. $allParams = array_merge($getParams, $postParams);
  931. $mobile = $allParams['mobile'] ?? '';
  932. $code = $allParams['code'] ?? '';
  933. if (empty($mobile) || empty($code)) {
  934. util::fail('手机号和验证码不能为空');
  935. }
  936. if (!stringUtil::isMobile($mobile)) {
  937. util::fail('请填写正确手机号');
  938. }
  939. // 1. 校验验证码
  940. $codeCacheKey = 'LOGIN_SMS_CODE_' . $mobile;
  941. $savedCode = Yii::$app->redis->executeCommand('GET', [$codeCacheKey]);
  942. if (empty($savedCode) || $savedCode !== $code) {
  943. util::fail('验证码错误或已过期');
  944. }
  945. // 2. 验证通过,立即销毁验证码以确保一次性使用
  946. Yii::$app->redis->executeCommand('DEL', [$codeCacheKey]);
  947. // 3. 查找用户并执行登录
  948. $admin = AdminService::getByCondition(['mobile' => $mobile], true);
  949. if (empty($admin)) {
  950. util::fail('请先注册哦...');
  951. }
  952. $openShop = $admin->openGhsShop ?? 1;
  953. $currentShopId = $admin->currentGhsShopId ?? 0;
  954. if (empty($currentShopId)) {
  955. if ($openShop == 2) {
  956. util::fail('审核中');
  957. }
  958. util::fail('请先注册');
  959. }
  960. $currentShop = \bizGhs\shop\classes\ShopClass::getById($currentShopId, true);
  961. if (empty($currentShop)) {
  962. util::fail('没有找到门店71');
  963. }
  964. $mainId = $currentShop->mainId ?? 0;
  965. $adminId = $admin->id;
  966. $shopAdmin = ShopAdminService::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  967. if (empty($shopAdmin)) {
  968. util::fail('您没有权限');
  969. }
  970. if ($shopAdmin->delStatus == 1) {
  971. util::fail('您没有权限哦');
  972. }
  973. if ($shopAdmin->status == 0) {
  974. util::fail("您的账号已被冻结");
  975. }
  976. $token = jwt::getNewToken($adminId);
  977. $shopAdminId = $shopAdmin->id ?? 0;
  978. //是否有切换门店的权限
  979. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  980. //1没有开店 2已申请待审核 3已开店
  981. $openShop = $admin['openGhsShop'] ?? 1;
  982. $showDemo = 1;
  983. $shop = ShopClass::getById($currentShopId, true);
  984. $skCustomId = $shop->skCustomId ?? 0;
  985. $apiHost = Yii::$app->params['ghsHost'];
  986. $imgUploadApi = $apiHost . '/upload/save-file';
  987. //使用手册
  988. $cacheKey = 'close_book_' . $shopAdminId;
  989. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  990. $showBook = !empty($hasClose) ? 0 : 1;
  991. $cacheKey = 'has_hit_navigate_' . $shopAdminId;
  992. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  993. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  994. $ghsUpgrading = getenv('GHS_UPGRADING') == false ? 0 : getenv('GHS_UPGRADING');
  995. if ($ghsUpgrading == 1) {
  996. $allowShopAdminIdsStr = getenv('GHS_UPGRADE_ALLOW_SHOP_ADMIN_IDS') ?: '';
  997. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  998. if (!in_array($shopAdminId, $allowShopAdminIds)) {
  999. util::fail('系统升级中,稍后再试');
  1000. }
  1001. }
  1002. $lookAllShop = 0;
  1003. if (getenv('YII_ENV') == 'production') {
  1004. $couldLookAllShop = dict::getDict('couldLookAllShop');
  1005. if (in_array($adminId, $couldLookAllShop)) {
  1006. $lookAllShop = 1;
  1007. }
  1008. } else {
  1009. $lookAllShop = 1;
  1010. }
  1011. //惠雅鲜花员工不能看收入情况
  1012. if (in_array($adminId, [2366, 2812, 4004])) {
  1013. $shopAdmin->super = 0;
  1014. }
  1015. $labelList = LabelClass::getMyUnUseLabelList($mainId);
  1016. //把设备状态更新为登录
  1017. if (isset($allParams['deviceId']) && $allParams['deviceId'] != '') {
  1018. $device = \bizGhs\device\classes\GhsDeviceClass::getByCondition(['shopId'=>$currentShopId, 'deviceId'=>$allParams['deviceId']], true);
  1019. if ($device) {
  1020. $device->status = 1;
  1021. $device->save();
  1022. }
  1023. }
  1024. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  1025. util::success([
  1026. 'token' => $token,
  1027. 'admin' => $admin,
  1028. 'shopAdminId' => $shopAdminId,
  1029. 'shopId' => $currentShopId,
  1030. 'switchShop' => $switchShop,
  1031. 'openShop' => $openShop,
  1032. 'showDemo' => $showDemo,
  1033. //有小程序新版本提示更新
  1034. 'update' => 0,
  1035. 'skCustomId' => $skCustomId,
  1036. 'apiHost' => $apiHost,
  1037. 'imgUploadApi' => $imgUploadApi,
  1038. 'showBook' => $showBook,
  1039. 'hasHitNavigate' => $hasHitNavigate,
  1040. 'staff' => $shopAdmin,
  1041. 'lookAllShop' => $lookAllShop,
  1042. 'labelList' => $labelList,
  1043. ]);
  1044. }
  1045. }