User.php 13 KB

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