AdminController.php 16 KB

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