AdminController.php 14 KB

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