AdminController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\shop\services\ShopAdminService;
  5. use bizHd\admin\classes\AdminClass;
  6. use bizHd\admin\classes\ShopAdminClass;
  7. use bizHd\admin\services\AdminService;
  8. use bizHd\staff\classes\StaffClass;
  9. use bizHd\wx\services\WxOpenService;
  10. use common\components\imgUtil;
  11. use common\components\noticeUtil;
  12. use common\components\util;
  13. use Yii;
  14. use yii\helpers\Json;
  15. use bizHd\user\classes\UserClass;
  16. use \bizHd\device\classes\HdDeviceClass;
  17. class AdminController extends BaseController
  18. {
  19. public $guestAccess = ['mini-full-info', 'mini-mobile', 'login-detail'];
  20. //已告之有多个供货商
  21. public function actionHasNotice()
  22. {
  23. $admin = $this->admin;
  24. $admin->noticeHasMoreGhs = 1;
  25. $admin->save();
  26. util::complete();
  27. }
  28. //清空登录信息 ssh 20240428
  29. public function actionClearLogin()
  30. {
  31. $deviceId = Yii::$app->request->post('deviceId', '');
  32. $admin = $this->admin;
  33. $admin->miniOpenId = '';
  34. $admin->ghsMiniOpenId = '';
  35. $admin->save();
  36. //设备登录状态更新
  37. if ($deviceId != '') {
  38. $device = HdDeviceClass::getByCondition(['shopId' => $this->shopId, 'deviceId' => $deviceId], true);
  39. if ($device) {
  40. $device->status = 0;//退出
  41. $device->save();
  42. }
  43. }
  44. util::complete();
  45. }
  46. //我已离职(从某门店离职)
  47. public function actionDelStaff()
  48. {
  49. $admin = $this->admin;
  50. $staff = $this->shopAdmin;
  51. if ($staff->founder == 2) {
  52. util::fail('老板不能离职');
  53. }
  54. if ($staff->delStatus == 1) {
  55. util::fail('您已离职');
  56. }
  57. $adminId = $this->adminId;
  58. $shop = $this->shop;
  59. if ($shop->adminId == $adminId) {
  60. util::fail('老板不能离职哦');
  61. }
  62. // 使用事务保证数据一致性
  63. $connection = Yii::$app->db;
  64. $transaction = $connection->beginTransaction();
  65. try {
  66. // 执行离职操作,多处使用,关键词 staff_lz
  67. $respond = StaffClass::executeStaffResignation($admin, $staff);
  68. $transaction->commit();
  69. util::success($respond);
  70. } catch (\Exception $exception) {
  71. $transaction->rollBack();
  72. Yii::error("员工离职失败:" . $exception->getMessage());
  73. util::fail('离职操作失败');
  74. }
  75. }
  76. //个人信息 ssh 20230505
  77. public function actionInfo()
  78. {
  79. $admin = $this->admin;
  80. util::success(['info' => $admin]);
  81. }
  82. //修改个人信息 ssh 20230505
  83. public function actionUpdateAdmin()
  84. {
  85. $post = Yii::$app->request->post();
  86. $openId = $post['openId'] ?? '';
  87. $name = $post['name'] ?? '';
  88. $admin = $this->admin;
  89. $admin->openId = $openId;
  90. if (!empty($name)) {
  91. $admin->name = $name;
  92. }
  93. $admin->save();
  94. if (!empty($name)) {
  95. $adminId = $this->adminId;
  96. ShopAdminClass::updateByCondition(['adminId' => $adminId], ['name' => $name]);
  97. }
  98. util::complete();
  99. }
  100. //修改miniOpenId ssh 20220912
  101. public function actionUpdateMiniOpenId()
  102. {
  103. $admin = $this->admin;
  104. $miniOpenId = Yii::$app->request->get('miniOpenId');
  105. $admin->miniOpenId = $miniOpenId;
  106. $admin->save();
  107. noticeUtil::push("已经重新获取并更新了openId", '15280215347');
  108. util::complete();
  109. }
  110. public function actionHasTokenAutoLogin()
  111. {
  112. $admin = $this->admin;
  113. if (empty($admin)) {
  114. util::fail('请先注册');
  115. }
  116. $openShop = $admin['openShop'] ?? 1;
  117. $currentShopId = $admin['currentShopId'] ?? 0;
  118. if (empty($currentShopId)) {
  119. if ($openShop == 2) {
  120. util::fail('审核中');
  121. }
  122. util::fail('请先注册');
  123. }
  124. $currentShop = ShopClass::getById($currentShopId, true);
  125. if (empty($currentShop)) {
  126. util::fail('没有找到门店34');
  127. }
  128. $pfShopId = $currentShop->pfShopId ?? 0;
  129. $onlyCg = $currentShop->onlyCg ?? 1;
  130. $shareLogo = imgUtil::groupImg('buy_logo.jpg');
  131. if (isset($currentShop->shareLogo) && !empty($currentShop->shareLogo)) {
  132. $shareLogo = imgUtil::groupImg($currentShop->shareLogo);
  133. }
  134. $mainId = $currentShop->mainId ?? 0;
  135. $adminId = $admin['id'];
  136. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'mainId' => $mainId, 'delStatus' => 0], true);
  137. if (empty($shopAdmin)) {
  138. util::fail('无法操作');
  139. }
  140. if (!empty($shopAdmin) && $shopAdmin->status == 0) {
  141. util::fail("您的账号已被冻结");
  142. }
  143. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  144. $shopAdmin->save();
  145. $shopAdminId = $shopAdmin->id ?? 0;
  146. //是否有切换门店的权限
  147. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  148. $showDemo = 1;
  149. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  150. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  151. if ($hdUpgrading == 1) {
  152. $allowShopAdminIdsStr = getenv('HD_UPGRADE_ALLOW_SHOP_ADMIN_IDS') ?: '';
  153. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  154. if (!in_array($shopAdminId, $allowShopAdminIds)) {
  155. util::fail('系统升级中,稍后再试');
  156. }
  157. }
  158. $apiHost = Yii::$app->params['hdHost'];
  159. $imgUploadApi = $apiHost . '/upload/save-file';
  160. //惠雅鲜花员工不能看收入情况
  161. if (in_array($adminId, [2366, 2812, 4004])) {
  162. $shopAdmin->super = 0;
  163. }
  164. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  165. util::success([
  166. 'token' => '',
  167. 'admin' => $admin,
  168. 'shopId' => $currentShopId,
  169. 'shopAdminId' => $shopAdminId,
  170. 'switchShop' => $switchShop,
  171. 'pfShopId' => $pfShopId,
  172. 'onlyCg' => $onlyCg,
  173. 'shareLogo' => $shareLogo,
  174. 'showDemo' => $showDemo,
  175. 'imgUploadApi' => $imgUploadApi,
  176. //有小程序新版本提示更新
  177. 'update' => $remind,
  178. 'staff' => $shopAdmin,
  179. ]);
  180. }
  181. //最近店铺 ssh 2021.2.27
  182. public function actionRecentShop()
  183. {
  184. util::success([]);
  185. }
  186. //获取登陆员工的权限 ssh 2019.11.24
  187. public function actionLoginAuth()
  188. {
  189. $auth = AdminService::getAuth($this->adminId, $this->sjId);
  190. util::success($auth);
  191. }
  192. //添加员工 ssh 2019.12.8
  193. public function actionAdd()
  194. {
  195. $post = Yii::$app->request->post();
  196. $post['sjId'] = $this->sjId;
  197. $admin = AdminService::addAdmin($post);
  198. $id = $admin['id'];
  199. $info = AdminService::getDetail($id);
  200. util::success($info);
  201. }
  202. //删除员工 ssh 2019.12.9
  203. public function actionDelete()
  204. {
  205. $id = Yii::$app->request->post('id');
  206. $admin = AdminClass::getById($id);
  207. AdminService::valid($admin, $this->sjId);
  208. AdminService::deleteAdmin($admin, $this->adminId);
  209. util::complete();
  210. }
  211. //修改员工 ssh 2019.12.9
  212. public function actionUpdate()
  213. {
  214. $post = Yii::$app->request->post();
  215. $id = isset($post['id']) && is_numeric($post['id']) ? $post['id'] : 0;
  216. unset($post['id']);
  217. $originalAdmin = AdminService::getById($id);
  218. $post['sjId'] = $this->sjId;
  219. AdminService::updateAdmin($originalAdmin, $post);
  220. util::complete('修改成功');
  221. }
  222. //查看员工详情 ssh 2019.12.9
  223. public function actionDetail()
  224. {
  225. $id = Yii::$app->request->get('id');
  226. $admin = AdminService::getDetail($id);
  227. AdminService::valid($admin, $this->sjId);
  228. util::success($admin);
  229. }
  230. //密码修改
  231. public function actionPassword()
  232. {
  233. $post = Yii::$app->request->post();
  234. $old = isset($post['old']) ? $post['old'] : '';
  235. $new = isset($post['new']) ? $post['new'] : '';
  236. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  237. if (empty($new)) {
  238. util::fail('请输入新密码');
  239. }
  240. if ($new != $confirm) {
  241. util::fail('二次密码不一致');
  242. }
  243. $admin = $this->admin->attributes;
  244. $adminId = $this->adminId;
  245. if (!empty($admin['password'])) {
  246. if (empty($old)) {
  247. util::fail('请填写旧密码');
  248. }
  249. if (password_verify($old, $admin['password']) == false) {
  250. util::fail('旧密码错误');
  251. }
  252. }
  253. AdminService::updateById($adminId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  254. util::complete('修改成功');
  255. }
  256. //确认密码修改
  257. public function actionConfirmPassword()
  258. {
  259. $post = Yii::$app->request->post();
  260. $old = isset($post['old']) ? $post['old'] : '';
  261. $new = isset($post['new']) ? $post['new'] : '';
  262. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  263. if (empty($new)) {
  264. util::fail('请输入新密码');
  265. }
  266. if ($new != $confirm) {
  267. util::fail('二次密码不一致');
  268. }
  269. $admin = $this->admin->attributes;
  270. $adminId = $this->adminId;
  271. if (!empty($admin['payPassword'])) {
  272. if (empty($old)) {
  273. util::fail('请填写旧密码');
  274. }
  275. if (password_verify($old, $admin['payPassword']) == false) {
  276. util::fail('旧密码错误');
  277. }
  278. }
  279. AdminService::updateById($adminId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT)]);
  280. util::complete('修改成功');
  281. }
  282. //登陆员工的详情 ssh 2019.12.26
  283. public function actionLoginDetail()
  284. {
  285. $detail = AdminService::getDetail($this->adminId);
  286. util::success($detail);
  287. }
  288. //绑定帐号自动登录 ssh 20210121
  289. public function actionBindAutoLogin()
  290. {
  291. $post = Yii::$app->request->post();
  292. $iv = isset($post['iv']) ? $post['iv'] : '';
  293. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  294. $merchant = WxOpenService::getWxInfo();
  295. $appId = $merchant['miniAppId'];
  296. $miniOpenId = $post['miniOpenId'];
  297. if (empty($miniOpenId)) {
  298. util::fail('绑定失败');
  299. }
  300. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  301. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  302. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  303. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  304. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  305. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  306. if ($errCode != 0) {
  307. Yii::info($result . ' ' . $errCode);
  308. util::fail('绑定失败了');
  309. }
  310. Yii::info('小程序获取用户信息:' . $result);
  311. $wxInfo = Json::decode($result);
  312. $nickName = $wxInfo['nickName'] ?? '';
  313. if (empty($nickName)) {
  314. util::fail('绑定失败');
  315. }
  316. $admin = $this->admin ?? [];
  317. if (empty($admin)) {
  318. util::fail('绑定失败');
  319. }
  320. $admin->miniOpenId = $miniOpenId;
  321. $admin->save();
  322. util::complete();
  323. }
  324. //小程序用户完整信息 ssh 2019.12.12
  325. public function actionMiniFullInfo()
  326. {
  327. $post = Yii::$app->request->post();
  328. $iv = isset($post['iv']) ? $post['iv'] : '';
  329. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  330. $merchant = WxOpenService::getWxInfo();
  331. $appId = $merchant['miniAppId'];
  332. $admin = $this->admin->attributes;
  333. $miniOpenId = $admin['miniOpenId'];
  334. if (empty($miniOpenId)) {
  335. util::fail('没有找到管理员信息(miniOpenId)');
  336. }
  337. $unionId = $admin['unionId'] ?? '';
  338. if (empty($unionId)) {
  339. util::fail('没有找到管理员信息(unionId)');
  340. }
  341. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  342. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  343. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  344. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  345. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  346. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  347. if ($errCode != 0) {
  348. Yii::info($result . ' ' . $errCode);
  349. util::fail('获取用户信息失败');
  350. }
  351. Yii::info('小程序获取用户信息:' . $result);
  352. $wxInfo = Json::decode($result);
  353. $source = UserClass::$userSourceId['mini']['name'];
  354. $wxInfo['miniOpenId'] = $admin['miniOpenId'] ?? '';
  355. //这个接口没有提供unionId,这边补充一下
  356. $wxInfo['unionId'] = $unionId;
  357. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($wxInfo, $source);
  358. $adminId = $admin['id'];
  359. $admin = AdminService::getAdminById($adminId);
  360. util::success($admin);
  361. }
  362. //小程序用户手机号
  363. public function actionMiniMobile()
  364. {
  365. $post = Yii::$app->request->post();
  366. $iv = $post['iv'];
  367. $encryptedData = $post['encryptedData'];
  368. $merchant = WxOpenService::getWxInfo();
  369. $appId = $merchant['miniAppId'];
  370. $admin = $this->admin->attributes;
  371. $miniOpenId = $admin['miniOpenId'];
  372. if (empty($miniOpenId)) {
  373. util::fail('没有找到管理员信息(miniOpenId)');
  374. }
  375. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  376. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  377. if (empty($sessionKey)) {
  378. util::fail('sesstion key empty');
  379. }
  380. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  381. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  382. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  383. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  384. if ($errCode != 0) {
  385. util::fail('解密失败');
  386. }
  387. $arr = Json::decode($result);
  388. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  389. $originalMobile = $admin['mobile'] ?? '';
  390. if (empty($originalMobile)) {
  391. \biz\admin\classes\AdminClass::updateById($this->adminId, ['mobile' => $mobile]);
  392. }
  393. util::success(['mobile' => $mobile]);
  394. }
  395. //设置支付密码 ssh 2021.1.24
  396. public function actionSetPayPassword()
  397. {
  398. $get = Yii::$app->request->get();
  399. $adminId = $this->adminId;
  400. $info = AdminClass::getAdminById($adminId);
  401. if (isset($info['payPassword']) && !empty($info['payPassword'])) {
  402. util::fail('您已经设置了支付密码');
  403. }
  404. $password = $get['password'] ?? '';
  405. if (empty($password)) {
  406. util::fail('请输入支付密码');
  407. }
  408. $string = password_hash($password, PASSWORD_BCRYPT);
  409. AdminClass::updateById($adminId, ['payPassword' => $string]);
  410. $data = ['payPassword' => $string];
  411. util::success($data);
  412. }
  413. //修改或保存 clientId
  414. public function actionReplaceClientId()
  415. {
  416. $get = Yii::$app->request->get();
  417. $clientId = $get['id'] ?? '';
  418. $deviceId = $get['deviceId'] ?? '';
  419. $autoLogin = $get['auto'] ?? 0; // 是否自动登录的请求
  420. if (empty($clientId)) {
  421. util::success(['returnStatus' => 'FAILURE']);
  422. }
  423. $hdDevice = HdDeviceClass::getByCondition(['shopId'=>$this->shopId, 'deviceId'=>$deviceId], true);
  424. if ($hdDevice == null) {
  425. $device = [
  426. 'deviceId' => $deviceId,
  427. 'clientId' => $clientId,
  428. 'mainId' => $this->mainId,
  429. 'shopId' => $this->shopId,
  430. 'adminId' => $this->adminId,
  431. 'status' => 1
  432. ];
  433. if ($autoLogin == 1) {
  434. $device['autoLoginTime'] = date('Y-m-d H:i:s');
  435. } else {
  436. $device['pwdLoginTime'] = date('Y-m-d H:i:s');
  437. }
  438. HdDeviceClass::add($device);
  439. } else {
  440. if ($autoLogin == 1) {
  441. $hdDevice->autoLoginTime = date('Y-m-d H:i:s');
  442. } else {
  443. $hdDevice->pwdLoginTime = date('Y-m-d H:i:s');
  444. }
  445. $hdDevice->clientId = $clientId;
  446. $hdDevice->mainId = $this->mainId;
  447. $hdDevice->shopId = $this->shopId;
  448. $hdDevice->adminId = $this->adminId;
  449. $hdDevice->save();
  450. }
  451. //noticeUtil::push("保存花店零售客户端ID:" . $clientId, '15280215347');
  452. util::complete();
  453. }
  454. //修改密码 ssh 20220106
  455. public function actionModifyPassword()
  456. {
  457. $get = Yii::$app->request->get();
  458. $password = $get['password'] ?? '';
  459. if (empty($password)) {
  460. util::fail('请填写密码');
  461. }
  462. if (ctype_alnum($password)) {
  463. util::fail('密码必须是字母、数字和符号的组合');
  464. }
  465. $adminId = $this->adminId;
  466. $password = password_hash($password, PASSWORD_BCRYPT);
  467. AdminClass::updateById($adminId, ['password' => $password]);
  468. util::complete('操作成功');
  469. }
  470. }