AdminController.php 14 KB

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