123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\controller;
- use app\api\model\user\CommissionsDetail;
- use app\api\model\UserCoupon;
- use app\common\enum\Setting as SettingEnum;
- use app\common\exception\BaseException;
- use app\api\model\UserCoupon as UserCouponModel;
- use app\api\service\User as UserService;
- use app\common\library\helper;
- use app\common\model\Shops;
- use app\api\model\user\BonusHistory;
- use app\api\model\user\CommissionSteps;
- use app\api\model\Setting as SettingModel;
- use app\common\model\UserVerify as UserVerifyModel;
- use app\common\model\SmsCode;
- /**
- * 用户实名验证管理
- * Class User
- * @package app\api
- */
- class Userverify extends Controller
- {
- public function add(){
- $param = $this->request->param();
- $userinfo = UserService::getCurrentLoginUser(true);
- $id_card = $param['id_card']??'';
- if(!$this->isValid($id_card)){
- return $this->renderError("请输入正确的身份证号");
- }
- $user_name = $param['user_name']??'';
- $mobile = $param['mobile']??'';
- $sms_code = $param['sms_code']??'';
- $user_id = $userinfo->user_id;
- $sms = SmsCode::where("user_id",$user_id)->where("mobile",$mobile)->where("create_time",'>',time()-300)->order(['id'=>'desc'])->find();
- if(empty($sms)){
- return $this->renderError("请获取手机验证码");
- }
- if($sms->code!=$sms_code){
- return $this->renderError("验证码不正确");
- }
- UserVerifyModel::where('user_id',$user_id)->delete();
- $data['user_id'] = $user_id;
- $data['id_card'] = $id_card;
- $data['user_name'] = $user_name;
- $data['mobile'] = $mobile;
- $data['create_time'] = time();
- $data['update_time'] = time();
- $userverify = new UserVerifyModel;
- $userverify->save($data);
- $sms->is_use = 1;
- $sms->save();
- return $this->renderSuccess();
- }
- /**
- * 校验身份证号是否合法
- * @param string $num 待校验的身份证号
- * @return bool
- */
- public function isValid(string $num)
- {
- //老身份证长度15位,新身份证长度18位
- $length = strlen($num);
- if ($length == 15) { //如果是15位身份证
- //15位身份证没有字母
- if (!is_numeric($num)) {
- return false;
- }
- // 省市县(6位)
- $areaNum = substr($num, 0, 6);
- // 出生年月(6位)
- $dateNum = substr($num, 6, 6);
- } else if ($length == 18) { //如果是18位身份证
- //基本格式校验
- if (!preg_match('/^\d{17}[0-9xX]$/', $num)) {
- return false;
- }
- // 省市县(6位)
- $areaNum = substr($num, 0, 6);
- // 出生年月日(8位)
- $dateNum = substr($num, 6, 8);
- } else { //假身份证
- return false;
- }
- //验证地区
- if (!$this->isAreaCodeValid($areaNum)) {
- return false;
- }
- //验证日期
- if (!$this->isDateValid($dateNum)) {
- return false;
- }
- //验证最后一位
- if (!$this->isVerifyCodeValid($num)) {
- return false;
- }
- return true;
- }
- /**
- * 省市自治区校验
- * @param string $area 省、直辖市代码
- * @return bool
- */
- private static function isAreaCodeValid(string $area) {
- $provinceCode = substr($area, 0, 2);
- // 根据GB/T2260—999,省市代码11到65
- if (11 <= $provinceCode && $provinceCode <= 65) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 验证出生日期合法性
- * @param string $date 日期
- * @return bool
- */
- private function isDateValid(string $date) {
- if (strlen($date) == 6) { //15位身份证号没有年份,这里拼上年份
- $date = '19'.$date;
- }
- $year = intval(substr($date, 0, 4));
- $month = intval(substr($date, 4, 2));
- $day = intval(substr($date, 6, 2));
- //日期基本格式校验
- if (!checkdate($month, $day, $year)) {
- return false;
- }
- //日期格式正确,但是逻辑存在问题(如:年份大于当前年)
- $currYear = date('Y');
- if ($year > $currYear) {
- return false;
- }
- return true;
- }
- /**
- * 验证18位身份证最后一位
- * @param string $num 待校验的身份证号
- * @return bool
- */
- public function isVerifyCodeValid(string $num)
- {
- if (strlen($num) == 18) {
- $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
- $tokens = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
- $checkSum = 0;
- for ($i = 0; $i < 17; $i++) {
- $checkSum += intval($num[$i]) * $factor[$i];
- }
- $mod = $checkSum % 11;
- $token = $tokens[$mod];
- $lastChar = strtoupper($num[17]);
- if ($lastChar != $token) {
- return false;
- }
- }
- return true;
- }
- // /**
- // * 当前用户详情
- // * @return array|\think\response\Json
- // * @throws BaseException
- // */
- // public function info()
- // {
- // // 当前用户信息
- // $userInfo = UserService::getCurrentLoginUser(true);
- // // 获取用户头像
- // $userInfo['avatar'] = $userInfo['avatar'] ?? ['preview_url' => config('chef.user_default_avatar'), 'external_url' => config('chef.user_default_avatar')];
- // // 获取会员等级
- // $userInfo['grade'];
- // // 今日收益
- // $userInfo['today_profits'] = helper::bcadd(CommissionsDetail::getUserTodayProfits($userInfo->user_id), 0, 2);
- // $userInfo['shop_name'] = '';
- // $userInfo['is_pickup'] = 0;
- // if($userInfo['shop_id'] && $userInfo['role']==4){
- // $shops = Shops::find($userInfo['shop_id']);
- // $userInfo['shop_name'] = $shops['shop_name'];
- // if($shops['is_pickup']){
- // $userInfo['is_pickup'] = $shops['is_pickup'];//开启门店自提
- // }
- // }
- // $userInfo['bind_shop_name'] = Shops::find($userInfo['bind_shop_id'])['shop_name'] ?? '';
- // //待结算金额
- // $djs_amount = CommissionsDetail::where('user_id', $userInfo->user_id)->where('is_shop_commission',0)->where('clearing_status', 0)->sum('clearing_money') ?? 0;
- // $userInfo['djs_amount'] = helper::bcadd($djs_amount, 0, 2);
- // $userInfo['show_cash_box'] = false;
- // $wait_clearing = CommissionsDetail::getUserWaitCommission($userInfo['user_id']);
- // if ($wait_clearing >0 || $userInfo['can_withdraw_money'] >0){
- // $userInfo['show_cash_box'] = true;
- // }
- // $userInfo['can_withdraw_money'] = helper::bcsub($userInfo['can_withdraw_money'],0,2); // 佣金已结算总金额
- // $userInfo['have_withdrew_money'] = helper::bcsub($userInfo['have_withdrew_money'],0,2); // 已提现金额
- // $userInfo['ktxyj_amount'] = helper::bcsub($userInfo['ktxyj_amount'],0,2); // 可提现金额
- // // 上月达量奖励金
- // $userInfo['last_month_bonus'] = BonusHistory::lastMonthBonus($userInfo['user_id']);
- // // 阶梯奖励金计算
- // $userInfo['calc_bonus_steps'] = CommissionSteps::calcBonusSteps($userInfo);
- // // 阶梯奖励金计算
- // $userInfo['sales_bonus_steps'] = $list['distributor_step'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR_STEP)['distributor'];
- // // 我的优惠券可用数量
- // $userInfo['user_coupon_num'] = (new UserCoupon)->getCount($userInfo['user_id']);
- // return $this->renderSuccess(compact('userInfo'));
- // }
- // /**
- // * 账户资产
- // * @return array|\think\response\Json
- // * @throws BaseException
- // */
- // public function assets()
- // {
- // // 当前用户信息
- // $userInfo = UserService::getCurrentLoginUser(true);
- // // 用户优惠券模型
- // $model = new UserCouponModel;
- // // 返回数据
- // return $this->renderSuccess([
- // 'assets' => [
- // 'balance' => $userInfo['balance'], // 账户余额
- // 'points' => $userInfo['points'], // 会员积分
- // 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用)
- // ]
- // ]);
- // }
- }
|