Просмотр исходного кода

Wesmiler 校企小程序源代码部署

wesmiler 4 лет назад
Родитель
Сommit
101aa87873

+ 14 - 0
app/api/controller/User.php

@@ -42,6 +42,19 @@ class User extends Controller
     }
 
     /**
+     * @return Json
+     * @throws \cores\exception\BaseException
+     */
+    public function setInfo(): Json
+    {
+        $model = new UserModel;
+        if (!$result = $model->saveInfo($this->postForm())) {
+            return $this->renderSuccess($model->getError() ?: '保存失败');
+        }
+        return $this->renderSuccess($result? $result : '保存成功');
+    }
+
+    /**
      * 账户资产
      * @return Json
      * @throws BaseException
@@ -75,4 +88,5 @@ class User extends Controller
         }
         return $this->renderSuccess('恭喜您,手机号绑定成功');
     }
+
 }

+ 126 - 0
app/api/model/User.php

@@ -12,6 +12,7 @@ declare (strict_types=1);
 
 namespace app\api\model;
 
+use app\api\validate\user\Info as ValidateInfo;
 use think\facade\Cache;
 use app\api\service\User as UserService;
 use app\api\model\UserOauth as UserOauthModel;
@@ -112,4 +113,129 @@ class User extends UserModel
             throwError('很抱歉,该手机号已绑定其他账户');
         }
     }
+
+    /**
+     * @param array $data
+     * @return string
+     * @throws BaseException
+     */
+    public function saveInfo(array $data): string
+    {
+        // 修改手机号需要验证验证码
+        $userInfo = UserService::getCurrentLoginUser(true);
+        // 验证信息
+        $this->checkInfo($data, $userInfo);
+
+        $info = UserInfo::detail($userInfo['user_id']);
+        $userInfo->transaction(function () use ($data, $userInfo, $info) {
+
+            try {
+                $userData = [
+                    'user_id'=> $userInfo['user_id'],
+                    'nick_name'=> $data['nick_name'],
+                    'real_name'=> $data['real_name'],
+                    'gender'=> (int)$data['gender'],
+                    'age'=> (int)$data['age'],
+                    'student_no'=>$data['student_no'],
+                    'user_login'=> $data['user_login'],
+                    'mobile'=> $data['mobile'],
+                ];
+
+                if($userInfo['user_type']<=0){
+                    $userData['user_type'] = (int)$data['user_type'];
+                }
+
+                $userInfo->save($userData);
+
+                $infoData = [
+                    'user_id'=> $userInfo['user_id'],
+                    'school_id'=> (int)$data['school_id'],
+                    'position'=> (int)$data['position'],
+                    'qq'=> (int)$data['qq'],
+                    'idcard'=> $data['idcard'],
+                    'idcard_front_img'=> $data['idcard_front_img'],
+                    'work_certify'=> $data['work_certify'],
+                    'education_certify'=> $data['education_certify'],
+                    'parent_name'=> $data['parent_name'],
+                    'admission_year'=> $data['admission_year'],
+                ];
+
+                if(empty($info)){
+                    $infoData['status'] = 2;
+                }
+
+                $info->save($infoData);
+            } catch(\Exception $exception){
+                throwError('保存失败');
+            }
+        });
+
+        return $info || $data['user_type']==3? '保存成功' : '保存成功,等待审核';
+
+    }
+
+    /**
+     * 验证用户信息
+     * @param array $data
+     * @param array $userInfo
+     * @return bool
+     * @throws BaseException
+     */
+    private function checkInfo(array $data, array $userInfo): bool
+    {
+        $validate = new ValidateInfo;
+        if (!$validate->check($data)) {
+            throwError($validate->getError());
+        }
+
+        if($data['user_type'] == 2 && empty($data['parent_name'])){
+            throwError('家长姓名不为空');
+        }
+
+        if(empty($data['school_id'])){
+            throwError('学校不为空');
+        }
+
+        if($data['user_type'] == 1){
+            if(empty($data['admission_year'])){
+                throwError('请选择入学年份');
+            }
+
+            if(empty($data['education_certify'])){
+                throwError('请上传教育证明');
+            }
+        }else if($data['user_type'] == 3){
+            if(empty($data['position'])){
+                throwError('请选择职务');
+            }
+
+            if(empty($data['work_certify'])){
+                throwError('请上传职务证明');
+            }
+        }
+
+        if($userInfo['mobile'] != $data['mobile']) {
+            if (empty($data['smsCode'])) {
+                throwError('短信验证码不为空');
+            }
+
+            // 验证短信验证码是否匹配
+            if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
+                throwError('短信验证码不正确');
+            }
+        }
+
+        if($data['idcard'] && $userInfo['user_id'] == UserInfo::checkExistByIdcard($data['idcard'])){
+            throwError('身份证号码已被使用');
+        }
+
+        if(empty($data['idcard_front_img'])){
+            throwError('请上传身份证明');
+        }
+
+        if($data['mobile'] && $userInfo['user_id'] == self::checkExistByMobile($data['mobile'])){
+            throwError('手机号码已被使用');
+        }
+
+    }
 }

+ 18 - 0
app/api/model/UserInfo.php

@@ -11,6 +11,7 @@
 declare (strict_types=1);
 
 namespace app\api\model;
+use app\common\model\User;
 use app\common\model\UserInfo as UserInfoModel;
 
 /**
@@ -28,4 +29,21 @@ class UserInfo extends UserInfoModel
         'store_id',
         'update_time'
     ];
+
+    /**
+     * 获取用户信息
+     * @param $where
+     * @param array $with
+     * @return static|array|false|null
+     */
+    public static function detail($where, array $with = [])
+    {
+        $filter = [];
+        if (is_array($where)) {
+            $filter = array_merge($filter, $where);
+        } else {
+            $filter['user_id'] = (int)$where;
+        }
+        return static::get($filter, $with);
+    }
 }

+ 58 - 0
app/api/validate/user/Info.php

@@ -0,0 +1,58 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\api\validate\user;
+
+use think\Validate;
+
+/**
+ * 验证类:用户资料
+ * Class Info
+ * @package app\api\validate\user
+ */
+class Info extends Validate
+{
+    /**
+     * 验证规则
+     * @var array
+     */
+    protected $rule = [
+        // 短信验证码 (用户输入)
+        'qq' => ['number'],
+        // 用户手机号
+        'idcard' => ['idCard','max:20'],
+        'real_name'=> ['require','max:20'],
+        'nick_name'=> ['require','max:100'],
+        'user_login'=> ['require','max:30'],
+        'mobile'=> ['require','mobile'],
+        'gender'=> ['require']
+    ];
+
+    /**
+     * 验证提示
+     * @var string[]
+     */
+    protected $message  =   [
+        'qq.number' => 'QQ号格式不正确',
+        'idcard.idCard' => '身份证号格式不正确',
+        'idcard.max' => '身份证号格式不正确',
+        'mobile.require' => '手机号不为空',
+        'mobile.mobile' => '手机号格式不正确',
+        'user_login.require' => '账号不为空',
+        'user_login.max' => '账号格式不正确',
+        'nick_name.require' => '昵称不为空',
+        'nick_name.max' => '昵称格式不正确',
+        'real_name.require' => '姓名不为空',
+        'real_name.max' => '姓名格式不正确',
+        'gender.require' => '性别不为空',
+    ];
+}

+ 14 - 0
app/common/model/UserInfo.php

@@ -27,4 +27,18 @@ class UserInfo extends BaseModel
     // 定义主键
     protected $pk = 'id';
 
+    /**
+     * 指定的手机号是否已存在
+     * @param string $mobile
+     * @return bool
+     */
+    public static function checkExistByIdcard(string $idcard): bool
+    {
+        $model = new static;
+        return (bool)$model->alias('a')
+            ->leftJoin('user u','u.user_id=a.user_id')
+            ->where('a.idcard', '=', $idcard)
+            ->where('u.is_delete', '=', 0)
+            ->value('a.user_id');
+    }
 }