AdminController.php 13 KB

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