Csmadminapp.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace app\admin\controller\csmadmin;
  3. use fast\Http;
  4. use think\Hook;
  5. use fast\Random;
  6. use think\Session;
  7. use addons\csmadmin\library\DDUtils;
  8. use addons\csmadmin\library\WxUtils;
  9. use addons\csmadmin\library\CsmUtils;
  10. use app\common\library\Ems as Emslib;
  11. use app\common\library\Sms as Smslib;
  12. use addons\csmadmin\library\CsmNotify;
  13. use addons\csmadmin\library\CsmBackend;
  14. use addons\csmadmin\library\CsmContants;
  15. use addons\csmadmin\library\CsmadminUtils;
  16. use addons\csmadmin\library\service\AdminService;
  17. /**
  18. * Tables
  19. * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/index
  20. * 手机、钉钉和微信绑定变更
  21. */
  22. class Csmadminapp extends CsmBackend
  23. {
  24. protected $noNeedLogin = [
  25. 'login',
  26. 'getWxByOpenID',
  27. 'modifywxdialog',
  28. 'modifydddialog',
  29. 'sendWxCodeAjax',
  30. 'sendEmailCodeAjax',
  31. 'sendMobileCodeAjax',
  32. 'modifyemaildialog',
  33. 'modifymobiledialog',
  34. 'getWeixinh5mobileurl',
  35. 'h5loginbycode'
  36. ];
  37. protected $noNeedRight = [
  38. 'login',
  39. 'getWxByOpenID',
  40. 'modifywxdialog',
  41. 'modifydddialog',
  42. 'sendWxCodeAjax',
  43. 'sendEmailCodeAjax',
  44. 'sendMobileCodeAjax',
  45. 'modifyemaildialog',
  46. 'modifymobiledialog',
  47. 'getWeixinh5mobileurl',
  48. 'h5loginbycode'
  49. ];
  50. protected $model = null;
  51. protected $relationSearch = true;
  52. public function _initialize()
  53. {
  54. parent::_initialize();
  55. }
  56. public function login()
  57. {
  58. $username2 = $this->csmreq("username2", true);
  59. $ff = strpos($username2, "@");
  60. $service = new AdminService();
  61. if ($ff === false) {
  62. // 手机号码
  63. if (strlen($username2) == 11) {
  64. $row = $service->getRowByMobile($username2);
  65. $username2 = $row ? $row->username : $username2;
  66. }
  67. } else {
  68. // 邮箱地址
  69. $row = $service->getRowByEmail($username2);
  70. $username2 = $row ? $row->username : $username2;
  71. }
  72. // 如果帐号不存在,且待审核中,则报错
  73. if (true) {
  74. $service = new AdminService();
  75. $row = $service->getRowByUsername($username2);
  76. if (!$row) {
  77. $dao2 = new \app\admin\model\csmadmin\Adminapply();
  78. $row2 = $dao2->where('mobile|email', '=', $username2)
  79. ->order("id", "desc")
  80. ->find();
  81. if ($row2 && $row2->auditstatus == '0') {
  82. CsmUtils::error("您的帐号正在审核中,暂无法登陆,请耐心等待审核。");
  83. }
  84. }
  85. }
  86. $this->success('', '', array(
  87. "username" => $username2
  88. ));
  89. }
  90. /*
  91. * 修改钉钉Dialog
  92. * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/modifydddialog
  93. */
  94. public function modifydddialog()
  95. {
  96. $config = get_addon_config(CsmContants::$ADDONS);
  97. $ddappid = $config['ddappid'];
  98. $this->assign("ddappid", $ddappid);
  99. $this->assign("hosturl", $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"]);
  100. return $this->view->fetch();
  101. }
  102. /**
  103. * 钉钉扫码,根据$loginTmpCode获取钉钉信息
  104. *
  105. * @see https://ding-doc.dingtalk.com/doc#/serverapi2/kymkv6 row返回内容:{
  106. * "errcode": 0,
  107. * "errmsg": "ok",
  108. * "user_info": {
  109. * "nick": "张三",
  110. * "openid": "liSii8KCxxxxx",
  111. * "unionid": "7Huu46kk"
  112. * }
  113. * }
  114. */
  115. public function sendddlogintmpcode()
  116. {
  117. $loginTmpCode = $this->csmreq("code", true);
  118. $row = DDUtils::getUserInfoByLoginTmpCode($loginTmpCode);
  119. $this->assign("row", $row);
  120. return $this->view->fetch();
  121. }
  122. /*
  123. * 修改微信Dialog
  124. * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/modifywxdialog
  125. */
  126. public function modifywxdialog()
  127. {
  128. $sceneId = time();
  129. $wxqdurl = WxUtils::qrcodeCreate($sceneId);
  130. $this->assign("wxqdurl", $wxqdurl);
  131. $this->assign("scene_id", $sceneId);
  132. return $this->view->fetch();
  133. }
  134. // //http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/parseWxCode
  135. // public function parseWxCode(){
  136. // $postStr = "
  137. // <xml><ToUserName><![CDATA[gh_946a4387f177]]></ToUserName>
  138. // <FromUserName><![CDATA[oB_aRt-Jp4QAPHzin_34LBOMhybE]]></FromUserName>
  139. // <CreateTime>1586088016</CreateTime>
  140. // <MsgType><![CDATA[event]]></MsgType>
  141. // <Event><![CDATA[SCAN]]></Event>
  142. // <EventKey><![CDATA[1586087903]]></EventKey>
  143. // <Ticket><![CDATA[gQE38TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyNkY1Nk5YN1NhaFQxeHdwYk51MXgAAgTgx4leAwSAUQEA]]></Ticket>
  144. // </xml>
  145. // ";
  146. // $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  147. // $openid = $postObj->FromUserName; //openid
  148. // $event = strtolower($postObj->Event);
  149. // $eventKey = (int)($postObj->EventKey);
  150. // var_dump($eventKey);
  151. // }
  152. public function getWxByOpenID()
  153. {
  154. $sceneId = $this->csmreq("scene_id", true);
  155. $dao = new \app\admin\model\csmadmin\Wxscan();
  156. $row = $dao->where("scene_id", "=", $sceneId)->find();
  157. if ($row != null) {
  158. $this->success('', null, array(
  159. 'row' => $row
  160. ));
  161. } else {
  162. $this->error('');
  163. }
  164. }
  165. /*
  166. * 修改手机号码Dialog
  167. * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/modifymobiledialog
  168. */
  169. public function modifymobiledialog()
  170. {
  171. if ($this->request->isAjax()) {
  172. $mobile = $this->csmreq("csmadminmobile", true);
  173. $event = $this->csmreq("csmadminevent", true);
  174. $ccode = $this->csmreq("csmadminccode", true);
  175. $sendccode = Session::get(CsmadminUtils::getSessionKey($mobile, $event));
  176. if ($sendccode == $ccode) {
  177. $this->success();
  178. } else {
  179. $this->error('验证码错误!');
  180. }
  181. }
  182. return $this->view->fetch();
  183. }
  184. // 发送消息
  185. public function sendMobileCodeAjax()
  186. {
  187. $mobile = $this->csmreq("mobile", true);
  188. $event = $this->csmreq("event", true);
  189. $captcha = $this->csmreq("captcha", true);
  190. $captchaResult = \think\Validate::is($captcha, 'captcha');
  191. if (!$captchaResult) {
  192. $this->error('验证码不正确');
  193. }
  194. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  195. $this->error(__('手机号不正确'));
  196. }
  197. $last = Smslib::get($mobile, $event);
  198. if ($last && time() - $last['createtime'] < 60) {
  199. $this->error(__('发送频繁'));
  200. }
  201. $ipSendTotal = \app\common\model\Sms::where([
  202. 'ip' => $this->request->ip()
  203. ])->whereTime('createtime', '-1 hours')->count();
  204. if ($ipSendTotal >= 5) {
  205. $this->error(__('发送频繁'));
  206. }
  207. if (!Hook::get('sms_send')) {
  208. $this->error(__('请在后台插件管理安装短信验证插件'));
  209. }
  210. $ccode = Random::numeric(4);
  211. Session::set(CsmadminUtils::getSessionKey($mobile, $event), $ccode);
  212. $ret = CsmNotify::sendccodesms($mobile, $ccode, $event);
  213. if ($ret) {
  214. $this->success(__('发送成功'));
  215. } else {
  216. $this->error(__('发送失败,请检查短信配置是否正确'));
  217. }
  218. }
  219. /*
  220. * 修改邮箱Dialog
  221. * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadminapp/modifyemaildialog
  222. */
  223. public function modifyemaildialog()
  224. {
  225. if ($this->request->isAjax()) {
  226. $email = $this->csmreq("csmadminemail", true);
  227. $event = $this->csmreq("csmadminevent", true);
  228. $ccode = $this->csmreq("csmadminccode", true);
  229. $sendccode = Session::get(CsmadminUtils::getSessionKey($email, $event));
  230. if ($sendccode == $ccode) {
  231. $this->success();
  232. } else {
  233. $this->error('验证码错误!');
  234. }
  235. }
  236. return $this->view->fetch();
  237. }
  238. // 发送消息
  239. public function sendEmailCodeAjax()
  240. {
  241. $email = $this->csmreq("email", true);
  242. $event = $this->csmreq("event", true);
  243. $captcha = $this->csmreq("captcha", true);
  244. $captchaResult = \think\Validate::is($captcha, 'captcha');
  245. if (!$captchaResult) {
  246. $this->error('验证码不正确');
  247. }
  248. $last = Emslib::get($email, $event);
  249. if ($last && time() - $last['createtime'] < 60) {
  250. $this->error(__('发送频繁'));
  251. }
  252. $ccode = mt_rand(1000, 9999);
  253. $obj = \app\common\library\Email::instance();
  254. $result = $obj->to($email)
  255. ->subject('验证码')
  256. ->message("你的验证码是:" . $ccode)
  257. ->send();
  258. if ($result) {
  259. Session::set(CsmadminUtils::getSessionKey($email, $event), $ccode);
  260. $this->success(__('发送成功'));
  261. } else {
  262. $this->error(__('发送失败,请检查邮件配置是否正确'));
  263. }
  264. }
  265. /**
  266. * 获取是否微信登录,并返回微信appid(用于在微信容器中,自动登录)
  267. *
  268. * v1.5.0 增加后台管理功能微信中自动登录
  269. * @return void
  270. */
  271. public function getWeixinh5mobileurl()
  272. {
  273. $config = get_addon_config(CsmContants::$ADDONS);
  274. $haswxfield = $config["haswxfield"];
  275. $wxappid = $config["wxappid"];
  276. //var_dump(['haswxfield' => $haswxfield, 'wxappid' => $wxappid]);
  277. $this->success('', null, ['haswxfield' => $haswxfield, 'wxappid' => $wxappid]);
  278. }
  279. /**
  280. * 微信H5,通过code登录
  281. *
  282. * v1.5.0 增加后台管理功能微信中自动登录
  283. *
  284. * @see https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
  285. */
  286. public function h5loginbycode()
  287. {
  288. $code = $this->csmreq('code', true);
  289. $config = get_addon_config(CsmContants::$ADDONS);
  290. $appid = $config['wxappid']; // $config['cloginwxappid'];
  291. $appsecret = $config['wxappsecret']; // $config['cloginwxappsecret'];
  292. $weixinurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
  293. $weixin = Http::get($weixinurl);
  294. $weixinsr = json_decode($weixin,true);
  295. if($weixinsr!=null && isset($weixinsr['openid'])){
  296. $dao = new \app\admin\model\csmadmin\Admin();
  297. $row = $dao->where('wxuserid','=',$weixinsr['openid'])->where('status','=','normal')->find();
  298. if($row){
  299. $service = new AdminService();
  300. $row = $service->getRowById($row->faadmin_id);
  301. if ($row) {
  302. $service->directLogin($row);
  303. $this->success();
  304. } else {
  305. $this->error('帐号不存在,请重新登录!');
  306. }
  307. $this->success('', null, ['row' => $row]);
  308. }
  309. }
  310. $this->error('帐号不存在,请重新登录!');
  311. }
  312. }