AdminController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace ghs\controllers;
  3. use bizHd\wx\classes\WxOpenClass;
  4. use bizGhs\admin\classes\AdminClass;
  5. use bizGhs\admin\services\AdminService;
  6. use bizHd\wx\services\WxOpenService;
  7. use common\components\noticeUtil;
  8. use common\components\util;
  9. use Yii;
  10. use yii\helpers\Json;
  11. use bizHd\user\classes\UserClass;
  12. class AdminController extends BaseController
  13. {
  14. public $guestAccess = ['recent-shop', 'mini-full-info', 'mini-mobile', 'login-detail'];
  15. //绑定帐号自动登录 ssh 20210121
  16. public function actionBindAutoLogin()
  17. {
  18. $post = Yii::$app->request->post();
  19. $iv = isset($post['iv']) ? $post['iv'] : '';
  20. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  21. $merchant = WxOpenClass::getGhsWxInfo();
  22. $appId = $merchant['miniAppId'];
  23. $miniOpenId = $post['miniOpenId'];
  24. if (empty($miniOpenId)) {
  25. util::fail('绑定失败');
  26. }
  27. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  28. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  29. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  30. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  31. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  32. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  33. if ($errCode != 0) {
  34. Yii::info($result . ' ' . $errCode);
  35. util::fail('绑定失败了');
  36. }
  37. Yii::info('小程序获取用户信息:' . $result);
  38. $wxInfo = Json::decode($result);
  39. $nickName = $wxInfo['nickName'] ?? '';
  40. if (empty($nickName)) {
  41. util::fail('绑定失败');
  42. }
  43. $admin = $this->admin ?? [];
  44. if (empty($admin)) {
  45. util::fail('绑定失败');
  46. }
  47. $admin->ghsMiniOpenId = $miniOpenId;
  48. $admin->save();
  49. util::complete();
  50. }
  51. //修改保存安卓clientId
  52. public function actionReplaceClientId()
  53. {
  54. $get = Yii::$app->request->get();
  55. $id = $get['id'] ?? '';
  56. if (empty($id)) {
  57. util::success(['returnStatus' => 'FAILURE']);
  58. }
  59. $this->admin->clientId = $id;
  60. $this->admin->save();
  61. noticeUtil::push("保存客户端ID:" . $id, '15280215347');
  62. util::complete();
  63. }
  64. //修改密码 ssh 20220106
  65. public function actionModifyPassword()
  66. {
  67. $get = Yii::$app->request->get();
  68. $password = $get['password'] ?? '';
  69. if (empty($password)) {
  70. util::fail('请填写密码');
  71. }
  72. if (ctype_alnum($password)) {
  73. util::fail('密码必须是字母、数字和符号的组合');
  74. }
  75. $adminId = $this->adminId;
  76. $password = password_hash($password, PASSWORD_BCRYPT);
  77. AdminClass::updateById($adminId, ['password' => $password]);
  78. util::complete('操作成功');
  79. }
  80. //最近店铺 ssh 2021.2.27
  81. public function actionRecentShop()
  82. {
  83. $respond = AdminClass::getRecentShop($this->admin);
  84. util::success($respond);
  85. }
  86. //获取登陆员工的权限 ssh 2019.11.24
  87. public function actionLoginAuth()
  88. {
  89. $auth = AdminService::getAuth($this->adminId, $this->sjId);
  90. util::success($auth);
  91. }
  92. //添加员工 ssh 2019.12.8
  93. public function actionAdd()
  94. {
  95. $post = Yii::$app->request->post();
  96. $post['sjId'] = $this->sjId;
  97. $admin = AdminService::addAdmin($post);
  98. $id = $admin['id'];
  99. $info = AdminService::getDetail($id);
  100. util::success($info);
  101. }
  102. //删除员工 ssh 2019.12.9
  103. public function actionDelete()
  104. {
  105. Yii::$app->end();
  106. $id = Yii::$app->request->post('id');
  107. $admin = AdminClass::getById($id);
  108. AdminService::valid($admin, $this->sjId);
  109. AdminService::deleteAdmin($admin, $this->adminId);
  110. util::complete();
  111. }
  112. //查看员工详情 ssh 2019.12.9
  113. public function actionDetail()
  114. {
  115. $id = Yii::$app->request->get('id');
  116. $admin = AdminService::getDetail($id);
  117. AdminService::valid($admin, $this->sjId);
  118. util::success($admin);
  119. }
  120. //密码修改
  121. public function actionPassword()
  122. {
  123. $post = Yii::$app->request->post();
  124. $old = isset($post['old']) ? $post['old'] : '';
  125. $new = isset($post['new']) ? $post['new'] : '';
  126. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  127. if (empty($new)) {
  128. util::fail('请输入新密码');
  129. }
  130. if ($new != $confirm) {
  131. util::fail('二次密码不一致');
  132. }
  133. $admin = $this->admin->attributes;
  134. $adminId = $this->adminId;
  135. if (!empty($admin['password'])) {
  136. if (empty($old)) {
  137. util::fail('请填写旧密码');
  138. }
  139. if (password_verify($old, $admin['password']) == false) {
  140. util::fail('旧密码错误');
  141. }
  142. }
  143. AdminService::updateById($adminId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  144. util::complete('修改成功');
  145. }
  146. //支付密码修改
  147. public function actionConfirmPassword()
  148. {
  149. $post = Yii::$app->request->post();
  150. $old = isset($post['old']) ? $post['old'] : '';
  151. $new = isset($post['new']) ? $post['new'] : '';
  152. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  153. if (empty($new)) {
  154. util::fail('请输入新密码');
  155. }
  156. if ($new != $confirm) {
  157. util::fail('二次密码不一致');
  158. }
  159. $admin = $this->admin->attributes;
  160. $adminId = $this->adminId;
  161. if (!empty($admin['payPassword'])) {
  162. if (empty($old)) {
  163. util::fail('请填写旧密码');
  164. }
  165. if (password_verify($old, $admin['payPassword']) == false) {
  166. util::fail('旧密码错误');
  167. }
  168. }
  169. AdminService::updateById($adminId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT)]);
  170. util::complete('修改成功');
  171. }
  172. //登陆员工的详情 ssh 2019.12.26
  173. public function actionLoginDetail()
  174. {
  175. $detail = AdminService::getDetail($this->adminId);
  176. util::success($detail);
  177. }
  178. //小程序用户完整信息 ssh 2019.12.12
  179. public function actionMiniFullInfo()
  180. {
  181. $post = Yii::$app->request->post();
  182. $iv = isset($post['iv']) ? $post['iv'] : '';
  183. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  184. $merchant = WxOpenClass::getGhsWxInfo();
  185. $appId = $merchant['miniAppId'];
  186. $admin = $this->admin;
  187. $adminId = $this->adminId;
  188. $miniOpenId = $admin->ghsMiniOpenId;
  189. if (empty($miniOpenId)) {
  190. util::fail('没有找到管理员信息(miniOpenId)');
  191. }
  192. $unionId = $admin->unionId ?? '';
  193. if (empty($unionId)) {
  194. util::fail('没有找到管理员信息(unionId)');
  195. }
  196. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  197. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  198. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  199. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  200. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  201. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  202. if ($errCode != 0) {
  203. Yii::info($result . ' ' . $errCode);
  204. util::fail('获取用户信息失败');
  205. }
  206. Yii::info('小程序获取用户信息:' . $result);
  207. $wxInfo = Json::decode($result);
  208. $source = UserClass::$userSourceId['mini']['name'];
  209. $wxInfo['ghsMiniOpenId'] = $admin->ghsMiniOpenId ?? '';
  210. $wxInfo['unionId'] = $unionId;
  211. $wxInfo['mainId'] = $this->mainId;
  212. //更新
  213. AdminClass::replaceAdmin($wxInfo, $source);
  214. $admin = AdminService::getAdminById($adminId);
  215. util::success($admin);
  216. }
  217. //小程序用户手机号
  218. public function actionMiniMobile()
  219. {
  220. $post = Yii::$app->request->post();
  221. $iv = $post['iv'];
  222. $encryptedData = $post['encryptedData'];
  223. $merchant = WxOpenClass::getGhsWxInfo();
  224. $appId = $merchant['miniAppId'];
  225. $admin = $this->admin->attributes;
  226. $miniOpenId = $admin['miniOpenId'];
  227. if (empty($miniOpenId)) {
  228. util::fail('没有找到管理员信息(miniOpenId)');
  229. }
  230. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  231. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  232. if (empty($sessionKey)) {
  233. util::fail('sesstion key empty');
  234. }
  235. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  236. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  237. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  238. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  239. if ($errCode != 0) {
  240. util::fail('解密失败');
  241. }
  242. $arr = Json::decode($result);
  243. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  244. $originalMobile = $admin['mobile'] ?? '';
  245. if (empty($originalMobile)) {
  246. \biz\admin\classes\AdminClass::updateById($this->adminId, ['mobile' => $mobile]);
  247. }
  248. util::success(['mobile' => $mobile]);
  249. }
  250. //已经存有token,但没有store信息重新获取
  251. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  252. public function actionHasTokenAutoLogin()
  253. {
  254. $admin = $this->admin;
  255. $currentGhsShopId = $admin->currentGhsShopId ?? 0;
  256. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($this->shopAdmin);
  257. $openShop = $admin['openGhsShop'] ?? 1;
  258. $showDemo = 1;
  259. $skCustomId = $this->shop->skCustomId ?? 0;
  260. $apiHost = Yii::$app->params['ghsHost'];
  261. $imgUploadApi = $apiHost . '/upload/save-file';
  262. //使用手册
  263. $cacheKey = 'close_book_' . $this->shopAdminId;
  264. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  265. $showBook = !empty($hasClose) ? 0 : 1;
  266. $cacheKey = 'has_hit_navigate_' . $this->shopAdminId;
  267. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  268. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  269. $ghsUpgrading = getenv('GHS_UPGRADING') == false ? 0 : getenv('GHS_UPGRADING');
  270. if ($ghsUpgrading == 1) {
  271. util::fail('系统升级中,稍后再试');
  272. }
  273. $lookAllShop = 0;
  274. $adminId = $admin['id'] ?? 0;
  275. if (getenv('YII_ENV') == 'production') {
  276. if ($adminId == 4) {
  277. $lookAllShop = 1;
  278. }
  279. } else {
  280. $lookAllShop = 1;
  281. }
  282. util::success([
  283. 'token' => '',
  284. 'admin' => $admin,
  285. 'shopAdminId' => $this->shopAdminId,
  286. 'shopId' => $currentGhsShopId,
  287. 'switchShop' => $switchShop,
  288. 'openShop' => $openShop,
  289. 'showDemo' => $showDemo,
  290. //有小程序新版本提示更新
  291. 'update' => 0,
  292. 'skCustomId' => $skCustomId,
  293. 'apiHost' => $apiHost,
  294. 'imgUploadApi' => $imgUploadApi,
  295. 'showBook' => $showBook,
  296. 'hasHitNavigate' => $hasHitNavigate,
  297. 'staff' => $this->shopAdmin,
  298. 'lookAllShop' => $lookAllShop,
  299. ]);
  300. }
  301. }