AdminController.php 12 KB

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