AdminController.php 13 KB

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