AuthController.php 31 KB

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