AuthController.php 30 KB

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