AuthController.php 40 KB

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