AdminController.php 13 KB

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