AdminController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. //授权信息并实现自动登录 shish 20210121
  121. public function actionAuthToAutoLogin()
  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. $miniOpenId = $post['miniOpenId'];
  129. if (empty($miniOpenId)) {
  130. util::fail('启用失败');
  131. }
  132. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  133. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  134. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  135. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  136. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  137. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  138. if ($errCode != 0) {
  139. Yii::info($result . ' ' . $errCode);
  140. util::fail('获取用户信息失败');
  141. }
  142. Yii::info('小程序获取用户信息:' . $result);
  143. $wxInfo = Json::decode($result);
  144. print_r($wxInfo);
  145. }
  146. //小程序用户完整信息 ssh 2019.12.12
  147. public function actionMiniFullInfo()
  148. {
  149. $post = Yii::$app->request->post();
  150. $iv = isset($post['iv']) ? $post['iv'] : '';
  151. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  152. $merchant = WxOpenService::getWxInfo();
  153. $appId = $merchant['miniAppId'];
  154. $admin = $this->admin->attributes;
  155. $miniOpenId = $admin['miniOpenId'];
  156. if (empty($miniOpenId)) {
  157. util::fail('没有找到管理员信息(miniOpenId)');
  158. }
  159. $unionId = $admin['unionId'] ?? '';
  160. if (empty($unionId)) {
  161. util::fail('没有找到管理员信息(unionId)');
  162. }
  163. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  164. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  165. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  166. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  167. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  168. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  169. if ($errCode != 0) {
  170. Yii::info($result . ' ' . $errCode);
  171. util::fail('获取用户信息失败');
  172. }
  173. Yii::info('小程序获取用户信息:' . $result);
  174. $wxInfo = Json::decode($result);
  175. $source = UserClass::$userSourceId['mini']['name'];
  176. $wxInfo['miniOpenId'] = $admin['miniOpenId'] ?? '';
  177. //这个接口没有提供unionId,这边补充一下
  178. $wxInfo['unionId'] = $unionId;
  179. $admin = AdminService::replaceAdmin($wxInfo, $source);
  180. $adminId = $admin['id'];
  181. $admin = AdminService::getAdminById($adminId);
  182. util::success($admin);
  183. }
  184. //小程序用户手机号
  185. public function actionMiniMobile()
  186. {
  187. $post = Yii::$app->request->post();
  188. $iv = $post['iv'];
  189. $encryptedData = $post['encryptedData'];
  190. $merchant = WxOpenService::getWxInfo();
  191. $appId = $merchant['miniAppId'];
  192. $admin = $this->admin->attributes;
  193. $miniOpenId = $admin['miniOpenId'];
  194. if (empty($miniOpenId)) {
  195. util::fail('没有找到管理员信息(miniOpenId)');
  196. }
  197. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  198. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  199. if (empty($sessionKey)) {
  200. util::fail('sesstion key empty');
  201. }
  202. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  203. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  204. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  205. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  206. if ($errCode != 0) {
  207. util::fail('解密失败');
  208. }
  209. $arr = Json::decode($result);
  210. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  211. $originalMobile = $admin['mobile'] ?? '';
  212. if (empty($originalMobile)) {
  213. \biz\admin\classes\AdminClass::updateById($this->adminId, ['mobile' => $mobile]);
  214. }
  215. util::success(['mobile' => $mobile]);
  216. }
  217. //设置支付密码 ssh 2021.1.24
  218. public function actionSetPayPassword()
  219. {
  220. $get = Yii::$app->request->get();
  221. $adminId = $this->adminId;
  222. $info = AdminClass::getAdminById($adminId);
  223. if (isset($info['payPassword']) && !empty($info['payPassword'])) {
  224. util::fail('您已经设置了支付密码');
  225. }
  226. $password = $get['password'] ?? '';
  227. if (empty($password)) {
  228. util::fail('请输入支付密码');
  229. }
  230. $string = password_hash($password, PASSWORD_BCRYPT);
  231. AdminClass::updateById($adminId, ['payPassword' => $string]);
  232. $data = ['payPassword' => $string];
  233. util::success($data);
  234. }
  235. }