AdminController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\item\classes\LabelClass;
  4. use bizGhs\shop\classes\ShopAdminClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizGhs\admin\classes\AdminClass;
  7. use bizGhs\admin\services\AdminService;
  8. use common\components\dict;
  9. use common\components\stringUtil;
  10. use common\components\util;
  11. use Yii;
  12. use yii\helpers\Json;
  13. use bizHd\user\classes\UserClass;
  14. use bizGhs\device\classes\GhsDeviceClass;
  15. class AdminController extends BaseController
  16. {
  17. public $guestAccess = ['recent-shop', 'mini-full-info', 'mini-mobile', 'login-detail'];
  18. //清空登录信息 ssh 20240428
  19. public function actionClearLogin()
  20. {
  21. $deviceId = Yii::$app->request->post('deviceId', '');
  22. $admin = $this->admin;
  23. $admin->miniOpenId = '';
  24. $admin->ghsMiniOpenId = '';
  25. $admin->save();
  26. //设备登录状态更新
  27. if ($deviceId != '') {
  28. $device = GhsDeviceClass::getByCondition(['shopId'=>$this->shopId, 'deviceId'=>$deviceId], true);
  29. if ($device) {
  30. $device->status = 0;//退出
  31. $device->save();
  32. }
  33. }
  34. util::complete();
  35. }
  36. //个人信息 ssh 20230506
  37. public function actionInfo()
  38. {
  39. $admin = $this->admin;
  40. util::success(['info' => $admin]);
  41. }
  42. //修改个人信息 ssh 20230505
  43. public function actionUpdateAdmin()
  44. {
  45. $post = Yii::$app->request->post();
  46. $ghsOpenId = $post['ghsOpenId'] ?? '';
  47. $name = $post['name'] ?? '';
  48. $admin = $this->admin;
  49. $admin->ghsOpenId = $ghsOpenId;
  50. if (!empty($name)) {
  51. $admin->name = $name;
  52. }
  53. $admin->save();
  54. if (!empty($name)) {
  55. $adminId = $this->adminId;
  56. ShopAdminClass::updateByCondition(['adminId' => $adminId], ['name' => $name]);
  57. }
  58. util::complete();
  59. }
  60. //绑定帐号自动登录 ssh 20210121
  61. public function actionBindAutoLogin()
  62. {
  63. $post = Yii::$app->request->post();
  64. $iv = isset($post['iv']) ? $post['iv'] : '';
  65. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  66. $merchant = WxOpenClass::getGhsWxInfo();
  67. $appId = $merchant['miniAppId'];
  68. $miniOpenId = $post['miniOpenId'];
  69. if (empty($miniOpenId)) {
  70. util::fail('绑定失败');
  71. }
  72. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  73. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  74. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  75. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  76. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  77. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  78. if ($errCode != 0) {
  79. Yii::info($result . ' ' . $errCode);
  80. util::fail('绑定失败了');
  81. }
  82. Yii::info('小程序获取用户信息:' . $result);
  83. $wxInfo = Json::decode($result);
  84. $nickName = $wxInfo['nickName'] ?? '';
  85. if (empty($nickName)) {
  86. util::fail('绑定失败');
  87. }
  88. $admin = $this->admin ?? [];
  89. if (empty($admin)) {
  90. util::fail('绑定失败');
  91. }
  92. $admin->ghsMiniOpenId = $miniOpenId;
  93. $admin->save();
  94. util::complete();
  95. }
  96. //修改或保存安卓clientId
  97. public function actionReplaceClientId()
  98. {
  99. $get = Yii::$app->request->get();
  100. $id = $get['id'] ?? '';
  101. $deviceId = $get['deviceId'] ?? '';
  102. $autoLogin = $get['auto'] ?? 0; // 是否自动登录的请求
  103. //noticeUtil::push("批发客户端ID: " . $id);
  104. if (empty($id)) {
  105. util::success(['returnStatus' => 'FAILURE']);
  106. }
  107. $this->admin->clientId = $id;
  108. $this->admin->save();
  109. $ghsDevice = GhsDeviceClass::getByCondition(['shopId'=>$this->shopId, 'deviceId'=>$deviceId], true);
  110. if ($ghsDevice == null){
  111. $device = [
  112. 'deviceId' => $deviceId,
  113. 'clientId' => $id,
  114. 'mainId' => $this->mainId,
  115. 'shopId' => $this->shopId,
  116. 'adminId' => $this->adminId,
  117. 'status' => 1
  118. ];
  119. if ($autoLogin == 1) {
  120. $device['autoLoginTime'] = date('Y-m-d H:i:s');
  121. } else {
  122. $device['pwdLoginTime'] = date('Y-m-d H:i:s');
  123. }
  124. GhsDeviceClass::add($device);
  125. } else {
  126. if ($autoLogin == 1) {
  127. $ghsDevice->autoLoginTime = date('Y-m-d H:i:s');
  128. } else {
  129. $ghsDevice->pwdLoginTime = date('Y-m-d H:i:s');
  130. }
  131. $ghsDevice->clientId = $id;
  132. $ghsDevice->mainId = $this->mainId;
  133. $ghsDevice->shopId = $this->shopId;
  134. $ghsDevice->adminId = $this->adminId;
  135. $ghsDevice->save();
  136. }
  137. util::complete();
  138. }
  139. //修改密码 ssh 20220106
  140. public function actionModifyPassword()
  141. {
  142. $get = Yii::$app->request->get();
  143. $password = $get['password'] ?? '';
  144. if (empty($password)) {
  145. util::fail('请填写密码');
  146. }
  147. stringUtil::passwordCheck($password);
  148. $adminId = $this->adminId;
  149. $password = password_hash($password, PASSWORD_BCRYPT);
  150. AdminClass::updateById($adminId, ['password' => $password]);
  151. util::complete('操作成功');
  152. }
  153. //最近店铺 ssh 2021.2.27
  154. public function actionRecentShop()
  155. {
  156. $respond = AdminClass::getRecentShop($this->admin);
  157. util::success($respond);
  158. }
  159. //获取登陆员工的权限 ssh 2019.11.24
  160. public function actionLoginAuth()
  161. {
  162. $auth = AdminService::getAuth($this->adminId, $this->sjId);
  163. util::success($auth);
  164. }
  165. //添加员工 ssh 2019.12.8
  166. public function actionAdd()
  167. {
  168. $post = Yii::$app->request->post();
  169. $post['sjId'] = $this->sjId;
  170. $admin = AdminService::addAdmin($post);
  171. $id = $admin['id'];
  172. $info = AdminService::getDetail($id);
  173. util::success($info);
  174. }
  175. //删除员工 ssh 2019.12.9
  176. public function actionDelete()
  177. {
  178. Yii::$app->end();
  179. $id = Yii::$app->request->post('id');
  180. $admin = AdminClass::getById($id);
  181. AdminService::valid($admin, $this->sjId);
  182. AdminService::deleteAdmin($admin, $this->adminId);
  183. util::complete();
  184. }
  185. //查看员工详情 ssh 2019.12.9
  186. public function actionDetail()
  187. {
  188. $id = Yii::$app->request->get('id');
  189. $admin = AdminService::getDetail($id);
  190. AdminService::valid($admin, $this->sjId);
  191. util::success($admin);
  192. }
  193. //密码修改
  194. public function actionPassword()
  195. {
  196. $post = Yii::$app->request->post();
  197. $old = isset($post['old']) ? $post['old'] : '';
  198. $new = isset($post['new']) ? $post['new'] : '';
  199. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  200. if (empty($new)) {
  201. util::fail('请输入新密码');
  202. }
  203. if ($new != $confirm) {
  204. util::fail('二次密码不一致');
  205. }
  206. $admin = $this->admin->attributes;
  207. $adminId = $this->adminId;
  208. if (!empty($admin['password'])) {
  209. if (empty($old)) {
  210. util::fail('请填写旧密码');
  211. }
  212. if (password_verify($old, $admin['password']) == false) {
  213. util::fail('旧密码错误');
  214. }
  215. }
  216. AdminService::updateById($adminId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  217. util::complete('修改成功');
  218. }
  219. //支付密码修改
  220. public function actionConfirmPassword()
  221. {
  222. $post = Yii::$app->request->post();
  223. $old = isset($post['old']) ? $post['old'] : '';
  224. $new = isset($post['new']) ? $post['new'] : '';
  225. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  226. if (empty($new)) {
  227. util::fail('请输入新密码');
  228. }
  229. if ($new != $confirm) {
  230. util::fail('二次密码不一致');
  231. }
  232. $admin = $this->admin->attributes;
  233. $adminId = $this->adminId;
  234. if (!empty($admin['payPassword'])) {
  235. if (empty($old)) {
  236. util::fail('请填写旧密码');
  237. }
  238. if (password_verify($old, $admin['payPassword']) == false) {
  239. util::fail('旧密码错误');
  240. }
  241. }
  242. AdminService::updateById($adminId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT)]);
  243. util::complete('修改成功');
  244. }
  245. //登陆员工的详情 ssh 2019.12.26
  246. public function actionLoginDetail()
  247. {
  248. $detail = AdminService::getDetail($this->adminId);
  249. util::success($detail);
  250. }
  251. //小程序用户完整信息 ssh 2019.12.12
  252. public function actionMiniFullInfo()
  253. {
  254. $post = Yii::$app->request->post();
  255. $iv = isset($post['iv']) ? $post['iv'] : '';
  256. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  257. $merchant = WxOpenClass::getGhsWxInfo();
  258. $appId = $merchant['miniAppId'];
  259. $admin = $this->admin;
  260. $adminId = $this->adminId;
  261. $miniOpenId = $admin->ghsMiniOpenId;
  262. if (empty($miniOpenId)) {
  263. util::fail('没有找到管理员信息(miniOpenId)');
  264. }
  265. $unionId = $admin->unionId ?? '';
  266. if (empty($unionId)) {
  267. util::fail('没有找到管理员信息(unionId)');
  268. }
  269. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  270. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  271. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  272. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  273. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  274. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  275. if ($errCode != 0) {
  276. Yii::info($result . ' ' . $errCode);
  277. util::fail('获取用户信息失败');
  278. }
  279. Yii::info('小程序获取用户信息:' . $result);
  280. $wxInfo = Json::decode($result);
  281. $source = UserClass::$userSourceId['mini']['name'];
  282. $wxInfo['ghsMiniOpenId'] = $admin->ghsMiniOpenId ?? '';
  283. $wxInfo['unionId'] = $unionId;
  284. $wxInfo['mainId'] = $this->mainId;
  285. //更新
  286. AdminClass::replaceAdmin($wxInfo, $source);
  287. $admin = AdminService::getAdminById($adminId);
  288. util::success($admin);
  289. }
  290. //小程序用户手机号
  291. public function actionMiniMobile()
  292. {
  293. $post = Yii::$app->request->post();
  294. $iv = $post['iv'];
  295. $encryptedData = $post['encryptedData'];
  296. $merchant = WxOpenClass::getGhsWxInfo();
  297. $appId = $merchant['miniAppId'];
  298. $admin = $this->admin->attributes;
  299. $miniOpenId = $admin['miniOpenId'];
  300. if (empty($miniOpenId)) {
  301. util::fail('没有找到管理员信息(miniOpenId)');
  302. }
  303. $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  304. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  305. if (empty($sessionKey)) {
  306. util::fail('sesstion key empty');
  307. }
  308. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  309. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  310. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  311. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  312. if ($errCode != 0) {
  313. util::fail('解密失败');
  314. }
  315. $arr = Json::decode($result);
  316. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  317. $originalMobile = $admin['mobile'] ?? '';
  318. if (empty($originalMobile)) {
  319. \biz\admin\classes\AdminClass::updateById($this->adminId, ['mobile' => $mobile]);
  320. }
  321. util::success(['mobile' => $mobile]);
  322. }
  323. //已经存有token,但没有store信息重新获取
  324. //注意这里的输出内容有多个地方一样,要修改需要同步修改,请搜索关键词loginOK!!!!!!!!
  325. public function actionHasTokenAutoLogin()
  326. {
  327. $admin = $this->admin;
  328. $currentGhsShopId = $admin->currentGhsShopId ?? 0;
  329. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($this->shopAdmin);
  330. $openShop = $admin['openGhsShop'] ?? 1;
  331. $showDemo = 1;
  332. $skCustomId = $this->shop->skCustomId ?? 0;
  333. $mainId = $this->shop->mainId ?? 0;
  334. $apiHost = Yii::$app->params['ghsHost'];
  335. $imgUploadApi = $apiHost . '/upload/save-file';
  336. //使用手册
  337. $cacheKey = 'close_book_' . $this->shopAdminId;
  338. $hasClose = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  339. $showBook = !empty($hasClose) ? 0 : 1;
  340. $cacheKey = 'has_hit_navigate_' . $this->shopAdminId;
  341. $hasHit = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  342. $hasHitNavigate = !empty($hasHit) ? 1 : 0;
  343. $ghsUpgrading = getenv('GHS_UPGRADING') == false ? 0 : getenv('GHS_UPGRADING');
  344. if ($ghsUpgrading == 1) {
  345. util::fail('系统升级中,稍后再试');
  346. }
  347. $lookAllShop = 0;
  348. $adminId = $admin['id'] ?? 0;
  349. if (getenv('YII_ENV') == 'production') {
  350. $couldLookAllShop = dict::getDict('couldLookAllShop');
  351. if (in_array($adminId, $couldLookAllShop)) {
  352. $lookAllShop = 1;
  353. }
  354. } else {
  355. $lookAllShop = 1;
  356. }
  357. //惠雅鲜花员工不能看收入情况
  358. if (in_array($adminId, [2366, 2812, 4004])) {
  359. $this->shopAdmin->super = 0;
  360. }
  361. $labelList = LabelClass::getMyUnUseLabelList($mainId);
  362. util::success([
  363. 'token' => '',
  364. 'admin' => $admin,
  365. 'shopAdminId' => $this->shopAdminId,
  366. 'shopId' => $currentGhsShopId,
  367. 'switchShop' => $switchShop,
  368. 'openShop' => $openShop,
  369. 'showDemo' => $showDemo,
  370. //有小程序新版本提示更新
  371. 'update' => 0,
  372. 'skCustomId' => $skCustomId,
  373. 'apiHost' => $apiHost,
  374. 'imgUploadApi' => $imgUploadApi,
  375. 'showBook' => $showBook,
  376. 'hasHitNavigate' => $hasHitNavigate,
  377. 'staff' => $this->shopAdmin,
  378. 'lookAllShop' => $lookAllShop,
  379. 'labelList' => $labelList,
  380. ]);
  381. }
  382. }