AdminController.php 12 KB

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