AdminController.php 13 KB

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