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