AdminController.php 13 KB

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