AdminController.php 14 KB

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