AdminController.php 13 KB

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