AdminController.php 12 KB

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