User.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 (empty($userInfo) || $userInfo['is_delete']) {
  65. throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
  66. }
  67. // 获取用户关联的第三方用户信息(当前客户端)
  68. try {
  69. if(getPlatform() && getPlatform() != 'MP-WEIXIN'){
  70. $userInfo['currentOauth'] = UserOauthModel::getOauth($userId, getPlatform());
  71. }
  72. } catch (\Throwable $e) {
  73. throwError($e->getMessage());
  74. }
  75. return $userInfo;
  76. }
  77. /**
  78. * 绑定手机号(当前登录用户)
  79. * @param array $data
  80. * @return bool
  81. * @throws BaseException
  82. */
  83. public function bindMobile(array $data): bool
  84. {
  85. // 当前登录的用户信息
  86. $userInfo = UserService::getCurrentLoginUser(true);
  87. // 验证绑定的手机号
  88. $this->checkBindMobile($data);
  89. // 更新手机号记录
  90. return $userInfo->save(['mobile' => $data['mobile']]);
  91. }
  92. /**
  93. * 验证绑定的手机号
  94. * @param array $data
  95. * @return void
  96. * @throws BaseException
  97. */
  98. private function checkBindMobile(array $data): void
  99. {
  100. // 验证短信验证码是否匹配
  101. if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
  102. throwError('短信验证码不正确');
  103. }
  104. // 判断手机号是否已存在
  105. if (static::checkExistByMobile($data['mobile'])) {
  106. throwError('很抱歉,该手机号已绑定其他账户');
  107. }
  108. }
  109. /**
  110. * @param array $data
  111. * @return string
  112. * @throws BaseException
  113. */
  114. public function saveInfo(array $data): string
  115. {
  116. var_dump($data);
  117. // 修改手机号需要验证验证码
  118. $userInfo = UserService::getCurrentLoginUser(true);
  119. // 验证信息
  120. $this->checkInfo($data, $userInfo);
  121. $info = UserInfo::detail($userInfo['user_id']);
  122. $userInfo->transaction(function () use ($data, $userInfo, $info) {
  123. try {
  124. $userData = [
  125. 'user_id'=> $userInfo['user_id'],
  126. 'nick_name'=> $data['nick_name'],
  127. 'real_name'=> $data['real_name'],
  128. 'gender'=> (int)$data['gender'],
  129. 'age'=> (int)$data['age'],
  130. 'student_no'=>$data['student_no'],
  131. 'user_login'=> $data['user_login'],
  132. 'mobile'=> $data['mobile'],
  133. ];
  134. if($userInfo['user_type']<=0){
  135. $userData['user_type'] = (int)$data['user_type'];
  136. }
  137. $userInfo->save($userData);
  138. $infoData = [
  139. 'user_id'=> $userInfo['user_id'],
  140. 'school_id'=> (int)$data['school_id'],
  141. 'position'=> (int)$data['position'],
  142. 'qq'=> (int)$data['qq'],
  143. 'idcard'=> $data['idcard'],
  144. 'idcard_front_img'=> $data['idcard_front_img'],
  145. 'work_certify'=> $data['work_certify'],
  146. 'education_certify'=> $data['education_certify'],
  147. 'parent_name'=> $data['parent_name'],
  148. 'admission_year'=> $data['admission_year'],
  149. ];
  150. if(empty($info)){
  151. $infoData['status'] = 2;
  152. }
  153. $info->save($infoData);
  154. } catch(\Exception $exception){
  155. throwError('保存失败');
  156. }
  157. });
  158. return $info || $data['user_type']==3? '保存成功' : '保存成功,等待审核';
  159. }
  160. /**
  161. * 验证用户信息
  162. * @param array $data
  163. * @param array $userInfo
  164. * @return bool
  165. * @throws BaseException
  166. */
  167. private function checkInfo(array $data, $userInfo): bool
  168. {
  169. $validate = new ValidateInfo;
  170. if (!$validate->check($data)) {
  171. throwError($validate->getError());
  172. }
  173. if($data['user_type'] == 2 && empty($data['parent_name'])){
  174. throwError('家长姓名不为空');
  175. }
  176. if(empty($data['school_id'])){
  177. throwError('学校不为空');
  178. }
  179. if($data['user_type'] == 1){
  180. if(empty($data['admission_year'])){
  181. throwError('请选择入学年份');
  182. }
  183. if(empty($data['education_certify'])){
  184. throwError('请上传教育证明');
  185. }
  186. }else if($data['user_type'] == 3){
  187. if(empty($data['position'])){
  188. throwError('请选择职务');
  189. }
  190. if(empty($data['work_certify'])){
  191. throwError('请上传职务证明');
  192. }
  193. }
  194. if($userInfo['mobile'] != $data['mobile']) {
  195. if (empty($data['smsCode'])) {
  196. throwError('短信验证码不为空');
  197. }
  198. // 验证短信验证码是否匹配
  199. if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
  200. throwError('短信验证码不正确');
  201. }
  202. }
  203. if($data['idcard'] && $userInfo['user_id'] == UserInfo::checkExistByIdcard($data['idcard'])){
  204. throwError('身份证号码已被使用');
  205. }
  206. if(empty($data['idcard_front_img'])){
  207. throwError('请上传身份证明');
  208. }
  209. if($data['mobile'] && $userInfo['user_id'] == self::checkExistByMobile($data['mobile'])){
  210. throwError('手机号码已被使用');
  211. }
  212. }
  213. }