AuthController.php 33 KB

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