AdminController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\admin\classes\AdminClass;
  4. use bizHd\admin\services\AdminService;
  5. use bizHd\wx\services\WxOpenService;
  6. use common\components\util;
  7. use Yii;
  8. use yii\helpers\Json;
  9. use bizHd\user\classes\UserClass;
  10. class AdminController extends BaseController
  11. {
  12. public $guestAccess = ['mini-full-info', 'mini-mobile', 'login-detail'];
  13. //最近店铺 ssh 2021.2.27
  14. public function actionRecentShop()
  15. {
  16. util::success([]);
  17. }
  18. //获取登陆员工的权限 ssh 2019.11.24
  19. public function actionLoginAuth()
  20. {
  21. $auth = AdminService::getAuth($this->adminId, $this->sjId);
  22. util::success($auth);
  23. }
  24. //添加员工 ssh 2019.12.8
  25. public function actionAdd()
  26. {
  27. $post = Yii::$app->request->post();
  28. $post['sjId'] = $this->sjId;
  29. $admin = AdminService::addAdmin($post);
  30. $id = $admin['id'];
  31. $info = AdminService::getDetail($id);
  32. util::success($info);
  33. }
  34. //删除员工 ssh 2019.12.9
  35. public function actionDelete()
  36. {
  37. $id = Yii::$app->request->post('id');
  38. $admin = AdminClass::getById($id);
  39. AdminService::valid($admin, $this->sjId);
  40. AdminService::deleteAdmin($admin, $this->adminId);
  41. util::complete();
  42. }
  43. //修改员工 ssh 2019.12.9
  44. public function actionUpdate()
  45. {
  46. $post = Yii::$app->request->post();
  47. $id = isset($post['id']) && is_numeric($post['id']) ? $post['id'] : 0;
  48. unset($post['id']);
  49. $originalAdmin = AdminService::getById($id);
  50. $post['sjId'] = $this->sjId;
  51. AdminService::updateAdmin($originalAdmin, $post);
  52. util::complete('修改成功');
  53. }
  54. //查看员工详情 ssh 2019.12.9
  55. public function actionDetail()
  56. {
  57. $id = Yii::$app->request->get('id');
  58. $admin = AdminService::getDetail($id);
  59. AdminService::valid($admin, $this->sjId);
  60. util::success($admin);
  61. }
  62. //密码修改
  63. public function actionPassword()
  64. {
  65. $post = Yii::$app->request->post();
  66. $old = isset($post['old']) ? $post['old'] : '';
  67. $new = isset($post['new']) ? $post['new'] : '';
  68. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  69. if (empty($new)) {
  70. util::fail('请输入新密码');
  71. }
  72. if ($new != $confirm) {
  73. util::fail('二次密码不一致');
  74. }
  75. $admin = $this->admin->attributes;
  76. $adminId = $this->adminId;
  77. if (!empty($admin['password'])) {
  78. if (empty($old)) {
  79. util::fail('请填写旧密码');
  80. }
  81. if (password_verify($old, $admin['password']) == false) {
  82. util::fail('旧密码错误');
  83. }
  84. }
  85. AdminService::updateById($adminId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  86. util::complete('修改成功');
  87. }
  88. //确认密码修改
  89. public function actionConfirmPassword()
  90. {
  91. $post = Yii::$app->request->post();
  92. $old = isset($post['old']) ? $post['old'] : '';
  93. $new = isset($post['new']) ? $post['new'] : '';
  94. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  95. if (empty($new)) {
  96. util::fail('请输入新密码');
  97. }
  98. if ($new != $confirm) {
  99. util::fail('二次密码不一致');
  100. }
  101. $admin = $this->admin->attributes;
  102. $adminId = $this->adminId;
  103. if (!empty($admin['payPassword'])) {
  104. if (empty($old)) {
  105. util::fail('请填写旧密码');
  106. }
  107. if (password_verify($old, $admin['payPassword']) == false) {
  108. util::fail('旧密码错误');
  109. }
  110. }
  111. AdminService::updateById($adminId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT)]);
  112. util::complete('修改成功');
  113. }
  114. //登陆员工的详情 ssh 2019.12.26
  115. public function actionLoginDetail()
  116. {
  117. $detail = AdminService::getDetail($this->adminId);
  118. util::success($detail);
  119. }
  120. //小程序用户完整信息 ssh 2019.12.12
  121. public function actionMiniFullInfo()
  122. {
  123. $post = Yii::$app->request->post();
  124. $iv = isset($post['iv']) ? $post['iv'] : '';
  125. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  126. $merchant = WxOpenService::getWxInfo();
  127. $appId = $merchant['miniAppId'];
  128. $admin = $this->admin->attributes;
  129. $miniOpenId = $admin['miniOpenId'];
  130. if (empty($miniOpenId)) {
  131. util::fail('没有找到管理员信息(miniOpenId)');
  132. }
  133. $unionId = $admin['unionId'] ?? '';
  134. if (empty($unionId)) {
  135. util::fail('没有找到管理员信息(unionId)');
  136. }
  137. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  138. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  139. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  140. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  141. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  142. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  143. if ($errCode != 0) {
  144. Yii::info($result . ' ' . $errCode);
  145. util::fail('获取用户信息失败');
  146. }
  147. Yii::info('小程序获取用户信息:' . $result);
  148. $wxInfo = Json::decode($result);
  149. $source = UserClass::$userSourceId['mini']['name'];
  150. $wxInfo['miniOpenId'] = $admin['miniOpenId'] ?? '';
  151. //这个接口没有提供unionId,这边补充一下
  152. $wxInfo['unionId'] = $unionId;
  153. $admin = AdminService::replaceAdmin($wxInfo, $source);
  154. $adminId = $admin['id'];
  155. $admin = AdminService::getAdminById($adminId);
  156. util::success($admin);
  157. }
  158. //小程序用户手机号
  159. public function actionMiniMobile()
  160. {
  161. $post = Yii::$app->request->post();
  162. $iv = $post['iv'];
  163. $encryptedData = $post['encryptedData'];
  164. $merchant = WxOpenService::getWxInfo();
  165. $appId = $merchant['miniAppId'];
  166. $admin = $this->admin->attributes;
  167. $miniOpenId = $admin['miniOpenId'];
  168. if (empty($miniOpenId)) {
  169. util::fail('没有找到管理员信息(miniOpenId)');
  170. }
  171. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  172. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  173. if (empty($sessionKey)) {
  174. util::fail('sesstion key empty');
  175. }
  176. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  177. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  178. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  179. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  180. if ($errCode != 0) {
  181. util::fail('解密失败');
  182. }
  183. $arr = Json::decode($result);
  184. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  185. $originalMobile = $admin['mobile'] ?? '';
  186. if (empty($originalMobile)) {
  187. \biz\admin\classes\AdminClass::updateById($this->adminId, ['mobile' => $mobile]);
  188. }
  189. util::success(['mobile' => $mobile]);
  190. }
  191. //设置支付密码 ssh 2021.1.24
  192. public function actionSetPayPassword()
  193. {
  194. $get = Yii::$app->request->get();
  195. $adminId = $this->adminId;
  196. $info = AdminClass::getAdminById($adminId);
  197. if (isset($info['payPassword']) && !empty($info['payPassword'])) {
  198. util::fail('您已经设置了支付密码');
  199. }
  200. $password = $get['password'] ?? '';
  201. if (empty($password)) {
  202. util::fail('请输入支付密码');
  203. }
  204. $string = password_hash($password, PASSWORD_BCRYPT);
  205. AdminClass::updateById($adminId, ['payPassword' => $string]);
  206. $data = ['payPassword' => $string];
  207. util::success($data);
  208. }
  209. }