AdminController.php 12 KB

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