AdminController.php 12 KB

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