| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- 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;
- use app\common\model\User as UserModel;
- use cores\exception\BaseException;
- use yiovo\captcha\facade\CaptchaApi;
- /**
- * 用户模型类
- * Class User
- * @package app\api\model
- */
- class User extends UserModel
- {
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'open_id',
- 'is_delete',
- 'store_id',
- 'create_time',
- 'update_time'
- ];
- /**
- * 获取器:隐藏手机号中间四位
- * @param string $value
- * @return string
- */
- public function getMobileAttr(string $value): string
- {
- return strlen($value) === 11 ? hide_mobile($value) : $value;
- }
- /**
- * 获取用户信息
- * @param string $token
- * @return User|array|false|null
- * @throws BaseException
- */
- public static function getUserByToken(string $token)
- {
- // 检查登录态是否存在
- if (!Cache::has($token)) {
- return false;
- }
- // 用户的ID
- $userId = (int)Cache::get($token)['user']['user_id'];
- // 用户基本信息
- $userInfo = self::detail($userId);
- $userInfo['info']=$userInfo['info']? $userInfo['info'] : [];
- if (empty($userInfo) || $userInfo['is_delete']) {
- throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
- }
- // 获取用户关联的第三方用户信息(当前客户端)
- try {
- if(getPlatform() && getPlatform() != 'MP-WEIXIN'){
- $userInfo['currentOauth'] = UserOauthModel::getOauth($userId, getPlatform());
- }
- } catch (\Throwable $e) {
- throwError($e->getMessage());
- }
- return $userInfo;
- }
- /**
- * 绑定手机号(当前登录用户)
- * @param array $data
- * @return bool
- * @throws BaseException
- */
- public function bindMobile(array $data): bool
- {
- // 当前登录的用户信息
- $userInfo = UserService::getCurrentLoginUser(true);
- // 验证绑定的手机号
- $this->checkBindMobile($data);
- // 更新手机号记录
- return $userInfo->save(['mobile' => $data['mobile']]);
- }
- /**
- * 验证绑定的手机号
- * @param array $data
- * @return void
- * @throws BaseException
- */
- private function checkBindMobile(array $data): void
- {
- // 验证短信验证码是否匹配
- if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
- throwError('短信验证码不正确');
- }
- // 判断手机号是否已存在
- if (static::checkExistByMobile($data['mobile'])) {
- throwError('很抱歉,该手机号已绑定其他账户');
- }
- }
- /**
- * @param array $data
- * @return string
- * @throws BaseException
- */
- public function saveInfo(array $data): string
- {
- var_dump($data);
- // 修改手机号需要验证验证码
- $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, $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('手机号码已被使用');
- }
- }
- }
|