AdminController.php 15 KB

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