AuthController.php 34 KB

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