AuthController.php 32 KB

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