AdminController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. util::fail('系统升级中,稍后再试');
  153. }
  154. $apiHost = Yii::$app->params['hdHost'];
  155. $imgUploadApi = $apiHost . '/upload/save-file';
  156. //惠雅鲜花员工不能看收入情况
  157. if (in_array($adminId, [2366, 2812, 4004])) {
  158. $shopAdmin->super = 0;
  159. }
  160. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  161. util::success([
  162. 'token' => '',
  163. 'admin' => $admin,
  164. 'shopId' => $currentShopId,
  165. 'shopAdminId' => $shopAdminId,
  166. 'switchShop' => $switchShop,
  167. 'pfShopId' => $pfShopId,
  168. 'onlyCg' => $onlyCg,
  169. 'shareLogo' => $shareLogo,
  170. 'showDemo' => $showDemo,
  171. 'imgUploadApi' => $imgUploadApi,
  172. //有小程序新版本提示更新
  173. 'update' => $remind,
  174. 'staff' => $shopAdmin,
  175. ]);
  176. }
  177. //最近店铺 ssh 2021.2.27
  178. public function actionRecentShop()
  179. {
  180. util::success([]);
  181. }
  182. //获取登陆员工的权限 ssh 2019.11.24
  183. public function actionLoginAuth()
  184. {
  185. $auth = AdminService::getAuth($this->adminId, $this->sjId);
  186. util::success($auth);
  187. }
  188. //添加员工 ssh 2019.12.8
  189. public function actionAdd()
  190. {
  191. $post = Yii::$app->request->post();
  192. $post['sjId'] = $this->sjId;
  193. $admin = AdminService::addAdmin($post);
  194. $id = $admin['id'];
  195. $info = AdminService::getDetail($id);
  196. util::success($info);
  197. }
  198. //删除员工 ssh 2019.12.9
  199. public function actionDelete()
  200. {
  201. $id = Yii::$app->request->post('id');
  202. $admin = AdminClass::getById($id);
  203. AdminService::valid($admin, $this->sjId);
  204. AdminService::deleteAdmin($admin, $this->adminId);
  205. util::complete();
  206. }
  207. //修改员工 ssh 2019.12.9
  208. public function actionUpdate()
  209. {
  210. $post = Yii::$app->request->post();
  211. $id = isset($post['id']) && is_numeric($post['id']) ? $post['id'] : 0;
  212. unset($post['id']);
  213. $originalAdmin = AdminService::getById($id);
  214. $post['sjId'] = $this->sjId;
  215. AdminService::updateAdmin($originalAdmin, $post);
  216. util::complete('修改成功');
  217. }
  218. //查看员工详情 ssh 2019.12.9
  219. public function actionDetail()
  220. {
  221. $id = Yii::$app->request->get('id');
  222. $admin = AdminService::getDetail($id);
  223. AdminService::valid($admin, $this->sjId);
  224. util::success($admin);
  225. }
  226. //密码修改
  227. public function actionPassword()
  228. {
  229. $post = Yii::$app->request->post();
  230. $old = isset($post['old']) ? $post['old'] : '';
  231. $new = isset($post['new']) ? $post['new'] : '';
  232. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  233. if (empty($new)) {
  234. util::fail('请输入新密码');
  235. }
  236. if ($new != $confirm) {
  237. util::fail('二次密码不一致');
  238. }
  239. $admin = $this->admin->attributes;
  240. $adminId = $this->adminId;
  241. if (!empty($admin['password'])) {
  242. if (empty($old)) {
  243. util::fail('请填写旧密码');
  244. }
  245. if (password_verify($old, $admin['password']) == false) {
  246. util::fail('旧密码错误');
  247. }
  248. }
  249. AdminService::updateById($adminId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  250. util::complete('修改成功');
  251. }
  252. //确认密码修改
  253. public function actionConfirmPassword()
  254. {
  255. $post = Yii::$app->request->post();
  256. $old = isset($post['old']) ? $post['old'] : '';
  257. $new = isset($post['new']) ? $post['new'] : '';
  258. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  259. if (empty($new)) {
  260. util::fail('请输入新密码');
  261. }
  262. if ($new != $confirm) {
  263. util::fail('二次密码不一致');
  264. }
  265. $admin = $this->admin->attributes;
  266. $adminId = $this->adminId;
  267. if (!empty($admin['payPassword'])) {
  268. if (empty($old)) {
  269. util::fail('请填写旧密码');
  270. }
  271. if (password_verify($old, $admin['payPassword']) == false) {
  272. util::fail('旧密码错误');
  273. }
  274. }
  275. AdminService::updateById($adminId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT)]);
  276. util::complete('修改成功');
  277. }
  278. //登陆员工的详情 ssh 2019.12.26
  279. public function actionLoginDetail()
  280. {
  281. $detail = AdminService::getDetail($this->adminId);
  282. util::success($detail);
  283. }
  284. //绑定帐号自动登录 ssh 20210121
  285. public function actionBindAutoLogin()
  286. {
  287. $post = Yii::$app->request->post();
  288. $iv = isset($post['iv']) ? $post['iv'] : '';
  289. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  290. $merchant = WxOpenService::getWxInfo();
  291. $appId = $merchant['miniAppId'];
  292. $miniOpenId = $post['miniOpenId'];
  293. if (empty($miniOpenId)) {
  294. util::fail('绑定失败');
  295. }
  296. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  297. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  298. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  299. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  300. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  301. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  302. if ($errCode != 0) {
  303. Yii::info($result . ' ' . $errCode);
  304. util::fail('绑定失败了');
  305. }
  306. Yii::info('小程序获取用户信息:' . $result);
  307. $wxInfo = Json::decode($result);
  308. $nickName = $wxInfo['nickName'] ?? '';
  309. if (empty($nickName)) {
  310. util::fail('绑定失败');
  311. }
  312. $admin = $this->admin ?? [];
  313. if (empty($admin)) {
  314. util::fail('绑定失败');
  315. }
  316. $admin->miniOpenId = $miniOpenId;
  317. $admin->save();
  318. util::complete();
  319. }
  320. //小程序用户完整信息 ssh 2019.12.12
  321. public function actionMiniFullInfo()
  322. {
  323. $post = Yii::$app->request->post();
  324. $iv = isset($post['iv']) ? $post['iv'] : '';
  325. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  326. $merchant = WxOpenService::getWxInfo();
  327. $appId = $merchant['miniAppId'];
  328. $admin = $this->admin->attributes;
  329. $miniOpenId = $admin['miniOpenId'];
  330. if (empty($miniOpenId)) {
  331. util::fail('没有找到管理员信息(miniOpenId)');
  332. }
  333. $unionId = $admin['unionId'] ?? '';
  334. if (empty($unionId)) {
  335. util::fail('没有找到管理员信息(unionId)');
  336. }
  337. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  338. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  339. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  340. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  341. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  342. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  343. if ($errCode != 0) {
  344. Yii::info($result . ' ' . $errCode);
  345. util::fail('获取用户信息失败');
  346. }
  347. Yii::info('小程序获取用户信息:' . $result);
  348. $wxInfo = Json::decode($result);
  349. $source = UserClass::$userSourceId['mini']['name'];
  350. $wxInfo['miniOpenId'] = $admin['miniOpenId'] ?? '';
  351. //这个接口没有提供unionId,这边补充一下
  352. $wxInfo['unionId'] = $unionId;
  353. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($wxInfo, $source);
  354. $adminId = $admin['id'];
  355. $admin = AdminService::getAdminById($adminId);
  356. util::success($admin);
  357. }
  358. //小程序用户手机号
  359. public function actionMiniMobile()
  360. {
  361. $post = Yii::$app->request->post();
  362. $iv = $post['iv'];
  363. $encryptedData = $post['encryptedData'];
  364. $merchant = WxOpenService::getWxInfo();
  365. $appId = $merchant['miniAppId'];
  366. $admin = $this->admin->attributes;
  367. $miniOpenId = $admin['miniOpenId'];
  368. if (empty($miniOpenId)) {
  369. util::fail('没有找到管理员信息(miniOpenId)');
  370. }
  371. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  372. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  373. if (empty($sessionKey)) {
  374. util::fail('sesstion key empty');
  375. }
  376. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  377. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  378. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  379. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  380. if ($errCode != 0) {
  381. util::fail('解密失败');
  382. }
  383. $arr = Json::decode($result);
  384. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  385. $originalMobile = $admin['mobile'] ?? '';
  386. if (empty($originalMobile)) {
  387. \biz\admin\classes\AdminClass::updateById($this->adminId, ['mobile' => $mobile]);
  388. }
  389. util::success(['mobile' => $mobile]);
  390. }
  391. //设置支付密码 ssh 2021.1.24
  392. public function actionSetPayPassword()
  393. {
  394. $get = Yii::$app->request->get();
  395. $adminId = $this->adminId;
  396. $info = AdminClass::getAdminById($adminId);
  397. if (isset($info['payPassword']) && !empty($info['payPassword'])) {
  398. util::fail('您已经设置了支付密码');
  399. }
  400. $password = $get['password'] ?? '';
  401. if (empty($password)) {
  402. util::fail('请输入支付密码');
  403. }
  404. $string = password_hash($password, PASSWORD_BCRYPT);
  405. AdminClass::updateById($adminId, ['payPassword' => $string]);
  406. $data = ['payPassword' => $string];
  407. util::success($data);
  408. }
  409. //修改或保存 clientId
  410. public function actionReplaceClientId()
  411. {
  412. $get = Yii::$app->request->get();
  413. $clientId = $get['id'] ?? '';
  414. $deviceId = $get['deviceId'] ?? '';
  415. $autoLogin = $get['auto'] ?? 0; // 是否自动登录的请求
  416. if (empty($clientId)) {
  417. util::success(['returnStatus' => 'FAILURE']);
  418. }
  419. $hdDevice = HdDeviceClass::getByCondition(['shopId'=>$this->shopId, 'deviceId'=>$deviceId], true);
  420. if ($hdDevice == null) {
  421. $device = [
  422. 'deviceId' => $deviceId,
  423. 'clientId' => $clientId,
  424. 'mainId' => $this->mainId,
  425. 'shopId' => $this->shopId,
  426. 'adminId' => $this->adminId,
  427. 'status' => 1
  428. ];
  429. if ($autoLogin == 1) {
  430. $device['autoLoginTime'] = date('Y-m-d H:i:s');
  431. } else {
  432. $device['pwdLoginTime'] = date('Y-m-d H:i:s');
  433. }
  434. HdDeviceClass::add($device);
  435. } else {
  436. if ($autoLogin == 1) {
  437. $hdDevice->autoLoginTime = date('Y-m-d H:i:s');
  438. } else {
  439. $hdDevice->pwdLoginTime = date('Y-m-d H:i:s');
  440. }
  441. $hdDevice->clientId = $clientId;
  442. $hdDevice->mainId = $this->mainId;
  443. $hdDevice->shopId = $this->shopId;
  444. $hdDevice->adminId = $this->adminId;
  445. $hdDevice->save();
  446. }
  447. //noticeUtil::push("保存花店零售客户端ID:" . $clientId, '15280215347');
  448. util::complete();
  449. }
  450. //修改密码 ssh 20220106
  451. public function actionModifyPassword()
  452. {
  453. $get = Yii::$app->request->get();
  454. $password = $get['password'] ?? '';
  455. if (empty($password)) {
  456. util::fail('请填写密码');
  457. }
  458. if (ctype_alnum($password)) {
  459. util::fail('密码必须是字母、数字和符号的组合');
  460. }
  461. $adminId = $this->adminId;
  462. $password = password_hash($password, PASSWORD_BCRYPT);
  463. AdminClass::updateById($adminId, ['password' => $password]);
  464. util::complete('操作成功');
  465. }
  466. }