AdminController.php 16 KB

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