AuthController.php 31 KB

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