AdminController.php 13 KB

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