AdminController.php 13 KB

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