AdminController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\admin\classes\AdminClass;
  4. use bizGhs\admin\services\AdminService;
  5. use biz\wx\services\WxOpenService;
  6. use common\components\util;
  7. use Yii;
  8. use yii\helpers\Json;
  9. use biz\user\classes\UserClass;
  10. class AdminController extends BaseController
  11. {
  12. public $withoutLogin = [];
  13. //获取登陆员工的权限 shish 2019.11.24
  14. public function actionLoginAuth()
  15. {
  16. $auth = AdminService::getAuth($this->adminId, $this->ghsId);
  17. util::success($auth);
  18. }
  19. //添加员工 shish 2019.12.8
  20. public function actionAdd()
  21. {
  22. $post = Yii::$app->request->post();
  23. $post['merchantId'] = $this->ghsId;
  24. $admin = AdminService::addAdmin($post);
  25. $id = $admin['id'];
  26. $info = AdminService::getDetail($id);
  27. util::success($info);
  28. }
  29. //删除员工 shish 2019.12.9
  30. public function actionDelete()
  31. {
  32. Yii::$app->end();
  33. $id = Yii::$app->request->post('id');
  34. $admin = AdminClass::getById($id);
  35. AdminService::valid($admin, $this->ghsId);
  36. AdminService::deleteAdmin($admin, $this->adminId);
  37. util::complete();
  38. }
  39. //查看员工详情 shish 2019.12.9
  40. public function actionDetail()
  41. {
  42. $id = Yii::$app->request->get('id');
  43. $admin = AdminService::getDetail($id);
  44. AdminService::valid($admin, $this->ghsId);
  45. util::success($admin);
  46. }
  47. //密码修改
  48. public function actionPassword()
  49. {
  50. $post = Yii::$app->request->post();
  51. $old = isset($post['old']) ? $post['old'] : '';
  52. $new = isset($post['new']) ? $post['new'] : '';
  53. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  54. if (empty($new)) {
  55. util::fail('请输入新密码');
  56. }
  57. if ($new != $confirm) {
  58. util::fail('二次密码不一致');
  59. }
  60. $admin = $this->admin;
  61. $adminId = $this->adminId;
  62. if (!empty($admin['password'])) {
  63. if (empty($old)) {
  64. util::fail('请填写旧密码');
  65. }
  66. if (password_verify($old, $admin['password']) == false) {
  67. util::fail('旧密码错误');
  68. }
  69. }
  70. AdminService::updateById($adminId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  71. util::complete('修改成功');
  72. }
  73. //确认密码修改
  74. public function actionConfirmPassword()
  75. {
  76. $post = Yii::$app->request->post();
  77. $old = isset($post['old']) ? $post['old'] : '';
  78. $new = isset($post['new']) ? $post['new'] : '';
  79. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  80. if (empty($new)) {
  81. util::fail('请输入新密码');
  82. }
  83. if ($new != $confirm) {
  84. util::fail('二次密码不一致');
  85. }
  86. $admin = $this->admin;
  87. $adminId = $this->adminId;
  88. if (!empty($admin['confirmPassword'])) {
  89. if (empty($old)) {
  90. util::fail('请填写旧密码');
  91. }
  92. if (password_verify($old, $admin['confirmPassword']) == false) {
  93. util::fail('旧密码错误');
  94. }
  95. }
  96. AdminService::updateById($adminId, ['confirmPassword' => password_hash($new, PASSWORD_BCRYPT)]);
  97. util::complete('修改成功');
  98. }
  99. //登陆员工的详情 shish 2019.12.26
  100. public function actionLoginDetail()
  101. {
  102. $detail = AdminService::getDetail($this->adminId);
  103. util::success($detail);
  104. }
  105. //小程序用户完整信息 shish 2019.12.12
  106. public function actionMiniFullInfo()
  107. {
  108. $post = Yii::$app->request->post();
  109. $iv = isset($post['iv']) ? $post['iv'] : '';
  110. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  111. $merchant = WxOpenService::getWxInfo();
  112. $appId = $merchant['miniAppId'];
  113. $admin = $this->admin;
  114. $miniOpenId = $admin['miniOpenId'];
  115. if (empty($miniOpenId)) {
  116. util::fail('没有找到管理员信息(miniOpenId)');
  117. }
  118. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  119. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  120. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  121. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  122. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  123. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  124. if ($errCode != 0) {
  125. Yii::info($result . ' ' . $errCode);
  126. util::fail('获取用户信息失败');
  127. }
  128. Yii::info('小程序获取用户信息:' . $result);
  129. $originalInfo = Json::decode($result);
  130. $source = UserClass::$userSourceId['mini']['name'];
  131. $admin = AdminService::replaceAdmin($originalInfo, $source);
  132. $adminId = $admin['id'];
  133. $admin = AdminService::getAdminById($adminId);
  134. util::success($admin);
  135. }
  136. //小程序用户手机号
  137. public function actionMiniMobile()
  138. {
  139. $post = Yii::$app->request->post();
  140. $iv = $post['iv'];
  141. $encryptedData = $post['encryptedData'];
  142. $merchant = WxOpenService::getWxInfo();
  143. $appId = $merchant['miniAppId'];
  144. $admin = $this->admin;
  145. $miniOpenId = $admin['miniOpenId'];
  146. if (empty($miniOpenId)) {
  147. util::fail('没有找到管理员信息(miniOpenId)');
  148. }
  149. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  150. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  151. if (empty($sessionKey)) {
  152. util::fail('sesstion key empty');
  153. }
  154. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  155. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  156. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  157. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  158. if ($errCode != 0) {
  159. util::fail('解密失败');
  160. }
  161. $arr = Json::decode($result);
  162. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  163. util::success(['mobile' => $mobile]);
  164. }
  165. }