AdminController.php 13 KB

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