AdminController.php 14 KB

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