123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 会员中心
- */
- class Usercenter extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- //申请实名认证
- //阿里云数链云三要素
- //https://market.aliyun.com/products/57000002/cmapi00053444.html?spm=5176.2020520132.101.7.3dc17218ETmLVi#sku=yuncode4744400005
- public function idcard_apply_new(){
- $truename = input('truename','');
- $idcard = input('idcard','');
- $idcard_images = input('idcard_images','');
- if(empty($truename) || empty($idcard) || empty($idcard_images)){
- $this->error('实名认证信息必填');
- }
- if($this->auth->idcard_status == 1){
- $this->error('您已经完成实名认证');
- }
- if($this->auth->idcard_status == 0){
- $this->error('您已经提交实名认证,请等待审核');
- }
- Db::startTrans();
- $check = Db::name('user_idcard')->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('您已经完成实名认证');
- }
- }
- $count = Db::name('user_idcard')->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 > 3) {
- $this->error('今日实名次数已到上限,明天再来吧');
- }
- }
- //阿里云身份证三要素认证
- $auth_restult = $this->usercertify_aliyun_three($idcard, $truename,$this->auth->mobile);
- if($auth_restult == false){
- $this->error('身份证信息与姓名或注册手机号不符');
- }
- //
- $data = [
- 'user_id' => $this->auth->id,
- 'truename' => $truename,
- 'idcard' => $idcard,
- 'idcard_images' => $idcard_images,
- '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_idcard')->where('id',$check['id'])->update($data);
- }else{
- $rs = Db::name('user_idcard')->insertGetId($data);
- }
- if(!$rs || !$update_rs){
- Db::rollback();
- $this->error('认证失败');
- }
- Db::commit();
- $this->success('认证通过');
- }
- //实名认证三要素
- private function usercertify_aliyun_three($cardNo = '',$realname = '',$mobile = ''){
- if(!$cardNo || !$realname || !$mobile){
- return false;
- }
- $host = 'https://slysjsysgz.market.alicloudapi.com';
- $path = '/mobile_three/check';
- $method = 'GET';
- $appcode = config('ali_three_code');
- $headers = array();
- array_push($headers, 'Authorization:APPCODE ' . $appcode);
- $querys = 'idcard='.$cardNo.'&mobile='.$mobile.'&name='.urlencode($realname);
- $bodys = '';
- $url = $host . $path . '?' . $querys;
- $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);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- if (1 == strpos('$'.$host, 'https://'))
- {
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- $result = (curl_exec($curl));
- curl_close($curl);
- $result = json_decode($result,true);
- if(is_array($result) && isset($result['success']) && $result['success'] == true){
- if(isset($result['data']) && isset($result['data']['result'])){
- if($result['data']['result'] == 0){
- //实名过了
- return true;
- }
- }
- }
- return false;
- }
- //申请实名认证
- public function idcard_apply(){
- $truename = input('truename','');
- $idcard = input('idcard','');
- $idcard_images = input('idcard_images','');
- if(empty($truename) || empty($idcard) || empty($idcard_images)){
- $this->error('实名认证信息必填');
- }
- if($this->auth->idcard_status == 1){
- $this->error('您已经完成实名认证');
- }
- if($this->auth->idcard_status == 0){
- $this->error('您已经提交实名认证,请等待审核');
- }
- Db::startTrans();
- $check = Db::name('user_idcard')->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('您已经完成实名认证');
- }
- }
- $count = Db::name('user_idcard')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
- if ($count) {
- $this->error('该身份证号已被他人使用');
- }
- $data = [
- 'user_id' => $this->auth->id,
- 'truename' => $truename,
- 'idcard' => $idcard,
- 'idcard_images' => $idcard_images,
- 'status' => 0,
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- //更新
- $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
- if(!empty($check)){
- $rs = Db::name('user_idcard')->where('id',$check['id'])->update($data);
- }else{
- $rs = Db::name('user_idcard')->insertGetId($data);
- }
- if(!$rs || !$update_rs){
- Db::rollback();
- $this->error('提交失败');
- }
- Db::commit();
- $this->success('提交成功,请等待审核');
- }
- //实名认证信息
- public function idcard_info(){
- $check = Db::name('user_idcard')->where('user_id',$this->auth->id)->order('id desc')->find();
- $this->success('success',$check);
- }
- //驾驶证认证
- public function driver_apply(){
- $driver_images = input('driver_images','');
- if(empty($driver_images)){
- $this->error('驾驶证图片必填');
- }
- if($this->auth->driver_status == 1){
- $this->error('您已经完成驾驶证认证');
- }
- if($this->auth->driver_status == 0){
- $this->error('您已经提交驾驶证认证,请等待审核');
- }
- Db::startTrans();
- $check = Db::name('user_driver')->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('您已经完成驾驶证认证');
- }
- }
- $data = [
- 'user_id' => $this->auth->id,
- 'driver_images' => $driver_images,
- 'status' => 0,
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- //更新
- $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['driver_status'=>0]);
- if(!empty($check)){
- $rs = Db::name('user_driver')->where('id',$check['id'])->update($data);
- }else{
- $rs = Db::name('user_driver')->insertGetId($data);
- }
- if(!$rs || !$update_rs){
- Db::rollback();
- $this->error('提交失败');
- }
- Db::commit();
- $this->success('提交成功,请等待审核');
- }
- //驾驶证认证信息
- public function driver_info(){
- $check = Db::name('user_driver')->where('user_id',$this->auth->id)->order('id desc')->find();
- $this->success('success',$check);
- }
- //我的消息通知
- public function msg_list(){
- $list = Db::name('user_msg')->where('user_id',$this->auth->id)->autopage()->select();
- if(!empty($list)){
- $ids = array_column($list,'id');
- Db::name('user_msg')->where('id','IN',$ids)->update(['status'=>1]);
- }
- $this->success('success',$list);
- }
- //我的消息通知
- public function msg_info(){
- $id = input('id');
- $info = Db::name('user_msg')->where('id',$id)->find();
- $this->success('success',$info);
- }
- //用户钱包流水
- public function moneylog(){
- $list = Db::name('user_money_log')->where('user_id',$this->auth->id)->order('id desc')->autopage()->select();
- $this->success(1,$list);
- }
- //我的积分页面
- public function myscore(){
- $score = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('score');
- $where = [
- 'type' => 2,
- 'stock' => ['gt',1],
- 'limit' => ['gt',0],
- 'usetimeend' => ['gt',time()],
- ];
- $coupons = Db::name('coupons')->where($where)->order('id desc')->select();
- //去掉不能领的
- $new_coupons = [];
- foreach($coupons as $key => $val){
- $user_conpon = Db::name('user_coupons')->where('user_id',$this->auth->id)->where('coupons_id',$val['id'])->count();
- if($user_conpon >= $val['limit']){
- continue;
- }
- $new_coupons[] = $val;
- }
- $rs['score'] = $score;
- $rs['coupons'] = $new_coupons;
- $this->success(1,$rs);
- }
- //积分日志
- public function scorelog(){
- $score_log = Db::name('user_score_log')->where('user_id',$this->auth->id)->order('id desc')->autopage()->select();
- $this->success(1,$score_log);
- }
- //积分兑换优惠券
- public function score_to_coupons(){
- $coupon_id = input('coupon_id',0);
- //检查
- Db::startTrans();
- $coupon_info = Db::name('coupons')->where('type',2)->where('id',$coupon_id)->lock(true)->find();
- if(!$coupon_info){
- Db::rollback();
- $this->error('不存在的优惠券');
- }
- if($coupon_info['stock'] < 1){
- Db::rollback();
- $this->error('优惠券已经领光了');
- }
- //检查
- $where = [
- 'user_id' => $this->auth->id,
- 'type' => 2,
- 'coupons_id' => $coupon_id,
- ];
- $check = Db::name('user_coupons')->where($where)->count();
- if($check >= $coupon_info['limit']){
- Db::rollback();
- $this->error('此券每人最多领取'.$coupon_info['limit'].'次');
- }
- //插入数据
- $data = [
- 'user_id' => $this->auth->id,
- 'company_id' => $coupon_info['company_id'],
- 'enough' => $coupon_info['enough'],
- 'amount' => $coupon_info['amount'],
- 'type' => $coupon_info['type'],
- 'coupons_id' => $coupon_id,
- 'createtime' => time(),
- 'usetimestart' => $coupon_info['usetimestart'],
- 'usetimeend' => $coupon_info['usetimeend'],
- 'status' => 0
- ];
- $log_id = Db::name('user_coupons')->insertGetId($data);
- //扣积分
- $wallet = new \app\common\model\Wallet;
- $wallet_rs = $wallet->lockChangeAccountRemain($this->auth->id,'score',-$coupon_info['score'],21,'兑换优惠券','user_coupons',$log_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- //扣库存
- $coupon_rs = Db::name('coupons')->where('id',$coupon_id)->update(['stock' => $coupon_info['stock'] - 1]);
- if($coupon_rs === false){
- Db::rollback();
- $this->error('领取失败');
- }
- //结束
- Db::commit();
- $this->success('兑换成功');
- }
- //领券中心
- public function coupons_hub(){
- $where = [
- 'coupons.type' => 1,
- 'coupons.stock' => ['gt',1],
- 'coupons.limit' => ['gt',0],
- 'coupons.usetimeend' => ['gt',time()],
- ];
- $list = Db::name('coupons')
- ->field('coupons.*,company.name as company_name')
- ->join('company','coupons.company_id = company.id','LEFT')
- ->where($where)
- ->order('coupons.id desc')->select();
- //去掉不能领的
- $new_list = [];
- foreach($list as $key => $val){
- $user_conpon = Db::name('user_coupons')->where('user_id',$this->auth->id)->where('coupons_id',$val['id'])->count();
- if($user_conpon >= $val['limit']){
- continue;
- }
- $new_list[] = $val;
- }
- $this->success(1,$new_list);
- }
- //领取一个优惠券
- public function coupons_get(){
- $coupon_id = input('coupon_id',0);
- //检查
- Db::startTrans();
- $coupon_info = Db::name('coupons')->where('type',1)->where('id',$coupon_id)->lock(true)->find();
- if(!$coupon_info){
- Db::rollback();
- $this->error('不存在的优惠券');
- }
- if($coupon_info['stock'] < 1){
- Db::rollback();
- $this->error('优惠券已经领光了');
- }
- //检查
- $where = [
- 'user_id' => $this->auth->id,
- 'type' => 1,
- 'coupons_id' => $coupon_id,
- ];
- $check = Db::name('user_coupons')->where($where)->count();
- if($check >= $coupon_info['limit']){
- Db::rollback();
- $this->error('此券每人最多领取'.$coupon_info['limit'].'次');
- }
- //插入数据
- $data = [
- 'user_id' => $this->auth->id,
- 'company_id' => $coupon_info['company_id'],
- 'enough' => $coupon_info['enough'],
- 'amount' => $coupon_info['amount'],
- 'type' => $coupon_info['type'],
- 'coupons_id' => $coupon_id,
- 'createtime' => time(),
- 'usetimestart' => $coupon_info['usetimestart'],
- 'usetimeend' => $coupon_info['usetimeend'],
- 'status' => 0
- ];
- $log_id = Db::name('user_coupons')->insertGetId($data);
- //扣库存
- $coupon_rs = Db::name('coupons')->where('id',$coupon_id)->update(['stock' => $coupon_info['stock'] - 1]);
- if($coupon_rs === false){
- Db::rollback();
- $this->error('领取失败');
- }
- //结束
- Db::commit();
- $this->success('领取成功');
- }
- //我的优惠券
- public function mycoupons(){
- $status = input('status',0);
- $where = [
- 'c.user_id' => $this->auth->id,
- 'c.status' => $status,
- ];
- //未使用的要过滤掉过期的
- if($status == 0){
- $where['c.usetimeend'] = ['gt',time()];
- }
- $list = Db::name('user_coupons')->alias('c')
- ->field('c.*,company.name as company_name')
- ->join('company','c.company_id = company.id','LEFT')
- ->where($where)
- ->autopage()
- ->select();
- $this->success(1,$list);
- }
- //推广小程序码
- public function usershareposter()
- {
- $scene = $this->auth->introcode;
- // $qrcode = Db::name('shopro_store')->where('id', $scene)->value('qrcode');
- // if (!$qrcode ) {
- $qrcode = $this->circle_code($scene, '');
- // Db::name('shopro_store')->where('id', $scene)->update(['qrcode' => $qrcode]);
- // }
- $this->success(1, $qrcode);
- }
- //二维码海报
- public function circle_code($scene, $page_url)
- {
- $value = config('wxMiniProgram');
- $appid = $value['appid'];
- $secret = $value['secret'];
- $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
- $res = file_get_contents($url);
- $token = json_decode($res, true)['access_token'];
- $URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $token;
- $data = [
- 'scene' => $scene, //二维码传入参数
- 'page' => $page_url, //扫码后进入页面
- 'env_version' => 'trial', //要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
- 'width' => 280, //二维码的宽度,单位 px,最小 280px,最大 1280px
- 'auto_color' => false, //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
- 'is_hyaline' => false, //是否需要透明底色,为 true 时,生成透明底色的小程序
- ];
- $json = json_encode($data); //数组加密
- //$result = $this->api_notice_increment($URL, $json); //用CURL 进行POST请求
- $result = curl_post($URL,$json);
- $path = ROOT_PATH . 'public/upload/extend/qrcode/original'; //ROOT_PATH 我使用的是TP5框架
- $path2 = '/upload/extend/qrcode/original';
- if (!file_exists($path)) { //判断目录是否存在
- mkdir($path, 0777, true);
- }
- $path = $path . '/' . $scene . '.png'; //最后要写入的目录及文件名
- $path2 = $path2 . '/' . $scene . '.png'; //最后要写入的目录及文件名
- //小程序码
- //if (!file_exists($path)) {
- // 创建将数据流文件写入我们创建的文件内容中
- file_put_contents($path, $result);
- //}
- //加上背景地图
- $path = $this->shareposter($path2);
- // $path = ROOT_PATH . 'public/'. $path;
- $path = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $path;
- return $path;
- //上传到oss
- /*$fileName = sha1(date('YmdHis', time()) . uniqid()) . '.png';
- $oss = new Oss();
- $oss_path = $oss->uploadFile($fileName, $path);
- return $oss_path;*/
- }
- //生成海报
- public function shareposter($path) {
- $info['id'] = $this->auth->id;
- $data = [
- [
- "left"=> "90px",
- "top"=> "445px",
- "type"=> "img",
- "width"=> "195px",
- "height"=> "185px",
- "src"=> $path
- ],
- ];
- $data = json_encode($data, 320);
- $poster = [
- 'id' => 1,
- 'title' => '测试',
- 'waittext' => '海报正在拼命生成中,请等待片刻...',
- 'bg_image' => '/assets/img/sharebak.png',
- 'data' => $data,
- 'status' => 'normal',
- 'weigh' => 0,
- 'createtime' => 1653993709,
- 'updatetime' => 1653994259,
- ];
- $image = new \addons\poster\library\Image();
- $imgurl = $image->createPosterImage($poster, $info);
- if (!$imgurl) {
- $this->error('生成海报出错');
- }
- // $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
- return $imgurl;
- }
- //我邀请的金钱日志,给邀请页面
- public function myinvite_moneylog(){
- $list = Db::name('user_money_log')->field('log.change_value,log.createtime,user.avatar,user.mobile')->alias('log')
- ->join('order','order.id = log.table_id')
- ->join('user','order.user_id = user.id')
- ->where('log.user_id',$this->auth->id)->where('log.log_type',3)->order('log.id desc')->autopage()->select();
- $list = list_domain_image($list,['avatar']);
- foreach($list as $key => &$val){
- $val['mobile'] = str_replace(substr($val['mobile'],3,5),'****',$val['mobile']);
- }
- $this->success(1,$list);
- }
-
- }
|