StudentEduexp.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use app\api\validate\user\EducationExp;
  14. use cores\BaseModel;
  15. use think\model\relation\HasOne;
  16. /**
  17. * 学生教育经历模型类
  18. * Class StudentEduexp
  19. * @package app\common\model
  20. */
  21. class StudentEduexp extends BaseModel
  22. {
  23. protected $globalScope = [''];
  24. // 定义表名
  25. protected $name = 'student_eduexp';
  26. // 定义主键
  27. protected $pk = 'id';
  28. /**
  29. * 获取凭证图片
  30. * @param string $value
  31. * @return string
  32. */
  33. public function getCertifyImgAttr(string $value): string
  34. {
  35. return $value? getPreview($value) :'';
  36. }
  37. /**
  38. * 获取用户最近的教育经历
  39. * @param $userId
  40. * @return StudentEduexp|array|mixed|\think\Model|null
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public static function getInfoByUser($userId)
  46. {
  47. if($userId<=0){
  48. return false;
  49. }
  50. return self::where(['user_id'=> $userId])->order('id desc')->find();
  51. }
  52. /**
  53. * @param $params
  54. * @return bool
  55. * @throws \cores\exception\BaseException
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function setData($params)
  61. {
  62. $validate = new EducationExp();
  63. if(!$validate->check($params)){
  64. throwError($validate->getError());
  65. }
  66. $userInfo = \app\api\service\User::getCurrentLoginUser(true);
  67. $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0;
  68. $info = self::getInfoByUser($userId);
  69. $certifyImg = isset($params['certify_img'])? $params['certify_img'] : '';
  70. if($info && $info['status'] == 2 && $checkModify = self::checkModify($params, $info) && empty($certifyImg)){
  71. throwError('您已修改过信息,请先上传凭证');
  72. }
  73. $data = [
  74. 'user_id'=> $userId,
  75. 'education'=> isset($params['education'])? $params['education'] : '',
  76. 'school_name'=> isset($params['school_name'])? $params['school_name'] : '',
  77. 'speciality'=> isset($params['speciality'])? $params['speciality'] : '',
  78. 'grade'=> isset($params['grade'])? $params['grade'] : '',
  79. 'status'=> 1,
  80. ];
  81. if($certifyImg){
  82. $data['certify_img'] = $certifyImg;
  83. }
  84. if($info){
  85. return $info->save($data);
  86. }else{
  87. return self::save($data);
  88. }
  89. }
  90. /**
  91. * 验证是否修改过内容
  92. * @param $params
  93. * @param $info
  94. * @return bool
  95. */
  96. public static function checkModify($params, $info)
  97. {
  98. foreach ($params as $k => $v){
  99. if($k != 'certify_img' && isset($info[$k]) && $info[$k] != $v){
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. }