AdminController.php 16 KB

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