AdminController.php 12 KB

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