AdminController.php 16 KB

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