AuthController.php 47 KB

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