AdminController.php 13 KB

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