Pārlūkot izejas kodu

消息模块,实名认证

lizhen_gitee 4 mēneši atpakaļ
vecāks
revīzija
17831f2df5

+ 132 - 0
application/api/controller/Message.php

@@ -0,0 +1,132 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 消息
+ */
+class Message extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+    //消息页信息
+    public function index(){
+        $rs = [
+            'msg_first'      => Db::name('message')->where('user_id',$this->auth->id)->order('id desc')->find(),
+            'msg_unread_num' => Db::name('message')->where('user_id',$this->auth->id)->where('status',0)->count(),
+            'msgsys_first'   => Db::name('message_sys')->order('id desc')->find(),
+        ];
+
+        //系统消息未读数量
+        $sys_ids = Db::name('message_sys')->column('id');
+        $all_num = count($sys_ids);
+        $read_num = Db::name('user_messagesys')->where('user_id',$this->auth->id)->where('msg_id','IN',$sys_ids)->count();
+        $rs['msgsys_unread_num'] = $all_num > $read_num ? $all_num - $read_num : 0; //因公告被删除,未读公告没来得及删除,相减可能会负数
+
+        $this->success(1,$rs);
+    }
+
+    //个人消息全部改为已读
+    public function message_read(){
+        Db::startTrans();
+        //读取即为已读
+        $map = [
+            'user_id'  => $this->auth->id,
+            'status'   => 0,
+        ];
+        $rs = Db::name('message')->where($map)->update(['status'=>1]);
+        if($rs === false){
+            Db::rollback();
+            $this->error('操作失败');
+        }
+
+
+        //系统消息
+        //全部未读
+        $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
+        if($rs === false){
+            Db::rollback();
+            $this->error('操作失败');
+        }
+
+        //循环全部已读
+        $list = Db::name('message_sys')->column('id');
+        if(!empty($list)){
+            $newall = [];
+            foreach($list as $key => $msg_id){
+                $newall[] = [
+                    'user_id' => $this->auth->id,
+                    'msg_id' => $msg_id,
+                ];
+            }
+            if(!empty($newall)){
+                $rs = Db::name('user_messagesys')->insertAll($newall);
+                if($rs === false){
+                    Db::rollback();
+                    $this->error('操作失败');
+                }
+            }
+        }
+
+        Db::commit();
+        $this->success();
+    }
+
+    //我的个人消息列表
+    public function mylist(){
+        $list = Db::name('message')->where('user_id',$this->auth->id)->autopage()->order('id desc')->select();
+
+        //读取即为已读
+        $map = [
+            'user_id' => $this->auth->id,
+            'status' => 0,
+        ];
+        Db::name('message')->where($map)->update(['status'=>1]);
+        $this->success('success',$list);
+    }
+
+    /*
+     * 获取系统消息列表
+     */
+    public function getmessagesys() {
+
+        $list = Db::name('message_sys')->autopage()->order('id desc')->select();
+
+        Db::startTrans();
+        //系统消息
+        //全部未读
+        $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
+        if($rs === false){
+            Db::rollback();
+            $this->error('操作失败');
+        }
+
+        //循环全部已读
+        $list2 = Db::name('message_sys')->column('id');
+        if(!empty($list2)){
+            $newall = [];
+            foreach($list2 as $key => $msg_id){
+                $newall[] = [
+                    'user_id' => $this->auth->id,
+                    'msg_id' => $msg_id,
+                ];
+            }
+            if(!empty($newall)){
+                $rs = Db::name('user_messagesys')->insertAll($newall);
+                if($rs === false){
+                    Db::rollback();
+                    $this->error('操作失败');
+                }
+            }
+        }
+
+        Db::commit();
+        $this->success("获取成功!",$list);
+    }
+
+
+
+}

+ 187 - 0
application/api/controller/Userauth.php

@@ -0,0 +1,187 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+use think\Cache;
+
+
+/**
+ * 实名认证,真人认证相关
+ */
+class Userauth extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+
+
+
+
+    //实名认证信息
+    public function idcard_info(){
+        $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
+        $this->success('success',$check);
+    }
+
+    //申请实名认证
+    public function apply_idcard_confirm(){
+        $truename = input('truename','');
+        $idcard   = input('idcard'  ,'');
+
+        if(empty($truename) || empty($idcard)){
+            $this->error('实名认证信息必填');
+        }
+
+        if($this->auth->idcard_status == 1){
+
+            $this->error('您已经完成实名认证');
+        }
+
+        if($this->auth->idcard_status == 0){
+            $this->error('您已经提交实名认证,请等待审核');
+        }
+
+        if($this->auth->idcard_status == 3){
+            $this->error('您的实名认证被取消,不能再次申请');
+        }
+
+        Db::startTrans();
+        $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
+        if(!empty($check)){
+            if($check['status'] == 0){
+                Db::rollback();
+                $this->error('您已经提交实名认证,请等待审核');
+            }
+            if($check['status'] == 1){
+                Db::rollback();
+                $this->error('您已经完成实名认证');
+            }
+            if($check['status'] == 3){
+                Db::rollback();
+                $this->error('您的实名认证被取消,不能再次申请');
+            }
+        }
+
+        $count = Db::name('user_idconfirm')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
+        if ($count) {
+            $this->error('该身份证号已被他人使用');
+        }
+
+        //限制每日请求次数
+        $time = time();
+        $today_end = strtotime(date('Y-m-d 23:59:59', $time));
+        $cache_time = $today_end - $time; //缓存时间
+        $time_count = Cache::get('fourauth' . $this->auth->id);
+        if (!$time_count) {
+            Cache::set('fourauth' . $this->auth->id, 1, $cache_time);
+        } else {
+            Cache::set('fourauth' . $this->auth->id, $time_count + 1, $cache_time);
+            if ($time_count > 5) {
+                $this->error('今日实名次数已到上限,明天再来吧');
+            }
+        }
+
+        //阿里云身份证三要素认证
+        $auth_restult = $this->userauth_aliyun_three($idcard, $truename,$this->auth->mobile);
+        if($auth_restult == false){
+            $this->error('身份证信息与姓名或注册手机号不符');
+        }
+
+        $data = [
+            'user_id' => $this->auth->id,
+            'truename' => $truename,
+            'idcard' => $idcard,
+            'status' => 1, //不需要人工刚审核了,直接过审
+            'createtime' => time(),
+            'updatetime' => time(),
+        ];
+
+        //更新
+        $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>1]);//不需要人工刚审核了,直接过审
+        if(!empty($check)){
+            $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
+        }else{
+            $rs = Db::name('user_idconfirm')->insertGetId($data);
+        }
+
+        //task任务
+        //实名认证
+        $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
+        if($task_rs === false){
+            Db::rollback();
+            $this->error('完成任务失败');
+        }
+
+        //系统消息
+        $msg_id = \app\common\model\Message::addMessage($this->auth->id,'实名认证','实名认证已经审核通过');
+
+        if(!$rs || !$update_rs){
+            Db::rollback();
+            $this->error('提交失败');
+        }
+
+        Db::commit();
+        $this->success('认证通过');
+
+    }
+
+
+
+    //产品链接:https://market.aliyun.com/products/57000002/cmapi026100.html?spm=5176.730005.result.8.1dbc123e8ArY19&innerSource=search#sku=yuncode2010000006
+    //阿里云-数脉api
+    //姓名+手机号+身份证号
+    private function userauth_aliyun_three($cardNo = '',$realname = '',$mobile = ''){
+        if(!$cardNo || !$realname || !$mobile){
+            return false;
+        }
+
+        $config = config('aliyun_auth_shumai');
+
+        $host = "https://mobile3elements.shumaidata.com";
+        $path = "/mobile/verify_real_name";
+        $method = "POST";
+        $appcode = $config['app_code'];
+        $headers = array();
+        array_push($headers, "Authorization:APPCODE " . $appcode);
+        //根据API的要求,定义相对应的Content-Type
+        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
+        $querys = "";
+        $bodys = "idcard=".$cardNo."&mobile=".$mobile."&name=".urlencode($realname);
+        $url = $host . $path;
+
+        $curl = curl_init();
+        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
+        curl_setopt($curl, CURLOPT_URL, $url);
+        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
+        curl_setopt($curl, CURLOPT_FAILONERROR, false);
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+        //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
+        //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
+        curl_setopt($curl, CURLOPT_HEADER, false);
+        if (1 == strpos("$".$host, "https://"))
+        {
+            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
+            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
+        }
+        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
+        $returnRes = curl_exec($curl);
+        curl_close($curl);
+        $result = json_decode($returnRes,true);
+
+        if(is_array($result) && isset($result['code']) && $result['code'] == 0){
+            if(isset($result['result']) && isset($result['result']['res'])){
+                if($result['result']['res'] == 1){
+                    //实名过了
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+
+
+
+
+}