AdminController.php 12 KB

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