AdminController.php 11 KB

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