User.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\api\validate\user\Info as ValidateInfo;
  14. use think\facade\Cache;
  15. use app\api\service\User as UserService;
  16. use app\api\model\UserOauth as UserOauthModel;
  17. use app\common\model\User as UserModel;
  18. use cores\exception\BaseException;
  19. use yiovo\captcha\facade\CaptchaApi;
  20. /**
  21. * 用户模型类
  22. * Class User
  23. * @package app\api\model
  24. */
  25. class User extends UserModel
  26. {
  27. /**
  28. * 隐藏字段
  29. * @var array
  30. */
  31. protected $hidden = [
  32. 'open_id',
  33. 'is_delete',
  34. 'store_id',
  35. 'create_time',
  36. 'update_time'
  37. ];
  38. /**
  39. * 获取器:隐藏手机号中间四位
  40. * @param string $value
  41. * @return string
  42. */
  43. public function getMobileAttr(string $value): string
  44. {
  45. return strlen($value) === 11 ? hide_mobile($value) : $value;
  46. }
  47. /**
  48. * 获取用户信息
  49. * @param string $token
  50. * @return User|array|false|null
  51. * @throws BaseException
  52. */
  53. public static function getUserByToken(string $token)
  54. {
  55. // 检查登录态是否存在
  56. if (!Cache::has($token)) {
  57. return false;
  58. }
  59. // 用户的ID
  60. $userId = (int)Cache::get($token)['user']['user_id'];
  61. // 用户基本信息
  62. $userInfo = self::detail($userId);
  63. $userInfo['info']=$userInfo['info']? $userInfo['info'] : [];
  64. if($userInfo['info']){
  65. $userInfo['info']['school']= isset($userInfo['info']['school']) && $userInfo['info']['school']? $userInfo['info']['school'] : [];
  66. }
  67. if (empty($userInfo) || $userInfo['is_delete']) {
  68. throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
  69. }
  70. // 获取用户关联的第三方用户信息(当前客户端)
  71. try {
  72. if(getPlatform() && getPlatform() != 'MP-WEIXIN'){
  73. $userInfo['currentOauth'] = UserOauthModel::getOauth($userId, getPlatform());
  74. }
  75. } catch (\Throwable $e) {
  76. throwError($e->getMessage());
  77. }
  78. return $userInfo;
  79. }
  80. /**
  81. * 绑定手机号(当前登录用户)
  82. * @param array $data
  83. * @return bool
  84. * @throws BaseException
  85. */
  86. public function bindMobile(array $data): bool
  87. {
  88. // 当前登录的用户信息
  89. $userInfo = UserService::getCurrentLoginUser(true);
  90. // 验证绑定的手机号
  91. $this->checkBindMobile($data);
  92. // 更新手机号记录
  93. return $userInfo->save(['mobile' => $data['mobile']]);
  94. }
  95. /**
  96. * 验证绑定的手机号
  97. * @param array $data
  98. * @return void
  99. * @throws BaseException
  100. */
  101. private function checkBindMobile(array $data): void
  102. {
  103. // 验证短信验证码是否匹配
  104. if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
  105. throwError('短信验证码不正确');
  106. }
  107. // 判断手机号是否已存在
  108. if (static::checkExistByMobile($data['mobile'])) {
  109. throwError('很抱歉,该手机号已绑定其他账户');
  110. }
  111. }
  112. /**
  113. * @param array $data
  114. * @return string
  115. * @throws BaseException
  116. */
  117. public function saveInfo(array $data): string
  118. {
  119. // 修改手机号需要验证验证码
  120. $userInfo = UserService::getCurrentLoginUser(true);
  121. // 验证信息
  122. $this->checkInfo($data, $userInfo);
  123. $info = UserInfo::detail($userInfo['user_id']);
  124. if(!is_null($info) && $info['school_id'] != $data['school_id']){
  125. throwError('已认证注册,无法修改学校,请联系客服');
  126. }
  127. if ($userInfo['user_type'] && $data['user_type'] != $userInfo['user_type']){
  128. throwError('账号类型不可修改');
  129. }
  130. $userInfo->transaction(function () use ($data, $userInfo, $info) {
  131. try {
  132. $userData = [
  133. 'user_id'=> $userInfo['user_id'],
  134. 'nick_name'=> $data['nick_name'],
  135. 'real_name'=> $data['real_name'],
  136. 'gender'=> (int)$data['gender'],
  137. 'age'=> isset($data['age'])? intval($data['age']) : 0,
  138. 'student_no'=>isset($data['student_no'])? $data['student_no'] :'',
  139. 'user_login'=> $data['user_login'],
  140. 'mobile'=> $data['mobile'],
  141. ];
  142. if($userInfo['user_type']<=0){
  143. $userData['user_type'] = (int)$data['user_type'];
  144. }
  145. $userInfo->save($userData);
  146. $infoData = [
  147. 'user_id'=> $userInfo['user_id'],
  148. 'school_id'=> (int)$data['school_id'],
  149. 'position'=> isset($data['position'])? intval($data['position']) : 0,
  150. 'speciality'=> isset($data['speciality'])? intval($data['speciality']) : 0,
  151. 'qq'=> isset($data['qq'])? $data['qq'] : '',
  152. 'idcard'=> isset($data['idcard'])? $data['idcard'] :'',
  153. 'idcard_front_img'=> isset($data['idcard_front_img'])? $data['idcard_front_img'] :'',
  154. 'work_certify'=> isset($data['work_certify'])? $data['work_certify'] :'',
  155. 'education_certify'=> isset($data['education_certify'])? $data['education_certify'] :'',
  156. 'parent_name'=> isset($data['parent_name'])? $data['parent_name'] :'',
  157. 'admission_year'=> isset($data['admission_year'])? $data['admission_year'] : '',
  158. 'status'=> $data['user_type'] != 3? 1 : (isset($info['status'])? $info['status'] : 2),
  159. ];
  160. // 资料是否被修改过
  161. if($this->checkModifyInfo($data, $info)){
  162. $infoData['status'] = 2;
  163. }
  164. if(is_null($info)){
  165. (new UserInfo)->save($infoData);
  166. }else{
  167. $info->save($infoData);
  168. }
  169. } catch(\Exception $exception){
  170. throwError('保存失败');
  171. }
  172. });
  173. return $info || $data['user_type']!=3? '保存成功' : '保存成功,等待审核';
  174. }
  175. /**
  176. * 验证重要资料是否已修改
  177. * @param array $data
  178. * @param $info
  179. * @return bool
  180. */
  181. public function checkModifyInfo(array $data, UserInfo $info): bool
  182. {
  183. if($data['user_type'] != 3){
  184. return false;
  185. }
  186. if(is_null($info)){
  187. return true;
  188. }
  189. $checkFields = ['idcard','mobile','real_name','user_login','student_no'];
  190. foreach($checkFields as $field){
  191. if(isset($data[$field]) && $info[$field] != $data[$field]){
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. /**
  198. * 验证用户信息
  199. * @param array $data
  200. * @param array $userInfo
  201. * @return bool
  202. * @throws BaseException
  203. */
  204. private function checkInfo(array $data, UserModel $userInfo): void
  205. {
  206. $validate = new ValidateInfo;
  207. if (!$validate->check($data)) {
  208. throwError($validate->getError());
  209. }
  210. if($data['user_type'] == 2 && empty($data['parent_name'])){
  211. throwError('家长姓名不为空');
  212. }
  213. if(empty($data['school_id'])){
  214. throwError('学校不为空');
  215. }
  216. if($data['user_type'] == 1){
  217. if(empty($data['admission_year'])){
  218. throwError('请选择入学年份');
  219. }
  220. if(empty($data['speciality'])){
  221. throwError('请选择就读专业');
  222. }
  223. if(empty($data['education_certify'])){
  224. throwError('请上传教育证明');
  225. }
  226. }else if($data['user_type'] == 3){
  227. if(empty($data['position'])){
  228. throwError('请选择职务');
  229. }
  230. if(empty($data['work_certify'])){
  231. throwError('请上传职务证明');
  232. }
  233. }
  234. if($userInfo['mobile'] != $data['mobile']) {
  235. if (empty($data['smsCode'])) {
  236. throwError('短信验证码不为空');
  237. }
  238. // 验证短信验证码是否匹配
  239. if ($data['smsCode'] && ($data['smsCode']!= '123456' && !CaptchaApi::checkSms($data['smsCode'], $data['mobile']))) {
  240. throwError('短信验证码不正确');
  241. }
  242. }
  243. $checkId = UserInfo::checkExistByIdcard($data['idcard']);
  244. if($data['idcard'] && $checkId && $userInfo['user_id'] != $checkId){
  245. throwError('身份证号码已被使用');
  246. }
  247. if(empty($data['idcard_front_img'])){
  248. throwError('请上传身份证明');
  249. }
  250. $chekId = self::checkExistByMobile($data['mobile']);
  251. if($data['mobile'] && $chekId && $userInfo['user_id'] != $chekId){
  252. throwError('手机号码已被使用');
  253. }
  254. }
  255. }