AuthController.php 31 KB

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