123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- <?php
- namespace app\api\controller;
- use app\api\model\MemberHouseModel;
- use app\api\model\YzmModel;
- use app\common\controller\Api;
- use aliyun\api_demo\SmsDemo;
- use app\api\model\CodeModel;
- use app\api\model\MemberModel;
- use app\api\model\AppDataModel;
- use app\api\controller\Common;
- class Member extends Api
- {
- protected $noNeedLogin = ['yzm','sendCode','register','create_qrcode','login','edit_password'];
- protected $noNeedRight = ['*'];
-
- function yzm($width = 80,$height = 40)
- {
- $str = '0123456789abcdefghizklmnopqrstuvwxyz';
- $code = substr(str_shuffle($str),0,'4');
- $rand=rand(10000,99999);
- $member_id=time().$rand;
-
- $dir = ROOT_PATH . 'public' . DS .'uploads/verification_code_img';
- if(!is_dir($dir)){
- mkdir($dir, 0777, true);
- }
-
- $file_name = ROOT_PATH . 'public' . DS .'uploads/verification_code_img/'.$member_id.'.png';
-
- $domain_name = config('site.cdnurl').'/uploads/verification_code_img/'.$member_id.'.png';
- $img = imagecreatetruecolor($width, $height);
- $black = imagecolorallocate($img, 0x00, 0x00, 0x00);
- $green = imagecolorallocate($img, 0x00, 0xFF, 0x00);
- $white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
- imagefill($img,0,0,$white);
- imagestring($img, 5, 22, 12, $code, $black);
-
- for($i=0;$i<100;$i++) {
- imagesetpixel($img, rand(0, $width) , rand(0, $width) , $black);
- imagesetpixel($img, rand(0, $width) , rand(0, $width) , $green);
- }
-
- imagepng($img,$file_name);
- imagedestroy($img);
- $log = ['code' => $code,
- 'rand' => $member_id,
- 'scene' => '图形验证码',
- 'time' => datetime(time())
- ];
- $addLog = YzmModel::create($log);
- $this->result('success', ['url'=>$domain_name,'rand'=>$member_id], 1);
- }
-
- public function sendCode()
- {
- $mobile = $this->request->post('mobile');
- $rand= $this->request->post('rand');
- $code= $this->request->post('code');
- $scene= $this->request->post('scene');
- if (empty($code)){
- $this->result('请输入图形验证码');
- }
- if (!$mobile || !$rand || !$code) {
- $this->result('参数错误');
- }
- if (empty($scene)){
- $scene=1;
- }
-
- $check_code = CodeModel::where('mobile', $mobile)->where('scene',$scene)
- ->order('id desc')
- ->find();
- $sendTime = strtotime($check_code['time']);
- if (time() - $sendTime < 300) {
- $this->result('5分钟之后才可以发送');
- }
- $get_yzm=YzmModel::where('rand',$rand)->where('status','0')->order('id','desc')->find();
- if (empty($get_yzm)){
- $this->result('图形验证码错误!');
- }
- $sendTime = strtotime($get_yzm['time']);
- if (time() - $sendTime > 300) {
- $this->result('图形验证码已过期');
- }
- if ($code !=$get_yzm['code']){
- $this->result('图形验证码错误');
- }else{
- YzmModel::where('id',$get_yzm['id'])->update(['status'=>'1']);
- }
- $code = rand(1000, 9999);
- $log = ['code' => '1234',
- 'mobile' => $mobile,
- 'scene' =>$scene,
- 'time' => datetime(time())
- ];
- $addLog = CodeModel::create($log);
- if ($addLog) {
- $this->result('验证码已发送', [], 1);
- } else {
- $this->result('验证码发送失败');
- }
- }
-
- public function register()
- {
- $mobile = $this->request->post('mobile');
- $checkCode = $this->request->post('check_code');
- $password = $this->request->post('password');
- if (!$mobile || !$checkCode || !$password) {
- $this->result('参数错误');
- }
-
- $check_code = CodeModel::where('mobile', $mobile)->where('scene','1')
- ->order('id desc')
- ->limit(0, 1)
- ->select();
- if (empty($check_code)) {
- $this->result('验证码错误');
- }
- if ($check_code[0]['code'] != $checkCode) {
- $this->result('验证码错误');
- }
- $sendTime = strtotime($check_code[0]['time']);
- if (time() - $sendTime > 300) {
- $this->result('验证码已过期');
- }
- $get_is_user=MemberModel::where('mobile', $mobile)->field('id')->find();
- if (!empty($get_is_user)){
- $this->result('手机号已经注册,请直接登录或者找回密码');
- }else{
- $where = ['id' => 1];
- $defaultField = ['default_nickname', 'default_avatar'];
- $default = AppDataModel::where($where)->field($defaultField)->find();
- $info = [
- 'avatar' => $default['default_avatar'],
- 'nickname' => substr_replace($mobile,'****',3,4),
- 'mobile' => $mobile,
- 'rands' =>createSalt(),
- 'password'=>md5(trim($password)),
- 'create_time' => datetime(time()),
- ];
- $createId = MemberModel::insertGetId($info);
- if ($createId) {
- $this->result('恭喜你成功注册新天物业', ['user_id' => intval($createId)], 1);
-
-
- } else {
- $this->result('注册失败', null, 0);
- }
- }
- }
-
- public function login()
- {
- $mobile = $this->request->post('mobile');
- $password = $this->request->post('password');
- if (!$mobile || !$password) {
- $this->result('参数错误');
- }
- $user = MemberModel::where('mobile', $mobile)->where('password',md5(trim($password)))->field('id,rands')->find();
- if (!$user){
- $this->result('账号或密码错误,请重新登录');
- }else{
- $get_hu=MemberHouseModel::where('mid',$user['id'])->where('is_delete','0')->field('id')->find();
- if (empty($get_hu)){
- $user['hu_id']=0;
- }else{
- $user['hu_id']=$get_hu['id'];
- }
- MemberModel::where('id', $user['id'])->setInc('login_num', 1);
-
- $salt = createSalt();
- $token = md5($user['id'].$salt);
-
- $updateToken = MemberModel::where('mobile', $mobile)
- ->update(['salt' => $salt, 'token' => $token]);
- if ($updateToken) {
- $this->result('登录成功',['user_id' => $user['id'], 'token' => $token,'rands'=>$user['rands'],'hu_id'=>$user['hu_id']], 1);
- } else {
- $this->result('登录失败', null, 0);
- }
- }
- }
-
- public function edit_password()
- {
- $mobile = $this->request->post('mobile');
- $checkCode = $this->request->post('check_code');
- $password = $this->request->post('password');
- if (!$mobile || !$checkCode || !$password) {
- $this->result('参数错误');
- }
-
- $check_code = CodeModel::where('mobile', $mobile)->where('scene','2')
- ->order('id desc')
- ->limit(0, 1)
- ->select();
- if (empty($check_code)) {
- $this->result('验证码错误');
- }
- if ($check_code[0]['code'] != $checkCode) {
- $this->result('验证码错误');
- }
- $sendTime = strtotime($check_code[0]['time']);
- if (time() - $sendTime > 300) {
- $this->result('验证码已过期');
- }
- $user = MemberModel::where('mobile', $mobile)->field('id,rands')->find();
- if (empty($user)){
- $this->result('您的手机号还未注册用户,请注册');
- }
- MemberModel::where('id', $user['id'])->setInc('login_num', 1);
-
- $salt = createSalt();
- $token = md5($user['id'].$salt);
-
- $updateToken = MemberModel::where('mobile', $mobile)
- ->update(['salt' => $salt, 'token' => $token,'password'=>md5(trim($password))]);
- if ($updateToken) {
- $this->result('密码修改成功',['user_id' => $user['id'], 'token' => $token,'rands'=>$user['rands']], 1);
- } else {
- $this->result('登录失败', null, 0);
- }
- }
-
- public function my_detail(){
- $userId = $this->request->post('user_id');
- $house_id = $this->request->post('house_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
-
- $my=MemberModel::where(['id'=>$userId])->find();
- if (!empty($house_id)){
- $where['id']=$house_id;
- }
- $where['mid']=$userId;
- $where['is_delete']='0';
- $list=MemberHouseModel::where($where)
- ->with(['property','village','dong','danyuan','hu'])
- ->find();
- $my['house']='';
- if (!empty($list)){
- $my['house']=$list['village']['name'].$list['dong']['name'].$list['danyuan']['name'].$list['hu']['name'];
- }
- $this->result('success', $my, 1);
- }
-
- public function my_edit(){
- $userId = $this->request->post('user_id');
- $nickname= $this->request->post('nickname');
- $avatar = $this->request->post('avatar');
- $sex = $this->request->post('sex');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- $update=[];
- if (!empty($nickname)){
- $update['nickname']=preg_replace('/[\xf0-\xf7].{3}/', '', trim($nickname));
- }
- if (!empty($avatar)){
- $update['avatar']=$avatar;
- }
- if (!empty($sex)){
- $update['sex']=$sex;
- }else{
- $update['sex']='未知';
- }
- if (!empty($update)){
- MemberModel::where(['id'=>$userId])->update($update);
- $this->result('信息修改成功', [], 1);
- }else{
- $this->result('没修改任何信息');
- }
- }
-
- public function wx_login()
- {
- $userId = $this->request->post('user_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- $code = $this->request->post('code');
- if (!isset($code)) {
- return $this->result('未接收到code', []);
- }
- $appid = 'wxbe636a16aae015f9';
- $appsecret = '46a02319143e8c2f9b1d96104bd467e7';
- $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
- $tokens =http_curl($access_token_url);
- if (isset($tokens['openid'])) {
- $member=MemberModel::where('id',$userId)->update(['openid'=>$tokens['openid']]);
- return $this->result('success', '', 200);
- } else {
- return $this->result('网络错误104');
- }
- }
-
- public function my_house_list(){
- $userId = $this->request->post('user_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- $list=MemberHouseModel::where(['mid'=>$userId,'is_delete'=>'0'])
- ->with(['property','village','dong','danyuan','hu'])
- ->select();
- foreach ($list as $k=>$v){
- $list[$k]['house']=$v['property']['name']." ".$v['village']['name']." ".$v['danyuan']['name']." ".$v['hu']['name'];
- }
- $this->result('房屋列表', $list, 1);
- }
-
- public function my_house_add(){
- $userId = $this->request->post('user_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- $property_id = $this->request->post('property_id');
- $village_id = $this->request->post('village_id');
- $dong_id = $this->request->post('dong_id');
- $danyuan_id = $this->request->post('danyuan_id');
- $hu_id= $this->request->post('hu_id');
- $update=[
- 'property_id'=>$property_id,
- 'village_id'=>$village_id,
- 'dong_id'=>$dong_id,
- 'danyuan_id'=>$danyuan_id,
- 'hu_id'=>$hu_id,
- 'mid'=>$userId,
- 'createtime'=>time(),
- 'updatetime'=>time()
- ];
- MemberHouseModel::insert($update);
- $this->result('住房绑定成功', '', 1);
- }
-
- public function my_house_del(){
- $userId = $this->request->post('user_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- $house_id = $this->request->post('house_id');
- MemberHouseModel::where(['mid'=>$userId,'id'=>$house_id])->update(['is_delete'=>'1']);
- $this->result('住房解绑成功', '', 1);
- }
-
- public function out()
- {
- $userId = $this->request->post('user_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- $updateToken = MemberModel::where('id', $userId)
- ->update(['salt' => '0', 'token' => '0']);
- $this->result('成功退出','', 1);
- }
- }
|