AuthController.php 31 KB

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