AdminController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 $guestAccessAction = [];
  13. //最近店铺 shish 2021.2.27
  14. public function actionRecentShop()
  15. {
  16. $respond = AdminClass::getRecentShop($this->admin);
  17. util::success($respond);
  18. }
  19. //获取登陆员工的权限 shish 2019.11.24
  20. public function actionLoginAuth()
  21. {
  22. $auth = AdminService::getAuth($this->adminId, $this->sjId);
  23. util::success($auth);
  24. }
  25. //添加员工 shish 2019.12.8
  26. public function actionAdd()
  27. {
  28. $post = Yii::$app->request->post();
  29. $post['merchantId'] = $this->sjId;
  30. $admin = AdminService::addAdmin($post);
  31. $id = $admin['id'];
  32. $info = AdminService::getDetail($id);
  33. util::success($info);
  34. }
  35. //删除员工 shish 2019.12.9
  36. public function actionDelete()
  37. {
  38. $id = Yii::$app->request->post('id');
  39. $admin = AdminClass::getById($id);
  40. AdminService::valid($admin, $this->sjId);
  41. AdminService::deleteAdmin($admin, $this->adminId);
  42. util::complete();
  43. }
  44. //修改员工 shish 2019.12.9
  45. public function actionUpdate()
  46. {
  47. $post = Yii::$app->request->post();
  48. $id = isset($post['id']) && is_numeric($post['id']) ? $post['id'] : 0;
  49. unset($post['id']);
  50. $originalAdmin = AdminService::getById($id);
  51. $post['merchantId'] = $this->sjId;
  52. AdminService::updateAdmin($originalAdmin, $post);
  53. util::complete('修改成功');
  54. }
  55. //查看员工详情 shish 2019.12.9
  56. public function actionDetail()
  57. {
  58. $id = Yii::$app->request->get('id');
  59. $admin = AdminService::getDetail($id);
  60. AdminService::valid($admin, $this->sjId);
  61. util::success($admin);
  62. }
  63. //密码修改
  64. public function actionPassword()
  65. {
  66. $post = Yii::$app->request->post();
  67. $old = isset($post['old']) ? $post['old'] : '';
  68. $new = isset($post['new']) ? $post['new'] : '';
  69. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  70. if (empty($new)) {
  71. util::fail('请输入新密码');
  72. }
  73. if ($new != $confirm) {
  74. util::fail('二次密码不一致');
  75. }
  76. $admin = $this->admin;
  77. $adminId = $this->adminId;
  78. if (!empty($admin['password'])) {
  79. if (empty($old)) {
  80. util::fail('请填写旧密码');
  81. }
  82. if (password_verify($old, $admin['password']) == false) {
  83. util::fail('旧密码错误');
  84. }
  85. }
  86. AdminService::updateById($adminId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  87. util::complete('修改成功');
  88. }
  89. //确认密码修改
  90. public function actionConfirmPassword()
  91. {
  92. $post = Yii::$app->request->post();
  93. $old = isset($post['old']) ? $post['old'] : '';
  94. $new = isset($post['new']) ? $post['new'] : '';
  95. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  96. if (empty($new)) {
  97. util::fail('请输入新密码');
  98. }
  99. if ($new != $confirm) {
  100. util::fail('二次密码不一致');
  101. }
  102. $admin = $this->admin;
  103. $adminId = $this->adminId;
  104. if (!empty($admin['confirmPassword'])) {
  105. if (empty($old)) {
  106. util::fail('请填写旧密码');
  107. }
  108. if (password_verify($old, $admin['confirmPassword']) == false) {
  109. util::fail('旧密码错误');
  110. }
  111. }
  112. AdminService::updateById($adminId, ['confirmPassword' => password_hash($new, PASSWORD_BCRYPT)]);
  113. util::complete('修改成功');
  114. }
  115. //登陆员工的详情 shish 2019.12.26
  116. public function actionLoginDetail()
  117. {
  118. $detail = AdminService::getDetail($this->adminId);
  119. util::success($detail);
  120. }
  121. //小程序用户完整信息 shish 2019.12.12
  122. public function actionMiniFullInfo()
  123. {
  124. $post = Yii::$app->request->post();
  125. $iv = isset($post['iv']) ? $post['iv'] : '';
  126. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  127. $merchant = WxOpenService::getWxInfo();
  128. $appId = $merchant['miniAppId'];
  129. $admin = $this->admin;
  130. $miniOpenId = $admin['miniOpenId'];
  131. if (empty($miniOpenId)) {
  132. util::fail('没有找到管理员信息(miniOpenId)');
  133. }
  134. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  135. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  136. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  137. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  138. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  139. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  140. if ($errCode != 0) {
  141. Yii::info($result . ' ' . $errCode);
  142. util::fail('获取用户信息失败');
  143. }
  144. Yii::info('小程序获取用户信息:' . $result);
  145. $originalInfo = Json::decode($result);
  146. $source = UserClass::$userSourceId['mini']['name'];
  147. $admin = AdminService::replaceAdmin($originalInfo, $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;
  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. util::success(['mobile' => $mobile]);
  180. }
  181. //设置支付密码 shish 2021.1.24
  182. public function actionSetPayPassword()
  183. {
  184. $get = Yii::$app->request->get();
  185. $adminId = $this->adminId;
  186. $info = AdminClass::getAdminById($adminId, false);
  187. if (isset($info['payPassword']) && !empty($info['payPassword'])) {
  188. util::fail('您已经设置了支付密码');
  189. }
  190. $password = $get['password'] ?? '';
  191. if (empty($password)) {
  192. util::fail('请输入支付密码');
  193. }
  194. $string = password_hash($password, PASSWORD_BCRYPT);
  195. AdminClass::updateById($adminId, ['payPassword' => $string]);
  196. util::complete();
  197. }
  198. }