AuthController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. //充值送钱有没设置,没有设置则进行设置,临时方法,后面需要去掉,有多处,关键词recharge_sq_init ssh 20251229
  160. RechargeSqClass::initData($currentShop);
  161. //会员等级有没设置,没有设置则进行设置,临时方法,后面需要去掉,有多处,关键词member_level_init 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. //充值送钱有没设置,没有设置则进行设置,临时方法,后面需要去掉,有多处,关键词recharge_sq_init ssh 20251229
  253. RechargeSqClass::initData($currentShop);
  254. //会员等级有没设置,没有设置则进行设置,临时方法,后面需要去掉,有多处,关键词member_level_init ssh 20251229
  255. MemberLevelClass::initData($currentShop);
  256. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  257. util::success([
  258. 'token' => $token,
  259. 'admin' => $admin,
  260. 'shopId' => $currentShopId,
  261. 'shopAdminId' => $shopAdminId,
  262. 'switchShop' => $switchShop,
  263. 'pfShopId' => $pfShopId,
  264. 'onlyCg' => $onlyCg,
  265. 'shareLogo' => $shareLogo,
  266. 'imgUploadApi' => $imgUploadApi,
  267. 'showDemo' => $showDemo,
  268. //有小程序新版本提示更新
  269. 'update' => $remind,
  270. 'staff' => $shopAdmin,
  271. ]);
  272. }
  273. //本机号码一键登录 ssh 20210117
  274. public function actionPhoneLogin()
  275. {
  276. $get = Yii::$app->request->get();
  277. $access_token = $get['access_token'] ?? '';
  278. $openid = $get['openid'] ?? '';
  279. if (empty($access_token) || empty($openid)) {
  280. util::fail('参数错误');
  281. }
  282. //密钥,需要和uniCloud里的一致
  283. $secret = 'XHB2021198819640123';
  284. $params = ['access_token' => $access_token, 'openid' => $openid];
  285. $stringSignTemp = '';
  286. foreach ($params as $k => $v) {
  287. $stringSignTemp .= $k . '=' . $v . '&';
  288. }
  289. $stringSignTemp = rtrim($stringSignTemp, '&');
  290. $sign = hash_hmac('sha256', $stringSignTemp, $secret);
  291. $url = Yii::$app->params['hdUniCloudHost'] . "/getMobileNumber?access_token=" . $access_token . "&sign=" . $sign . "&" . $stringSignTemp;
  292. $curl = new curl\Curl();
  293. $respond = $curl->get($url);
  294. Yii::info($respond);
  295. $result = json_decode($respond, true);
  296. if (isset($result['code']) == false || $result['code'] != 1) {
  297. util::fail('获取手机号失败');
  298. }
  299. $phoneNumber = $result['data']['phoneNumber'] ?? '';
  300. if (empty($phoneNumber)) {
  301. util::fail('没有获取到手机号');
  302. }
  303. $admin = AdminService::getByCondition(['mobile' => $phoneNumber]);
  304. if (empty($admin)) {
  305. util::fail('请先注册');
  306. }
  307. $openShop = $admin['openShop'] ?? 1;
  308. $currentShopId = $admin['currentShopId'] ?? 0;
  309. if (empty($currentShopId)) {
  310. if ($openShop == 2) {
  311. util::fail('审核中');
  312. }
  313. util::fail('请先注册');
  314. }
  315. $currentShop = ShopClass::getById($currentShopId, true);
  316. if (empty($currentShop)) {
  317. util::fail('登录失败,没有找到门店38');
  318. }
  319. $pfShopId = $currentShop->pfShopId ?? 0;
  320. $onlyCg = $currentShop->onlyCg ?? 1;
  321. $shareLogo = imgUtil::groupImg('buy_logo.jpg');
  322. if (isset($currentShop->shareLogo) && !empty($currentShop->shareLogo)) {
  323. $shareLogo = imgUtil::groupImg($currentShop->shareLogo);
  324. }
  325. $adminId = $admin['id'];
  326. $mainId = $currentShop->mainId ?? 0;
  327. $shopAdmin = ShopAdminService::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  328. if (empty($shopAdmin)) {
  329. util::fail('无法操作');
  330. }
  331. if ($shopAdmin->status == 0) {
  332. util::fail("您的账号已被冻结");
  333. }
  334. if ($shopAdmin->delStatus == 1) {
  335. util::fail("您的账号已删除");
  336. }
  337. $token = jwt::getNewToken($adminId);
  338. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  339. $shopAdmin->save();
  340. $shopAdminId = $shopAdmin->id ?? 0;
  341. //是否有切换门店的权限
  342. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  343. $prefix = imgUtil::getPrefix();
  344. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  345. $admin['avatar'] = $avatar;
  346. $showDemo = 1;
  347. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  348. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  349. if ($hdUpgrading == 1) {
  350. util::fail('系统升级中,稍后再试');
  351. }
  352. $apiHost = Yii::$app->params['hdHost'];
  353. $imgUploadApi = $apiHost . '/upload/save-file';
  354. //惠雅鲜花员工不能看收入情况
  355. if (in_array($adminId, [2366, 2812, 4004])) {
  356. $shopAdmin->super = 0;
  357. }
  358. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  359. util::success([
  360. 'token' => $token,
  361. 'admin' => $admin,
  362. 'shopId' => $currentShopId,
  363. 'shopAdminId' => $shopAdminId,
  364. 'pfShopId' => $pfShopId,
  365. 'onlyCg' => $onlyCg,
  366. 'shareLogo' => $shareLogo,
  367. 'switchShop' => $switchShop,
  368. 'showDemo' => $showDemo,
  369. 'imgUploadApi' => $imgUploadApi,
  370. //有小程序新版本提示更新
  371. 'update' => $remind,
  372. 'staff' => $shopAdmin,
  373. ]);
  374. }
  375. //准备授权 ssh 2019.11.19
  376. public function actionPrepare()
  377. {
  378. $get = Yii::$app->request->get();
  379. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  380. if (empty($url)) {
  381. util::fail('没有网址');
  382. }
  383. $account = httpUtil::getAccount($url);
  384. if (empty($account)) {
  385. util::fail('没有商家帐号');
  386. }
  387. $merchant = MerchantService::getById($account);
  388. if (empty($merchant)) {
  389. util::fail('商家无效');
  390. }
  391. /**
  392. * authScope 参数的说明
  393. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  394. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  395. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  396. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  397. */
  398. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  399. $urlEncode = stringUtil::urlSafeB64Encode($url);
  400. //微信授权获取code ssh 2019.11.24
  401. $isOpen = 1;
  402. wxUtil::getCode($urlEncode, $merchant, $authScope, $isOpen);
  403. util::end();
  404. }
  405. //微信授权获取用户信息 ssh 2019.11.20
  406. public function actionGetUserInfo()
  407. {
  408. $get = Yii::$app->request->get();
  409. $code = isset($get['code']) ? $get['code'] : '';
  410. if (empty($code)) {
  411. util::fail('没有获取用户code');
  412. }
  413. if (isset($get['state']) && $get['state'] == 'authdeny') {
  414. util::fail('auth fail');
  415. }
  416. $urlEncode = $get['url'];
  417. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  418. $account = httpUtil::getAccount($url);
  419. if (empty($account)) {
  420. util::stop('商店ID无效!!');
  421. }
  422. $merchant = xhMerchantService::getByAccount($account);
  423. if (empty($merchant)) {
  424. util::stop('商店无效');
  425. }
  426. $sjId = $merchant['id'];
  427. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  428. if (empty($authScope)) {
  429. util::stop('没有授权类型');
  430. }
  431. $data = wxUtil::getOpenId($code, $merchant, 1);
  432. if ($data === false) {
  433. //微信授权获取code ssh 2019.11.24
  434. wxUtil::getCode($urlEncode, $merchant, $authScope, 1);
  435. util::end();
  436. }
  437. $openId = $data['openid'];
  438. $access_token = $data['access_token'];
  439. $user = UserService::getByOpenId($openId);
  440. //用户来源
  441. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  442. $source = $userSource['name'];
  443. if (empty($user)) {
  444. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  445. $originalInfo = [];
  446. $originalInfo['openId'] = $openId;
  447. $originalInfo['sjId'] = $sjId;
  448. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  449. if ($authScope == 'snsapi_userinfo') {
  450. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  451. $originalInfo = array_merge($baseInfo, $originalInfo);
  452. $originalInfo['isFull'] = 1;
  453. $originalInfo['unionId'] = $baseInfo['unionid'];
  454. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  455. $originalInfo['nickName'] = $baseInfo['nickName'];
  456. }
  457. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  458. } else {
  459. if ($user['isFull'] == 0) {
  460. if ($authScope == 'snsapi_userinfo') {
  461. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  462. $originalInfo = $baseInfo;
  463. $originalInfo['isFull'] = 1;
  464. $originalInfo['unionId'] = $baseInfo['unionid'];
  465. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  466. $originalInfo['openId'] = $baseInfo['openid'];
  467. $originalInfo['nickName'] = $baseInfo['nickName'];
  468. $originalInfo['sjId'] = $sjId;
  469. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  470. }
  471. }
  472. }
  473. //这个的基类没有设置统一的全局变量,这里进行设置一次
  474. $userId = $user['id'];
  475. $admin = AdminService::getByCondition(['userId' => $userId]);
  476. if (empty($admin)) {
  477. util::fail('您无法访问');
  478. }
  479. $adminId = $admin['id'];
  480. //获取token
  481. $token = jwt::getNewToken($adminId);
  482. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  483. $this->redirect($url);
  484. }
  485. //登陆并拿到token ssh 2019.11.23
  486. public function actionLogin()
  487. {
  488. $get = Yii::$app->request->get();
  489. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  490. if (stringUtil::isMobile($mobile) == false) {
  491. util::fail('请填写正常的手机号');
  492. }
  493. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  494. if (empty($password)) {
  495. util::fail('请输入密码');
  496. }
  497. $admin = AdminService::getByCondition(['mobile' => $mobile]);
  498. if (empty($admin)) {
  499. util::fail('请先注册哦。');
  500. }
  501. if (password_verify($password, $admin['password']) == false) {
  502. util::fail('密码错误');
  503. }
  504. $adminId = $admin['id'];
  505. $openShop = $admin['openShop'] ?? 1;
  506. $currentShopId = $admin['currentShopId'] ?? 0;
  507. if (empty($currentShopId)) {
  508. if ($openShop == 2) {
  509. util::fail('审核中');
  510. }
  511. util::fail('请先注册');
  512. }
  513. $currentShop = ShopClass::getById($currentShopId, true);
  514. if (empty($currentShop)) {
  515. util::fail('没有找到门店39');
  516. }
  517. $pfShopId = $currentShop->pfShopId ?? 0;
  518. if (!empty($pfShopId)) {
  519. //util::fail('批发店请不要在零售端登录');
  520. }
  521. $sjId = $currentShop->sjId ?? 0;
  522. $mainId = $currentShop->mainId ?? 0;
  523. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'mainId' => $mainId], true);
  524. if (empty($shopAdmin)) {
  525. util::fail('无法操作');
  526. }
  527. if ($shopAdmin->status == 0) {
  528. util::fail("您的账号已被冻结");
  529. }
  530. $token = jwt::getNewToken($adminId);
  531. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  532. $shopAdmin->save();
  533. //是否有切换门店的权限
  534. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  535. $prefix = imgUtil::getPrefix();
  536. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  537. $admin['avatar'] = $avatar;
  538. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  539. if ($hdUpgrading == 1) {
  540. util::fail('系统升级中,稍后再试');
  541. }
  542. $apiHost = Yii::$app->params['hdHost'];
  543. $imgUploadApi = $apiHost . '/upload/save-file';
  544. util::success(['token' => $token, 'account' => $sjId, 'shopId' => $currentShopId, 'imgUploadApi' => $imgUploadApi, 'switchShop' => $switchShop,]);
  545. }
  546. //静默获取小程序用户信息
  547. public function actionMiniInfo()
  548. {
  549. //花店平台
  550. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  551. $code = Yii::$app->request->get('code', '');
  552. $wxMiniBase = WxOpenService::getWxMiniBase();
  553. $appId = $wxMiniBase['miniAppId'];
  554. $appSecret = $wxMiniBase['miniAppSecret'];
  555. if (empty($appSecret)) {
  556. Yii::info('没有找到小程序的密钥');
  557. //没有管理店铺不允许登录
  558. util::success(['token' => '']);
  559. }
  560. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  561. $curl = new curl\Curl();
  562. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  563. $result = $curl->get($url);
  564. $arr = Json::decode($result);
  565. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  566. $openid = $arr['openid'] ?? '';
  567. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $openid;
  568. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  569. if (empty($openid)) {
  570. Yii::info(json_encode($arr));
  571. //没有管理店铺不允许登录
  572. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'sn' => 1]);
  573. }
  574. $admin = AdminService::getByCondition(['miniOpenId' => $openid]);
  575. if (empty($admin)) {
  576. //没有管理店铺不允许登录
  577. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'sn' => 2]);
  578. }
  579. $adminId = $admin['id'];
  580. //切换账号,多个地方要同步修改,关键词 change_account
  581. // if (getenv('YII_ENV') == 'production') {
  582. // if ($adminId == 4) {
  583. // $adminId = 28276;
  584. // $adminId = 27522;
  585. // $admin = AdminClass::getById($adminId);
  586. // }
  587. // } else {
  588. // if ($adminId == 919) {
  589. // $adminId = 1109;
  590. // $admin = AdminClass::getById($adminId);
  591. // }
  592. // }
  593. $currentShopId = $admin['currentShopId'] ?? 0;
  594. if (empty($currentShopId)) {
  595. //没有管理店铺不允许登录
  596. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'sn' => 3]);
  597. }
  598. $currentShop = ShopClass::getById($currentShopId, true);
  599. if (empty($currentShop)) {
  600. //没有管理店铺不允许登录
  601. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'sn' => 4]);
  602. }
  603. $mainId = $currentShop->mainId ?? 0;
  604. $pfShopId = $currentShop->pfShopId ?? 0;
  605. $onlyCg = $currentShop->onlyCg ?? 1;
  606. $shareLogo = imgUtil::groupImg('buy_logo.jpg');
  607. if (isset($currentShop->shareLogo) && !empty($currentShop->shareLogo)) {
  608. $shareLogo = imgUtil::groupImg($currentShop->shareLogo);
  609. }
  610. $shopAdmin = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  611. if (empty($shopAdmin)) {
  612. //没有管理店铺不允许登录
  613. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'sn' => 5]);
  614. }
  615. if (isset($shopAdmin->status) == false || $shopAdmin->status == 0) {
  616. //账号冻结不再自动登录
  617. util::success(['token' => '', 'currentMiniOpenId' => $openid, 'sn' => 6]);
  618. }
  619. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  620. $shopAdmin->save();
  621. $shopAdminId = $shopAdmin->id ?? 0;
  622. //是否有切换门店的权限
  623. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  624. $prefix = imgUtil::getPrefix();
  625. $avatar = isset($admin['avatar']) && !empty($admin['avatar']) ? $prefix . $admin['avatar'] : $prefix . '/retail/default-img.png';
  626. $admin['avatar'] = $avatar;
  627. $token = jwt::getNewToken($adminId);
  628. $showDemo = 1;
  629. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  630. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  631. if ($hdUpgrading == 1) {
  632. util::fail('系统升级中,稍后再试');
  633. }
  634. $apiHost = Yii::$app->params['hdHost'];
  635. $imgUploadApi = $apiHost . '/upload/save-file';
  636. //惠雅鲜花员工不能看收入情况
  637. if (in_array($adminId, [2366, 2812, 4004])) {
  638. $shopAdmin->super = 0;
  639. }
  640. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  641. util::success([
  642. 'token' => $token,
  643. 'admin' => $admin,
  644. 'shopId' => $currentShopId,
  645. 'shopAdminId' => $shopAdminId,
  646. 'switchShop' => $switchShop,
  647. 'pfShopId' => $pfShopId,
  648. 'onlyCg' => $onlyCg,
  649. 'shareLogo' => $shareLogo,
  650. 'showDemo' => $showDemo,
  651. 'imgUploadApi' => $imgUploadApi,
  652. //有小程序新版本提示更新
  653. 'update' => $remind,
  654. 'currentMiniOpenId' => $openid,
  655. 'staff' => $shopAdmin,
  656. ]);
  657. }
  658. //获取收款码的二维码图片 ssh 20210602
  659. public function actionGatheringImgUrl()
  660. {
  661. $id = Yii::$app->request->get('id', 0);
  662. $shop = ShopClass::getById($id, true);
  663. if (empty($shop)) {
  664. util::fail('没有找到门店40');
  665. }
  666. $sjId = $shop->sjId;
  667. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  668. $fileResource = file_get_contents($imgUrl);
  669. header('Content-type: image/jpeg');
  670. echo $fileResource;
  671. }
  672. //最新版本 ssh 20220211
  673. public function actionGetApkVersion()
  674. {
  675. $version = getenv('HD_NEW_APK_VERSION') == false ? '1.0.0' : getenv('HD_NEW_APK_VERSION');
  676. $mustUpdate = getenv('HD_NEW_APK_VERSION_MUST_UPDATE') == false ? 0 : getenv('HD_NEW_APK_VERSION_MUST_UPDATE');
  677. util::success(['version' => $version, 'mustUpdate' => $mustUpdate]);
  678. }
  679. //收银台apk更新 ssh 20220310
  680. public function actionGetPadApkVersion()
  681. {
  682. $version = getenv('HD_NEW_PAD_APK_VERSION') == false ? '1.0.0' : getenv('HD_NEW_PAD_APK_VERSION');
  683. $mustUpdate = getenv('HD_NEW_PAD_APK_VERSION_MUST_UPDATE') == false ? 0 : getenv('HD_NEW_PAD_APK_VERSION_MUST_UPDATE');
  684. util::success(['version' => $version, 'mustUpdate' => $mustUpdate]);
  685. }
  686. }