AdminController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace backend\controllers;
  3. use common\components\httpUtil;
  4. use common\components\jwt;
  5. use Yii;
  6. use yii\web\Controller;
  7. use yii\helpers\Json;
  8. use common\services\xhAdminService;
  9. use common\services\xhCommonService;
  10. use common\services\xhMerchantService;
  11. use common\components\util;
  12. use common\components\stringUtil;
  13. use common\components\qrCodeUtil;
  14. use common\services\xhAdminToMerchantService;
  15. class AdminController extends BackendController
  16. {
  17. public $withoutLogin = ['check-mobile'];
  18. /**
  19. * 生成管理员
  20. */
  21. public function actionGenerate()
  22. {
  23. $get = Yii::$app->request->get();
  24. $newAccountKey = isset($get['newAccount']) ? $get['newAccount'] : '';
  25. //解密获取新帐号
  26. $newAccount = urldecode(stringUtil::authcode($newAccountKey, 'DECODE'));
  27. if (empty($newAccount)) {
  28. util::stop('链接已经失效,请重新授权');
  29. }
  30. $session = Yii::$app->session;
  31. $session['usable_new_account'] = $newAccount;
  32. $relation = xhAdminToMerchantService::getByAdminId($this->adminId);
  33. if (!empty($relation)) {
  34. $this->redirect(['admin/result', 'account' => $this->merchant['id']]);
  35. util::end();
  36. }
  37. return $this->renderPartial('generate', ['admin' => $this->admin, 'merchant' => $this->merchant]);
  38. }
  39. /**
  40. * 检查手机是否已经生成管理员
  41. */
  42. public function actionCheckMobile()
  43. {
  44. $get = Yii::$app->request->get();
  45. $mobile = isset($get['mobile']) ? $get['mobile'] : '';
  46. $admin = xhAdminService::getByMobile($mobile);
  47. if (empty($admin)) {
  48. util::successInfo('可以使用');
  49. }
  50. util::failInfo('手机号已使用');
  51. }
  52. /**
  53. * 生成管理员,生成验证码
  54. */
  55. public function actionGenerateCode()
  56. {
  57. $get = Yii::$app->request->get();
  58. $mobile = isset($get['mobile']) ? $get['mobile'] : '';
  59. if (preg_match("/^1[0-9]\d{9}$/", $mobile) == false) {
  60. util::failInfo('手机号填写有误');
  61. }
  62. $admin = xhAdminService::getByMobile($mobile);
  63. if (!empty($admin)) {
  64. util::failInfo('这个手机号已经使用过了');
  65. }
  66. $session = Yii::$app->session;
  67. $now = time();
  68. if (isset($session['authCode']) == false || ($now - $session['authCodeTime']) > 120) {
  69. $num = stringUtil::getRandNum(5);
  70. $session['authCode'] = $num;
  71. $session['authCodeTime'] = time();
  72. $msg = "您正在设置管理员,验证码:" . $num;
  73. xhCommonService::sendMobileMsg($mobile, $msg);
  74. util::successInfo();
  75. }
  76. }
  77. /**
  78. * 检测验证码
  79. */
  80. public function actionCheckCode()
  81. {
  82. $get = Yii::$app->request->get();
  83. $authCode = isset($get['authCode']) ? $get['authCode'] : '';
  84. $session = Yii::$app->session;
  85. if (isset($session['authCode']) && $authCode == $session['authCode']) {
  86. util::successInfo('');
  87. }
  88. util::failInfo('验证号不正确');
  89. }
  90. public function actionResult()
  91. {
  92. return $this->renderPartial('result', ['merchant' => $this->merchant]);
  93. }
  94. /**
  95. * 绑定
  96. */
  97. public function actionBinding()
  98. {
  99. $post = Yii::$app->request->post();
  100. $session = Yii::$app->session;
  101. if (isset($session['usable_new_account']) == false || empty($session['usable_new_account'])) {
  102. util::failInfo('授权已经失效,请重新授权');
  103. }
  104. if (isset($session['authCode']) == false) {
  105. util::failInfo('没有验证码');
  106. }
  107. $code = isset($post['code']) ? $post['code'] : '';
  108. if ($code != $session['authCode']) {
  109. util::failInfo('验证码不正确');
  110. }
  111. $mobile = isset($post['mobile']) ? $post['mobile'] : '';
  112. if (stringUtil::isMobieNumber($mobile) == false) {
  113. util::failInfo('手机号填写有误');
  114. }
  115. $admin = xhAdminService::getByMobile($mobile);
  116. if (!empty($admin)) {
  117. util::failInfo('手机号已经被使用');
  118. }
  119. $relation = xhAdminToMerchantService::getByAdminId($this->adminId);
  120. if (!empty($relation)) {
  121. util::failInfo('管理员已经绑定商家');
  122. }
  123. $newAccount = $session['usable_new_account'];
  124. $newMerchant = xhMerchantService::getByAccount($newAccount);
  125. if (empty($newMerchant)) {
  126. util::failInfo('商家信息不完整');
  127. }
  128. $merchantId = $this->merchant['id'];
  129. $account = $this->merchant['id'];
  130. $adminId = $this->adminId;
  131. $data['mobile'] = $mobile;
  132. $data['password'] = md5($post['password']);
  133. xhAdminService::updateById($adminId, $data);
  134. $addAdminData = ['merchantId' => $newMerchant['id'], 'adminId' => $this->adminId, 'receiveMsg' => 1, 'createTime' => date("Y-m-d H:i:s")];
  135. xhAdminToMerchantService::add($addAdminData);
  136. unset($session['usable_new_account']);
  137. util::successInfo('设置成功');
  138. }
  139. public function actionGetReplaceQrCode()
  140. {
  141. $get = Yii::$app->request->get();
  142. $url = Yii::$app->params['adminUrl'] . '/admin/replace?account=' . $this->merchant['id'];
  143. $logo = Yii::getAlias("@app") . '/../images' . $this->merchant['logo'];//取商家的logo
  144. $imgUrl = qrCodeUtil::generateFixQrCode($url, 'backend_replace_admin_url_' . $this->merchant['id'], $logo);
  145. xhAdminService::logout();
  146. echo Json::encode(['code' => 'A0001', 'msg' => 'success', 'data' => ['url' => $imgUrl]]);
  147. }
  148. /**
  149. * 替换页
  150. */
  151. public function actionReplace()
  152. {
  153. if ($this->isWeixin == false) {
  154. util::stop('请使用微信访问');
  155. }
  156. $preAdmin = xhAdminToMerchantService::getAdminByMerchantId($this->merchantId);
  157. $preAdminId = $preAdmin['id'];
  158. $newAdminId = $this->adminId;
  159. if ($newAdminId == $preAdminId) {
  160. util::stop('您已经绑定过了');
  161. }
  162. $relation = xhAdminToMerchantService::getByAdminId($newAdminId);
  163. if (!empty($relation)) {
  164. util::stop('您的微信已经绑定商家,请先解除绑定');
  165. }
  166. return $this->renderPartial('replace', ['preAdmin' => $preAdmin]);
  167. }
  168. /**
  169. * 显示新管理员页
  170. */
  171. public function actionExecReplace()
  172. {
  173. $get = Yii::$app->request->get();
  174. $replaceAdminAuthCode = isset($get['replaceAdminAuthCode']) ? $get['replaceAdminAuthCode'] : '';
  175. $admin = $this->admin;
  176. return $this->renderPartial('execReplace', ['replaceAdminAuthCode' => $replaceAdminAuthCode, 'admin' => $admin]);
  177. }
  178. /**
  179. * 更换管理员生成验证码
  180. */
  181. public function actionRelaceAdminGenerateCode()
  182. {
  183. $get = Yii::$app->request->get();
  184. $preAdmin = xhAdminToMerchantService::getAdminByMerchantId($this->merchantId);
  185. $mobile = $preAdmin['mobile'];
  186. $session = Yii::$app->session;
  187. $now = time();
  188. if (isset($session['replaceAdminAuthCode']) == false || ($now - $session['replaceAdminAuthCodeTime']) > 120) {
  189. $num = stringUtil::getRandNum(5);
  190. $session['replaceAdminAuthCode'] = $num;
  191. $session['replaceAdminAuthCodeTime'] = time();
  192. $msg = "您正在更换管理员,验证码:" . $num;
  193. xhCommonService::sendMobileMsg($mobile, $msg);
  194. util::successInfo();
  195. }
  196. }
  197. /**
  198. * 检查验证码
  199. */
  200. public function actionCheckRelaceAdminGenerateCode()
  201. {
  202. $get = Yii::$app->request->get();
  203. $authCode = isset($get['authCode']) ? $get['authCode'] : '';
  204. $session = Yii::$app->session;
  205. if (isset($session['replaceAdminAuthCode']) && $authCode == $session['replaceAdminAuthCode']) {
  206. util::successInfo('');
  207. }
  208. util::failInfo('验证号不正确');
  209. }
  210. public function actionAjaxExecReplace()
  211. {
  212. $post = Yii::$app->request->post();
  213. $replaceAdminAuthCode = isset($post['replaceAdminAuthCode']) ? $post['replaceAdminAuthCode'] : '';
  214. $code = isset($post['code']) ? $post['code'] : '';
  215. $mobile = isset($post['mobile']) ? $post['mobile'] : '';
  216. $password = isset($post['password']) ? $post['password'] : '';
  217. $session = Yii::$app->session;
  218. if (isset($session['replaceAdminAuthCode']) == false || $session['replaceAdminAuthCode'] != $replaceAdminAuthCode) {
  219. util::stop('验证码已经失效了');
  220. }
  221. if (isset($session['authCode']) == false || $session['authCode'] != $code) {
  222. util::stop('验证码已经失效');
  223. }
  224. $preAdmin = xhAdminToMerchantService::getAdminByMerchantId($this->merchantId);
  225. $preAdminId = $preAdmin['id'];
  226. $newAdminId = $this->adminId;
  227. $relation = xhAdminToMerchantService::getByAdminId($newAdminId);
  228. if (!empty($relation)) {
  229. util::stop('您的微信已经绑定商家了,请先解除绑定');
  230. }
  231. //删除原有管理关系
  232. xhAdminToMerchantService::delByAdminId($preAdminId);
  233. //更新原管理员信息
  234. xhAdminService::updateById($preAdminId, ['mobile' => '', 'password' => '']);
  235. //添加新管理员
  236. xhAdminToMerchantService::add(['adminId' => $newAdminId, 'merchantId' => $this->merchantId, 'createTime' => date("Y-m-d H:i:s"), 'receiveMsg' => 1]);
  237. //更新管理员的信息
  238. xhAdminService::updateById($newAdminId, ['mobile' => $mobile, 'password' => md5($password)]);
  239. unset($session['replaceAdminAuthCode']);
  240. unset($session['authCode']);
  241. util::successInfo();
  242. }
  243. public function actionReplaceResult()
  244. {
  245. return $this->renderPartial('replaceResult');
  246. }
  247. //退出 shish 2019.11.24
  248. public function actionLogout()
  249. {
  250. $token = httpUtil::getToken();
  251. $r = jwt::delToken($token, $this->adminId);
  252. if ($r) {
  253. util::ok();
  254. }
  255. util::fail('没有退出');
  256. }
  257. }