123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- namespace app\admin\controller\csmadmin;
- use fast\Http;
- use think\Hook;
- use fast\Random;
- use think\Session;
- use addons\csmadmin\library\DDUtils;
- use addons\csmadmin\library\WxUtils;
- use addons\csmadmin\library\CsmUtils;
- use app\common\library\Ems as Emslib;
- use app\common\library\Sms as Smslib;
- use addons\csmadmin\library\CsmNotify;
- use addons\csmadmin\library\CsmBackend;
- use addons\csmadmin\library\CsmContants;
- use addons\csmadmin\library\CsmadminUtils;
- use addons\csmadmin\library\service\AdminService;
- /**
- * Tables
- * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/index
- * 手机、钉钉和微信绑定变更
- */
- class Csmadminapp extends CsmBackend
- {
- protected $noNeedLogin = [
- 'login',
- 'getWxByOpenID',
- 'modifywxdialog',
- 'modifydddialog',
- 'sendWxCodeAjax',
- 'sendEmailCodeAjax',
- 'sendMobileCodeAjax',
- 'modifyemaildialog',
- 'modifymobiledialog',
- 'getWeixinh5mobileurl',
- 'h5loginbycode'
- ];
- protected $noNeedRight = [
- 'login',
- 'getWxByOpenID',
- 'modifywxdialog',
- 'modifydddialog',
- 'sendWxCodeAjax',
- 'sendEmailCodeAjax',
- 'sendMobileCodeAjax',
- 'modifyemaildialog',
- 'modifymobiledialog',
- 'getWeixinh5mobileurl',
- 'h5loginbycode'
- ];
- protected $model = null;
- protected $relationSearch = true;
- public function _initialize()
- {
- parent::_initialize();
- }
- public function login()
- {
- $username2 = $this->csmreq("username2", true);
- $ff = strpos($username2, "@");
- $service = new AdminService();
- if ($ff === false) {
- // 手机号码
- if (strlen($username2) == 11) {
- $row = $service->getRowByMobile($username2);
- $username2 = $row ? $row->username : $username2;
- }
- } else {
- // 邮箱地址
- $row = $service->getRowByEmail($username2);
- $username2 = $row ? $row->username : $username2;
- }
- // 如果帐号不存在,且待审核中,则报错
- if (true) {
- $service = new AdminService();
- $row = $service->getRowByUsername($username2);
- if (!$row) {
- $dao2 = new \app\admin\model\csmadmin\Adminapply();
- $row2 = $dao2->where('mobile|email', '=', $username2)
- ->order("id", "desc")
- ->find();
- if ($row2 && $row2->auditstatus == '0') {
- CsmUtils::error("您的帐号正在审核中,暂无法登陆,请耐心等待审核。");
- }
- }
- }
- $this->success('', '', array(
- "username" => $username2
- ));
- }
- /*
- * 修改钉钉Dialog
- * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/modifydddialog
- */
- public function modifydddialog()
- {
- $config = get_addon_config(CsmContants::$ADDONS);
- $ddappid = $config['ddappid'];
- $this->assign("ddappid", $ddappid);
- $this->assign("hosturl", $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"]);
- return $this->view->fetch();
- }
- /**
- * 钉钉扫码,根据$loginTmpCode获取钉钉信息
- *
- * @see https://ding-doc.dingtalk.com/doc#/serverapi2/kymkv6 row返回内容:{
- * "errcode": 0,
- * "errmsg": "ok",
- * "user_info": {
- * "nick": "张三",
- * "openid": "liSii8KCxxxxx",
- * "unionid": "7Huu46kk"
- * }
- * }
- */
- public function sendddlogintmpcode()
- {
- $loginTmpCode = $this->csmreq("code", true);
- $row = DDUtils::getUserInfoByLoginTmpCode($loginTmpCode);
- $this->assign("row", $row);
- return $this->view->fetch();
- }
- /*
- * 修改微信Dialog
- * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/modifywxdialog
- */
- public function modifywxdialog()
- {
- $sceneId = time();
- $wxqdurl = WxUtils::qrcodeCreate($sceneId);
- $this->assign("wxqdurl", $wxqdurl);
- $this->assign("scene_id", $sceneId);
- return $this->view->fetch();
- }
- // //http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/parseWxCode
- // public function parseWxCode(){
- // $postStr = "
- // <xml><ToUserName><![CDATA[gh_946a4387f177]]></ToUserName>
- // <FromUserName><![CDATA[oB_aRt-Jp4QAPHzin_34LBOMhybE]]></FromUserName>
- // <CreateTime>1586088016</CreateTime>
- // <MsgType><![CDATA[event]]></MsgType>
- // <Event><![CDATA[SCAN]]></Event>
- // <EventKey><![CDATA[1586087903]]></EventKey>
- // <Ticket><![CDATA[gQE38TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyNkY1Nk5YN1NhaFQxeHdwYk51MXgAAgTgx4leAwSAUQEA]]></Ticket>
- // </xml>
- // ";
- // $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- // $openid = $postObj->FromUserName; //openid
- // $event = strtolower($postObj->Event);
- // $eventKey = (int)($postObj->EventKey);
- // var_dump($eventKey);
- // }
- public function getWxByOpenID()
- {
- $sceneId = $this->csmreq("scene_id", true);
- $dao = new \app\admin\model\csmadmin\Wxscan();
- $row = $dao->where("scene_id", "=", $sceneId)->find();
- if ($row != null) {
- $this->success('', null, array(
- 'row' => $row
- ));
- } else {
- $this->error('');
- }
- }
- /*
- * 修改手机号码Dialog
- * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/modifymobiledialog
- */
- public function modifymobiledialog()
- {
- if ($this->request->isAjax()) {
- $mobile = $this->csmreq("csmadminmobile", true);
- $event = $this->csmreq("csmadminevent", true);
- $ccode = $this->csmreq("csmadminccode", true);
- $sendccode = Session::get(CsmadminUtils::getSessionKey($mobile, $event));
- if ($sendccode == $ccode) {
- $this->success();
- } else {
- $this->error('验证码错误!');
- }
- }
- return $this->view->fetch();
- }
- // 发送消息
- public function sendMobileCodeAjax()
- {
- $mobile = $this->csmreq("mobile", true);
- $event = $this->csmreq("event", true);
- $captcha = $this->csmreq("captcha", true);
- $captchaResult = \think\Validate::is($captcha, 'captcha');
- if (!$captchaResult) {
- $this->error('验证码不正确');
- }
- if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('手机号不正确'));
- }
- $last = Smslib::get($mobile, $event);
- if ($last && time() - $last['createtime'] < 60) {
- $this->error(__('发送频繁'));
- }
- $ipSendTotal = \app\common\model\Sms::where([
- 'ip' => $this->request->ip()
- ])->whereTime('createtime', '-1 hours')->count();
- if ($ipSendTotal >= 5) {
- $this->error(__('发送频繁'));
- }
- if (!Hook::get('sms_send')) {
- $this->error(__('请在后台插件管理安装短信验证插件'));
- }
- $ccode = Random::numeric(4);
- Session::set(CsmadminUtils::getSessionKey($mobile, $event), $ccode);
- $ret = CsmNotify::sendccodesms($mobile, $ccode, $event);
- if ($ret) {
- $this->success(__('发送成功'));
- } else {
- $this->error(__('发送失败,请检查短信配置是否正确'));
- }
- }
- /*
- * 修改邮箱Dialog
- * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/modifyemaildialog
- */
- public function modifyemaildialog()
- {
- if ($this->request->isAjax()) {
- $email = $this->csmreq("csmadminemail", true);
- $event = $this->csmreq("csmadminevent", true);
- $ccode = $this->csmreq("csmadminccode", true);
- $sendccode = Session::get(CsmadminUtils::getSessionKey($email, $event));
- if ($sendccode == $ccode) {
- $this->success();
- } else {
- $this->error('验证码错误!');
- }
- }
- return $this->view->fetch();
- }
- // 发送消息
- public function sendEmailCodeAjax()
- {
- $email = $this->csmreq("email", true);
- $event = $this->csmreq("event", true);
- $captcha = $this->csmreq("captcha", true);
- $captchaResult = \think\Validate::is($captcha, 'captcha');
- if (!$captchaResult) {
- $this->error('验证码不正确');
- }
- $last = Emslib::get($email, $event);
- if ($last && time() - $last['createtime'] < 60) {
- $this->error(__('发送频繁'));
- }
- $ccode = mt_rand(1000, 9999);
- $obj = \app\common\library\Email::instance();
- $result = $obj->to($email)
- ->subject('验证码')
- ->message("你的验证码是:" . $ccode)
- ->send();
- if ($result) {
- Session::set(CsmadminUtils::getSessionKey($email, $event), $ccode);
- $this->success(__('发送成功'));
- } else {
- $this->error(__('发送失败,请检查邮件配置是否正确'));
- }
- }
- /**
- * 获取是否微信登录,并返回微信appid(用于在微信容器中,自动登录)
- *
- * v1.5.0 增加后台管理功能微信中自动登录
- * @return void
- */
- public function getWeixinh5mobileurl()
- {
- $config = get_addon_config(CsmContants::$ADDONS);
- $haswxfield = $config["haswxfield"];
- $wxappid = $config["wxappid"];
- //var_dump(['haswxfield' => $haswxfield, 'wxappid' => $wxappid]);
- $this->success('', null, ['haswxfield' => $haswxfield, 'wxappid' => $wxappid]);
- }
- /**
- * 微信H5,通过code登录
- *
- * v1.5.0 增加后台管理功能微信中自动登录
- *
- * @see https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
- */
- public function h5loginbycode()
- {
- $code = $this->csmreq('code', true);
- $config = get_addon_config(CsmContants::$ADDONS);
- $appid = $config['wxappid']; // $config['cloginwxappid'];
- $appsecret = $config['wxappsecret']; // $config['cloginwxappsecret'];
- $weixinurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
- $weixin = Http::get($weixinurl);
- $weixinsr = json_decode($weixin,true);
-
- if($weixinsr!=null && isset($weixinsr['openid'])){
- $dao = new \app\admin\model\csmadmin\Admin();
- $row = $dao->where('wxuserid','=',$weixinsr['openid'])->where('status','=','normal')->find();
- if($row){
- $service = new AdminService();
- $row = $service->getRowById($row->faadmin_id);
- if ($row) {
- $service->directLogin($row);
- $this->success();
- } else {
- $this->error('帐号不存在,请重新登录!');
- }
- $this->success('', null, ['row' => $row]);
- }
- }
- $this->error('帐号不存在,请重新登录!');
-
- }
- }
|