AdminController.php 8.5 KB

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