AdminController.php 17 KB

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