AdminController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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['merchantId'] = $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['merchantId'] = $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. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  134. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  135. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  136. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  137. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  138. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  139. if ($errCode != 0) {
  140. Yii::info($result . ' ' . $errCode);
  141. util::fail('获取用户信息失败');
  142. }
  143. Yii::info('小程序获取用户信息:' . $result);
  144. $wxInfo = Json::decode($result);
  145. $source = UserClass::$userSourceId['mini']['name'];
  146. $wxInfo['miniOpenId'] = $admin['miniOpenId'] ?? '';
  147. $admin = AdminService::replaceAdmin($wxInfo, $source);
  148. $adminId = $admin['id'];
  149. $admin = AdminService::getAdminById($adminId);
  150. util::success($admin);
  151. }
  152. //小程序用户手机号
  153. public function actionMiniMobile()
  154. {
  155. $post = Yii::$app->request->post();
  156. $iv = $post['iv'];
  157. $encryptedData = $post['encryptedData'];
  158. $merchant = WxOpenService::getWxInfo();
  159. $appId = $merchant['miniAppId'];
  160. $admin = $this->admin->attributes;
  161. $miniOpenId = $admin['miniOpenId'];
  162. if (empty($miniOpenId)) {
  163. util::fail('没有找到管理员信息(miniOpenId)');
  164. }
  165. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  166. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  167. if (empty($sessionKey)) {
  168. util::fail('sesstion key empty');
  169. }
  170. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  171. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  172. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  173. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  174. if ($errCode != 0) {
  175. util::fail('解密失败');
  176. }
  177. $arr = Json::decode($result);
  178. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  179. $originalMobile = $admin['mobile'] ?? '';
  180. if (empty($originalMobile)) {
  181. \biz\admin\classes\AdminClass::updateById($this->adminId, ['mobile' => $mobile]);
  182. }
  183. util::success(['mobile' => $mobile]);
  184. }
  185. //设置支付密码 ssh 2021.1.24
  186. public function actionSetPayPassword()
  187. {
  188. $get = Yii::$app->request->get();
  189. $adminId = $this->adminId;
  190. $info = AdminClass::getAdminById($adminId);
  191. if (isset($info['payPassword']) && !empty($info['payPassword'])) {
  192. util::fail('您已经设置了支付密码');
  193. }
  194. $password = $get['password'] ?? '';
  195. if (empty($password)) {
  196. util::fail('请输入支付密码');
  197. }
  198. $string = password_hash($password, PASSWORD_BCRYPT);
  199. AdminClass::updateById($adminId, ['payPassword' => $string]);
  200. $data = ['payPassword' => $string];
  201. util::success($data);
  202. }
  203. }