AuthController.php 33 KB

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