AdminController.php 14 KB

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