AdminController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $id = Yii::$app->request->post('id');
  33. $admin = AdminClass::getById($id);
  34. AdminService::valid($admin, $this->ghsId);
  35. AdminService::deleteAdmin($admin, $this->adminId);
  36. util::complete();
  37. }
  38. //修改员工 shish 2019.12.9
  39. public function actionUpdate()
  40. {
  41. $post = Yii::$app->request->post();
  42. $id = isset($post['id']) && is_numeric($post['id']) ? $post['id'] : 0;
  43. unset($post['id']);
  44. $originalAdmin = AdminService::getById($id);
  45. $post['merchantId'] = $this->ghsId;
  46. AdminService::updateAdmin($originalAdmin, $post);
  47. util::complete('修改成功');
  48. }
  49. //查看员工详情 shish 2019.12.9
  50. public function actionDetail()
  51. {
  52. $id = Yii::$app->request->get('id');
  53. $admin = AdminService::getDetail($id);
  54. AdminService::valid($admin, $this->ghsId);
  55. util::success($admin);
  56. }
  57. //密码修改
  58. public function actionPassword()
  59. {
  60. $post = Yii::$app->request->post();
  61. $old = isset($post['old']) ? $post['old'] : '';
  62. $new = isset($post['new']) ? $post['new'] : '';
  63. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  64. if (empty($new)) {
  65. util::fail('请输入新密码');
  66. }
  67. if ($new != $confirm) {
  68. util::fail('二次密码不一致');
  69. }
  70. $admin = $this->admin;
  71. $adminId = $this->adminId;
  72. if (!empty($admin['password'])) {
  73. if (empty($old)) {
  74. util::fail('请填写旧密码');
  75. }
  76. if (password_verify($old, $admin['password']) == false) {
  77. util::fail('旧密码错误');
  78. }
  79. }
  80. AdminService::updateById($adminId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  81. util::complete('修改成功');
  82. }
  83. //确认密码修改
  84. public function actionConfirmPassword()
  85. {
  86. $post = Yii::$app->request->post();
  87. $old = isset($post['old']) ? $post['old'] : '';
  88. $new = isset($post['new']) ? $post['new'] : '';
  89. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  90. if (empty($new)) {
  91. util::fail('请输入新密码');
  92. }
  93. if ($new != $confirm) {
  94. util::fail('二次密码不一致');
  95. }
  96. $admin = $this->admin;
  97. $adminId = $this->adminId;
  98. if (!empty($admin['confirmPassword'])) {
  99. if (empty($old)) {
  100. util::fail('请填写旧密码');
  101. }
  102. if (password_verify($old, $admin['confirmPassword']) == false) {
  103. util::fail('旧密码错误');
  104. }
  105. }
  106. AdminService::updateById($adminId, ['confirmPassword' => password_hash($new, PASSWORD_BCRYPT)]);
  107. util::complete('修改成功');
  108. }
  109. //登陆员工的详情 shish 2019.12.26
  110. public function actionLoginDetail()
  111. {
  112. $detail = AdminService::getDetail($this->adminId);
  113. util::success($detail);
  114. }
  115. //小程序用户完整信息 shish 2019.12.12
  116. public function actionMiniFullInfo()
  117. {
  118. $post = Yii::$app->request->post();
  119. $iv = isset($post['iv']) ? $post['iv'] : '';
  120. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  121. $merchant = WxOpenService::getWxInfo();
  122. $appId = $merchant['miniAppId'];
  123. $admin = $this->admin;
  124. $miniOpenId = $admin['miniOpenId'];
  125. if (empty($miniOpenId)) {
  126. util::fail('没有找到管理员信息(miniOpenId)');
  127. }
  128. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  129. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  130. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  131. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  132. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  133. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  134. if ($errCode != 0) {
  135. Yii::info($result . ' ' . $errCode);
  136. util::fail('获取用户信息失败');
  137. }
  138. Yii::info('小程序获取用户信息:' . $result);
  139. $originalInfo = Json::decode($result);
  140. $source = UserClass::$userSourceId['mini']['name'];
  141. $admin = AdminService::replaceAdmin($originalInfo, $source);
  142. $adminId = $admin['id'];
  143. $admin = AdminService::getAdminById($adminId);
  144. util::success($admin);
  145. }
  146. //小程序用户手机号
  147. public function actionMiniMobile()
  148. {
  149. $post = Yii::$app->request->post();
  150. $iv = $post['iv'];
  151. $encryptedData = $post['encryptedData'];
  152. $merchant = WxOpenService::getWxInfo();
  153. $appId = $merchant['miniAppId'];
  154. $admin = $this->admin;
  155. $miniOpenId = $admin['miniOpenId'];
  156. if (empty($miniOpenId)) {
  157. util::fail('没有找到管理员信息(miniOpenId)');
  158. }
  159. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  160. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  161. if (empty($sessionKey)) {
  162. util::fail('sesstion key empty');
  163. }
  164. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  165. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  166. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  167. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  168. if ($errCode != 0) {
  169. util::fail('解密失败');
  170. }
  171. $arr = Json::decode($result);
  172. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  173. util::success(['mobile' => $mobile]);
  174. }
  175. }