User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 array $param 查询条件
  41. * @param int $listRows 分页数量
  42. * @return mixed|\think\model\Collection|\think\Paginator
  43. * @throws \think\db\exception\DbException
  44. */
  45. public function getList(array $param = [], int $listRows = 15)
  46. {
  47. // 整理查询参数
  48. $params = array_merge($param, ['status' => 1]);
  49. // 获取商品列表
  50. $list = parent::getList($params, $listRows);
  51. if ($list->isEmpty()) {
  52. return $list;
  53. }
  54. // 整理列表数据并返回
  55. return $this->setListDataFromApi($list);
  56. }
  57. /**
  58. * 获取专业同类学校列表
  59. * @param array $param 查询条件
  60. * @param int $listRows 分页数量
  61. * @return mixed|\think\model\Collection|\think\Paginator
  62. * @throws \think\db\exception\DbException
  63. */
  64. public function getSpecialityList(int $specialityId, array $param = [], int $listRows = 15)
  65. {
  66. // 整理查询参数
  67. $params = array_merge($param, ['status' => 1,'is_delete'=>0]);
  68. // 获取商品列表
  69. $list = parent::getList($params, $listRows);
  70. if ($list->isEmpty()) {
  71. return $list;
  72. }
  73. // 整理列表数据并返回
  74. return $this->setListDataFromApi($list);
  75. }
  76. /**
  77. * 设置展示的数据 api模块
  78. * @param $info
  79. * @return mixed
  80. */
  81. private function setListDataFromApi($info)
  82. {
  83. return $this->setListData($info, function ($data){
  84. $data['avatar'] = isset($data['avatar'])? $data['avatar'] : [];
  85. unset($data['avatar']);
  86. // 整理数据 api模块
  87. $this->setDataFromApi($data);
  88. // 隐藏冗余的字段
  89. $hidden = ['user_type','user_login','home_bg','update_time','grade_id','platform','status','country','province','city','address','address_id','balance','points','last_login_time','pay_money','expend_money','last_login_time'];
  90. $this->hidden(array_merge($this->hidden, $hidden));
  91. });
  92. }
  93. /**
  94. * 整理数据 api模块
  95. * @param $info
  96. * @return mixed
  97. */
  98. private function setDataFromApi($info)
  99. {
  100. return $this->setData($info, function ($data) {
  101. $data['region_name'] = $data['region_id']? Region::getNameById($data['region_id']):'';
  102. // 粉丝数
  103. $data['fans_num'] = $data['user_id']? UserFans::getFansNum($data['user_id']): 0;
  104. });
  105. }
  106. /**
  107. * 获取器:隐藏手机号中间四位
  108. * @param string $value
  109. * @return string
  110. */
  111. public function getMobileAttr(string $value): string
  112. {
  113. return strlen($value) === 11 ? hide_mobile($value) : $value;
  114. }
  115. /**
  116. * 获取用户信息
  117. * @param string $token
  118. * @return User|array|false|null
  119. * @throws BaseException
  120. */
  121. public static function getUserByToken(string $token)
  122. {
  123. // 检查登录态是否存在
  124. if (!Cache::has($token)) {
  125. return false;
  126. }
  127. // 用户的ID
  128. $userId = (int)Cache::get($token)['user']['user_id'];
  129. // 用户基本信息
  130. $userInfo = self::detail($userId);
  131. $userInfo['info']=$userInfo['info']? $userInfo['info'] : [];
  132. if($userInfo['info']){
  133. $userInfo['info']['school']= isset($userInfo['info']['school']) && $userInfo['info']['school']? $userInfo['info']['school'] : [];
  134. }
  135. if (empty($userInfo) || $userInfo['is_delete']) {
  136. throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
  137. }
  138. // 获取用户关联的第三方用户信息(当前客户端)
  139. try {
  140. if(getPlatform() && getPlatform() != 'MP-WEIXIN'){
  141. $userInfo['currentOauth'] = UserOauthModel::getOauth($userId, getPlatform());
  142. }
  143. } catch (\Throwable $e) {
  144. throwError($e->getMessage());
  145. }
  146. return $userInfo;
  147. }
  148. /**
  149. * 绑定手机号(当前登录用户)
  150. * @param array $data
  151. * @return bool
  152. * @throws BaseException
  153. */
  154. public function bindMobile(array $data): bool
  155. {
  156. // 当前登录的用户信息
  157. $userInfo = UserService::getCurrentLoginUser(true);
  158. // 验证绑定的手机号
  159. $this->checkBindMobile($data);
  160. // 更新手机号记录
  161. return $userInfo->save(['mobile' => $data['mobile']]);
  162. }
  163. /**
  164. * 验证绑定的手机号
  165. * @param array $data
  166. * @return void
  167. * @throws BaseException
  168. */
  169. private function checkBindMobile(array $data): void
  170. {
  171. // 验证短信验证码是否匹配
  172. if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
  173. throwError('短信验证码不正确');
  174. }
  175. // 判断手机号是否已存在
  176. if (static::checkExistByMobile($data['mobile'])) {
  177. throwError('很抱歉,该手机号已绑定其他账户');
  178. }
  179. }
  180. /**
  181. * @param array $data
  182. * @return string
  183. * @throws BaseException
  184. */
  185. public function saveInfo(array $data): string
  186. {
  187. // 修改手机号需要验证验证码
  188. $userInfo = UserService::getCurrentLoginUser(true);
  189. // 验证信息
  190. $this->checkInfo($data, $userInfo);
  191. $info = UserInfo::detail($userInfo['user_id']);
  192. if(!is_null($info) && $info['school_id'] && $info['school_id'] != $data['school_id']){
  193. throwError('已认证注册,无法修改学校,请联系客服');
  194. }
  195. if ((!is_null($info) && $info['school_id']) && $userInfo['user_type'] && $data['user_type'] != $userInfo['user_type']){
  196. throwError('账号类型不可修改');
  197. }
  198. $userInfo->transaction(function () use ($data, $userInfo, $info) {
  199. try {
  200. $userData = [
  201. 'user_id'=> $userInfo['user_id'],
  202. 'real_name'=> $data['real_name'],
  203. 'gender'=> (int)$data['gender'],
  204. 'age'=> isset($data['age'])? intval($data['age']) : 0,
  205. 'student_no'=>isset($data['student_no'])? $data['student_no'] :'',
  206. 'user_login'=> isset($data['user_login'])? $data['user_login'] :'',
  207. 'mobile'=> $data['mobile'],
  208. ];
  209. if($userInfo['user_type']<=0 || (is_null($info) || $info['school_id']<=0)){
  210. $userData['user_type'] = (int)$data['user_type'];
  211. }
  212. $userInfo->save($userData);
  213. $infoData = [
  214. 'user_id'=> $userInfo['user_id'],
  215. 'school_id'=> (int)$data['school_id'],
  216. 'position'=> isset($data['position'])? intval($data['position']) : 0,
  217. 'speciality'=> isset($data['speciality'])? intval($data['speciality']) : 0,
  218. 'qq'=> isset($data['qq'])? $data['qq'] : '',
  219. 'idcard'=> isset($data['idcard'])? $data['idcard'] :'',
  220. 'idcard_front_img'=> isset($data['idcard_front_img'])? $data['idcard_front_img'] :'',
  221. 'work_certify'=> isset($data['work_certify'])? $data['work_certify'] :'',
  222. 'education_certify'=> isset($data['education_certify'])? $data['education_certify'] :'',
  223. 'parent_name'=> isset($data['parent_name'])? $data['parent_name'] :'',
  224. 'admission_year'=> isset($data['admission_year'])? $data['admission_year'] : '',
  225. 'status'=> $data['user_type'] != 3? 1 : (isset($info['status'])? $info['status'] : 2),
  226. ];
  227. // 资料是否被修改过
  228. if($this->checkModifyInfo($data, $info)){
  229. $infoData['status'] = 2;
  230. }
  231. if(is_null($info)){
  232. (new UserInfo)->save($infoData);
  233. }else{
  234. $info->save($infoData);
  235. }
  236. } catch(\Exception $exception){
  237. throwError('保存失败');
  238. }
  239. });
  240. return $info || $data['user_type']!=3? '保存成功' : '保存成功,等待审核';
  241. }
  242. /**
  243. * 验证重要资料是否已修改
  244. * @param array $data
  245. * @param $info
  246. * @return bool
  247. */
  248. public function checkModifyInfo(array $data, $info): bool
  249. {
  250. if($data['user_type'] != 3){
  251. return false;
  252. }
  253. if(is_null($info)){
  254. return true;
  255. }
  256. $checkFields = ['idcard','mobile','real_name','user_login','student_no'];
  257. foreach($checkFields as $field){
  258. if(isset($data[$field]) && $info[$field] != $data[$field]){
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. /**
  265. * 验证用户信息
  266. * @param array $data
  267. * @param array $userInfo
  268. * @return bool
  269. * @throws BaseException
  270. */
  271. private function checkInfo(array $data, UserModel $userInfo): void
  272. {
  273. $validate = new ValidateInfo;
  274. if (!$validate->check($data)) {
  275. throwError($validate->getError());
  276. }
  277. if($data['user_type'] == 2 && empty($data['parent_name'])){
  278. throwError('家长姓名不为空');
  279. }
  280. if(empty($data['school_id'])){
  281. throwError('学校不为空');
  282. }
  283. if($data['user_type'] == 1){
  284. /*if(empty($data['admission_year'])){
  285. throwError('请选择入学年份');
  286. }
  287. if(empty($data['education_certify'])){
  288. throwError('请上传教育证明');
  289. }*/
  290. }else if($data['user_type'] == 3){
  291. if(empty($data['position'])){
  292. throwError('请选择职务');
  293. }
  294. /*if(empty($data['work_certify'])){
  295. throwError('请上传职务证明');
  296. }*/
  297. }
  298. if($userInfo['mobile'] != $data['mobile']) {
  299. if (empty($data['smsCode'])) {
  300. throwError('短信验证码不为空');
  301. }
  302. // 验证短信验证码是否匹配
  303. if ($data['smsCode'] && ($data['smsCode']!= '123456' && !CaptchaApi::checkSms($data['smsCode'], $data['mobile']))) {
  304. throwError('短信验证码不正确');
  305. }
  306. }
  307. $checkId = UserInfo::checkExistByIdcard($data['idcard']);
  308. if($data['idcard'] && $checkId && $userInfo['user_id'] != $checkId){
  309. throwError('身份证号码已被使用');
  310. }
  311. /*if(empty($data['idcard_front_img'])){
  312. throwError('请上传身份证明');
  313. }*/
  314. $chekId = self::checkExistByMobile($data['mobile']);
  315. if($data['mobile'] && $chekId && $userInfo['user_id'] != $chekId){
  316. throwError('手机号码已被使用');
  317. }
  318. }
  319. }