Auth.php 15 KB

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