AdminController.php 13 KB

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