AdminController.php 14 KB

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