Auth.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace app\common\controller;
  3. use app\BaseController;
  4. use app\common\api\ChuanglanSmsApi;
  5. use app\common\api\EnterpriseApi;
  6. use app\common\model\MessageRecord;
  7. use app\common\model\TalentChecklog;
  8. use think\facade\Cache;
  9. use app\common\validate\Enterprise;
  10. use think\exception\ValidateException;
  11. use think\facade\Request;
  12. use app\common\api\UploadApi;
  13. class Auth extends BaseController
  14. {
  15. public function register(){
  16. $msg = "";
  17. if($this->request->isPost()){
  18. $source = intval($this->request['source']);
  19. $response_object = new \StdClass();
  20. $special = \StrUtil::getRequestDecodeParam($this->request,'special');
  21. $data = [
  22. 'username' => \StrUtil::getRequestDecodeParam($this->request,'username'),
  23. 'password' => \StrUtil::getRequestDecodeParam($this->request,'password'),
  24. 're_password' => \StrUtil::getRequestDecodeParam($this->request,'re_password'),
  25. 'name' => \StrUtil::getRequestDecodeParam($this->request,'name'),
  26. 'idCard' => \StrUtil::getRequestDecodeParam($this->request,'idCard'),
  27. 'agentName' => \StrUtil::getRequestDecodeParam($this->request,'agentName'),
  28. 'agentPhone' => \StrUtil::getRequestDecodeParam($this->request,'agentPhone'),
  29. 'verificationCode' => \StrUtil::getRequestDecodeParam($this->request,'verificationCode'),
  30. 'legal' => \StrUtil::getRequestDecodeParam($this->request,'legal'),
  31. 'street' => \StrUtil::getRequestDecodeParam($this->request,'street'),
  32. 'address' => \StrUtil::getRequestDecodeParam($this->request,'address'),
  33. 'type' => intval($this->request['type']),
  34. 'enterpriseTag' => \StrUtil::getRequestDecodeParam($this->request,'enterpriseTag'),
  35. 'enterpriseType' => \StrUtil::getRequestDecodeParam($this->request,'enterpriseType'),
  36. 'agentEmail' => \StrUtil::getRequestDecodeParam($this->request,'agentEmail'),
  37. 'ephone' => \StrUtil::getRequestDecodeParam($this->request,'ephone'),
  38. 'industryFieldNew' => \StrUtil::getRequestDecodeParam($this->request,'industryFieldNew'),
  39. 'industryFieldOld' => \StrUtil::getRequestDecodeParam($this->request,'industryFieldOld'),
  40. 'bankCard' => \StrUtil::getRequestDecodeParam($this->request,'bankCard'),
  41. 'bank' => \StrUtil::getRequestDecodeParam($this->request,'bank'),
  42. 'bankNetwork' => \StrUtil::getRequestDecodeParam($this->request,'bankNetwork')
  43. ];
  44. try {
  45. if(stripos($data['name'],"(")){
  46. $data['name'] = str_replace('(','(',$data['name']);
  47. }
  48. if(stripos($data['name'],")")){
  49. $data['name'] = str_replace(')',')',$data['name']);
  50. }
  51. if($special == 'qiye'){
  52. if($data['type'] == 1){
  53. validate(Enterprise::class)->batch(true)->scene('add')->check($data);
  54. }else{
  55. validate(Enterprise::class)->batch(true)->scene('jc_add')->check($data);
  56. }
  57. $data['special'] = 0;
  58. }else{
  59. $data['type'] = 1;
  60. validate(Enterprise::class)->batch(true)->scene('sy_add')->check($data);
  61. $data['special'] = 1;
  62. }
  63. //检验验证码
  64. $codeResult = MessageRecord::where('smsType',1)->where('phone',$data['agentPhone'])->order('createTime','desc')->find();
  65. if(!$codeResult){
  66. $response_object->code = 500;
  67. $response_object->msg = '请先发送验证码';
  68. return \StrUtil::back($response_object,"Register.epCallBack");
  69. }
  70. if(time() - strtotime($codeResult['createTime']) > 300){
  71. $response_object->code = 500;
  72. $response_object->msg = '验证码过期,请重新发送';
  73. return \StrUtil::back($response_object,"Register.epCallBack");
  74. }
  75. $uploadapi = new UploadApi();
  76. $files = $this->request->file();
  77. //检验附件 营业执照
  78. if(array_key_exists('imgurl',$files)){
  79. $upload_result = $uploadapi->uploadOne($this->request->file('imgurl'),'image');
  80. if($upload_result->code == 500){
  81. return \StrUtil::back($upload_result,"Register.epCallBack");
  82. }
  83. $imgurl = $upload_result->filepath;
  84. }else{
  85. $imgurl = '';
  86. }
  87. //检验附件 开户许可证
  88. if(array_key_exists('bankImg',$files)){
  89. $upload_result1 = $uploadapi->uploadOne($this->request->file('bankImg'),'image');
  90. if($upload_result->code == 500){
  91. return \StrUtil::back($upload_result1,"Register.epCallBack");
  92. }
  93. $bankImg = $upload_result1->filepath;
  94. }else{
  95. $bankImg = '';
  96. }
  97. //检验附件 行业领域佐证材料
  98. if(array_key_exists('domainImg',$files)){
  99. $upload_result2 = $uploadapi->uploadOne($this->request->file('domainImg'),'image');
  100. if($upload_result2->code == 500){
  101. return \StrUtil::back($upload_result2,"Register.epCallBack");
  102. }
  103. $domainImg = $upload_result2->filepath;
  104. }else{
  105. $domainImg = '';
  106. }
  107. //检验附件 人才联络员备案表
  108. if(array_key_exists('beian',$files)){
  109. $upload_result3 = $uploadapi->uploadOne($this->request->file('beian'),'system');
  110. if($upload_result->code == 500){
  111. return \StrUtil::back($upload_result3,"Register.epCallBack");
  112. }
  113. $beian = $upload_result3->filepath;
  114. }else{
  115. $beian = '';
  116. }
  117. $data['id'] = getStringId();
  118. $data['password'] = hash('md5',$data['password']);
  119. $data['source'] = $source;
  120. $data['imgurl'] = $imgurl;
  121. $data['bankImg'] = $bankImg;
  122. $data['domainImg'] = $domainImg;
  123. $data['beian'] = $beian;
  124. $data['createTime'] = date("Y-m-d H:i:s",time());
  125. $data['updateTime'] = date("Y-m-d H:i:s",time());
  126. $data['active'] = 1;
  127. $data['checkState'] = 1;
  128. \app\common\model\Enterprise::create($data);
  129. TalentChecklog::create([
  130. 'id' => getStringId(),
  131. 'mainId' => $data['id'],
  132. 'type' => 10,
  133. 'typeField' => null,
  134. 'active' => 1,
  135. 'state' => 1,
  136. 'step' => 100,
  137. 'stateChange' => '用户提交',
  138. 'description' => '用户注册',
  139. 'createTime' => date("Y-m-d H:i:s",time()),
  140. 'createUser' => '用户'
  141. ]);
  142. $response_object->code = 200;
  143. $response_object->msg = '注册成功';
  144. return \StrUtil::back($response_object,"Register.epCallBack");
  145. } catch (ValidateException $e){
  146. $error = $e->getError();
  147. $response_object->code = 500;
  148. $response_object->msg = array_pop($error);
  149. return \StrUtil::back($response_object,"Register.epCallBack");
  150. }
  151. }
  152. return view("", ["msg" => $msg]);
  153. }
  154. public function enterprise_edit()
  155. {
  156. $temp = session('temp');
  157. if(empty($temp)){
  158. return redirect("/");
  159. }
  160. $ep = EnterpriseApi::getOne(session('temp')['uid']);
  161. if($this->request->isPost()){
  162. $response_object = new \StdClass();
  163. $data = [
  164. 'name' => \StrUtil::getRequestDecodeParam($this->request,'name'),//单位名称
  165. 'idCard' => \StrUtil::getRequestDecodeParam($this->request,'idCard'),//统一社会信用代码
  166. 'agentName' => \StrUtil::getRequestDecodeParam($this->request,'agentName'),//人才联络员
  167. 'legal' => \StrUtil::getRequestDecodeParam($this->request,'legal'),//法人
  168. 'street' => \StrUtil::getRequestDecodeParam($this->request,'street'),//镇街
  169. 'address' => \StrUtil::getRequestDecodeParam($this->request,'address'),//地址
  170. 'type' => intval($this->request['type']),
  171. 'enterpriseTag' => \StrUtil::getRequestDecodeParam($this->request,'enterpriseTag'),//单位标签
  172. 'enterpriseType' => \StrUtil::getRequestDecodeParam($this->request,'enterpriseType'),//单位类型
  173. 'agentEmail' => \StrUtil::getRequestDecodeParam($this->request,'agentEmail'),//邮箱
  174. 'ephone' => \StrUtil::getRequestDecodeParam($this->request,'ephone'),//单位电话
  175. 'industryFieldNew' => \StrUtil::getRequestDecodeParam($this->request,'industryFieldNew'),//产业领域
  176. 'industryFieldOld' => \StrUtil::getRequestDecodeParam($this->request,'industryFieldOld'),//行业领域
  177. 'bankCard' => \StrUtil::getRequestDecodeParam($this->request,'bankCard'),//银行
  178. 'bank' => \StrUtil::getRequestDecodeParam($this->request,'bank'),//开户行
  179. 'bankNetwork' => \StrUtil::getRequestDecodeParam($this->request,'bankNetwork')//网点
  180. ];
  181. try {
  182. if(stripos($data['name'],"(")){
  183. $data['name'] = str_replace('(','(',$data['name']);
  184. }
  185. if(stripos($data['name'],")")){
  186. $data['name'] = str_replace(')',')',$data['name']);
  187. }
  188. $data['id'] = $ep->id;
  189. validate(\app\common\validate\Enterprise::class)->batch(true)->scene('check_common')->check($data);
  190. $data['checkState'] = 1;
  191. $ep->update($data);
  192. $response_object->code = 200;
  193. $response_object->msg = '修改成功';
  194. return \StrUtil::back($response_object,"Register.epCallBack");
  195. }catch (ValidateException $e){
  196. $error = $e->getError();
  197. $response_object->code = 500;
  198. $response_object->msg = array_pop($error);
  199. return \StrUtil::back($response_object,"Register.epCallBack");
  200. }
  201. }
  202. $msg = session('temp')['msg'];
  203. return view("",['msg' => $msg, 'ep' => $ep]);
  204. }
  205. public function findBackPassword(){
  206. $type = intval($this->request['type']);
  207. if(\StrUtil::isEmpOrNull($type)){
  208. return json(['msg' => '请选择账号类型','code' => 500]);
  209. }
  210. $username = \StrUtil::getRequestDecodeParam($this->request,'username');
  211. if(\StrUtil::isEmpOrNull($username)){
  212. return json(['msg' => '请填写账号','code' => 500]);
  213. }
  214. $password = \StrUtil::getRequestDecodeParam($this->request,'password');
  215. if(\StrUtil::isEmpOrNull($password)){
  216. return json(['msg' => '请填写新密码','code' => 500]);
  217. }
  218. $rePassword = \StrUtil::getRequestDecodeParam($this->request,'rePassword');
  219. if(\StrUtil::isEmpOrNull($rePassword)){
  220. return json(['msg' => '请填写重复新密码','code' => 500]);
  221. }
  222. if($password != $rePassword){
  223. return json(['msg' => '两次密码填写不一致','code' => 500]);
  224. }
  225. if(!preg_match("/^(?=.*\d)(?=.*[A-Za-z]).{8,}$/",$password)){
  226. return json(['msg' => '密码必须包含字母、数字、特殊字符且长度超过8位','code' => 500]);
  227. }
  228. $phone = \StrUtil::getRequestDecodeParam($this->request,'phone');
  229. if(\StrUtil::isEmpOrNull($phone)){
  230. return json(['msg' => '请填写手机号','code' => 500]);
  231. }
  232. $verificationCode = \StrUtil::getRequestDecodeParam($this->request,'verificationCode');
  233. if(\StrUtil::isEmpOrNull($verificationCode)){
  234. return json(['msg' => '请填写验证码','code' => 500]);
  235. }
  236. //检验验证码
  237. $codeResult = MessageRecord::where('smsType',1)->where('phone',$phone)->order('createTime','desc')->find();
  238. if(!$codeResult || (time() - strtotime($codeResult['sendingDate']) > 300) ){
  239. return json(['msg' => '请先发送验证码','code' => 500]);
  240. }
  241. if($codeResult['params'] != $verificationCode){
  242. return json(['msg' => '验证码不正确','code' => 500]);
  243. }
  244. $password = hash('md5',$password);
  245. if($type == 1){
  246. $count = \app\common\model\Enterprise::where('username',$username)->where('agentPhone',$phone)->count();
  247. if($count == 0){
  248. return json(['msg' => '未找到相关账号,请仔细核对账号及手机号信息!','code' => 500]);
  249. }
  250. if($count > 1){
  251. return json(['msg' => '找到多个账户,请联系相关单位处理!','code' => 500]);
  252. }
  253. $ep = \app\common\model\Enterprise::where('username',$username)->where('agentPhone',$phone)->find();
  254. $ep->password = $password;
  255. $ep->save();
  256. }else{
  257. //个人账户找回密码TODO
  258. }
  259. return json(['msg' => '找回密码成功!','code' => 200]);
  260. }
  261. public function verificationCode()
  262. {
  263. $phone = $this->request["phone"];
  264. $type = $this->request["type"];
  265. //校验手机号码是否为空
  266. if(\StrUtil::isEmpOrNull($phone)) {
  267. return json(["msg" => "请填写手机号码!"],500);
  268. }
  269. if (\StrUtil::isEmpOrNull($type)) {
  270. return json(["msg" => "请填写手机号码!"],500);
  271. }
  272. //校验手机号码格式是否正确
  273. if(\StrUtil::isMoblePhone($phone)) {
  274. return json(["msg" => "请填写正确的手机号码!"],500);
  275. }
  276. $record = Cache::get("verify_{$type}_{$phone}");
  277. if($record){
  278. $time = time();
  279. if($time - $record <= 60){
  280. return json(["msg" => "一分钟内请勿频繁发送短信!"],500);
  281. }
  282. }
  283. $code = '';
  284. for ($i = 1;$i <= 6;$i++){
  285. $code .= rand(0,9);
  286. }
  287. //$code = 999999;
  288. $template = "【晋江市人才服务平台】尊敬的用户,您的短信验证码为{$code},5分钟内有效。若非本人操作请忽略。";
  289. $smsapi = new ChuanglanSmsApi();
  290. $result = $smsapi->sendSMS($phone,$template);
  291. $result = json_decode($result,true);
  292. $id = getStringId();
  293. $record_data = [
  294. 'id' => $id,
  295. 'bizId' => $id,
  296. 'type' => 2,
  297. 'smsType' => 1,
  298. 'phone' => $phone,
  299. 'params' => $code,
  300. 'templateCode' => $template,
  301. 'state' => $result['code'] == 0 ? 2 : 3,
  302. 'sendingDate' => date("Y-m-d H:i:s",time()),
  303. 'createTime' => date("Y-m-d H:i:s",time()),
  304. 'msg' => $result['errorMsg']
  305. ];
  306. MessageRecord::create($record_data);
  307. if($result['code'] == 0){
  308. Cache::set("verify_{$type}_{$phone}",time());
  309. return json(["msg" => '验证码发送成功'],200);
  310. }else{
  311. return json(["msg" => '验证码发送失败'],500);
  312. }
  313. }
  314. }