User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. use think\Exception;
  6. class User extends Model
  7. {
  8. // 表名
  9. protected $name = 'user';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = false;
  15. protected $deleteTime = false;
  16. // 追加属性
  17. protected $append = [
  18. 'yaoqingtime_text',
  19. 'gender_text',
  20. 'idcard_status_text',
  21. 'real_status_text',
  22. 'logintime_text',
  23. 'jointime_text',
  24. 'status_text',
  25. 'hide_is_finishinfo_text',
  26. 'is_active_text',
  27. 'open_match_video_text',
  28. 'open_match_audio_text',
  29. 'jinyantime_text',
  30. 'jinyantype_text',
  31. 'is_shouchong_text'
  32. ];
  33. protected static function init()
  34. {
  35. self::beforeUpdate(function ($row) {
  36. $changed = $row->getChangedData();
  37. //如果有修改密码
  38. if (isset($changed['real_status'])) {
  39. if ($changed['real_status']) {
  40. $task_rs = \app\common\model\TaskLog::tofinish($row->id,6);
  41. } else {
  42. }
  43. }
  44. });
  45. self::beforeUpdate(function ($row) {
  46. $changed = $row->getChangedData();
  47. //检验 超推上级UID ,intro_uid
  48. if (isset($row['intro_uid'])) {
  49. if ($row['intro_uid']) {
  50. if($row['invite_uid']){
  51. throw new Exception('超推网上级UID 与 直推上级UID 不能共存');
  52. }
  53. $intro_info = Db::name('user')->where('id',$row['intro_uid'])->find();
  54. if(empty($intro_info)){
  55. throw new Exception('不存在的 超推网上级UID用户');
  56. }
  57. if($intro_info['group_id'] == 1){
  58. throw new Exception('超推网上级UID用户 不能是普通用户');
  59. }
  60. }
  61. }
  62. //检验二级超推
  63. if($row['group_id'] == 2){
  64. if(!$row['agent_id']){
  65. throw new Exception('二级超推必须填写 上级一级代理UID');
  66. }
  67. $agent_info = Db::name('user')->where('group_id',3)->where('id',$row['agent_id'])->find();
  68. if(empty($agent_info)){
  69. throw new Exception('不存在的 上级一级代理UID用户');
  70. }
  71. if(!$row['intro_uid']){
  72. throw new Exception('二级超推必须填写 超推网上级UID');
  73. }
  74. if($row['intro_uid'] != $row['id'] && $row['intro_uid'] != $row['agent_id']){
  75. throw new Exception('二级超推的 超推网上级UID 只能是 上级一级代理UID 或 自己的id');
  76. }
  77. }else{
  78. $row['agent_id'] = '';
  79. }
  80. //检验 直推上级UID ,invite_uid
  81. if (isset($row['invite_uid'])) {
  82. if ($row['invite_uid']) {
  83. if($row['intro_uid']){
  84. throw new Exception('超推网上级UID 与 直推上级UID 不能共存');
  85. }
  86. $invite_info = Db::name('user')->where('id',$row['invite_uid'])->find();
  87. if(empty($invite_info)){
  88. throw new Exception('不存在的 直推上级UID用户');
  89. }
  90. }
  91. }
  92. //如果有修改密码
  93. /*if (isset($changed['password'])) {
  94. if ($changed['password']) {
  95. $salt = \fast\Random::alnum();
  96. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  97. $row->salt = $salt;
  98. } else {
  99. unset($row->password);
  100. }
  101. }*/
  102. //手机去重
  103. /*if (isset($changed['mobile'])) {
  104. if($changed['mobile']){
  105. $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  106. if ($exists) {
  107. abort(500,'区号手机号已经被使用');
  108. }
  109. }else {
  110. unset($row->mobile);
  111. }
  112. }*/
  113. //手机去重
  114. /*if (isset($changed['simplemobile'])) {
  115. if($changed['simplemobile']){
  116. }else {
  117. unset($row->simplemobile);
  118. }
  119. }*/
  120. });
  121. /*self::beforeInsert(function ($row){
  122. if (isset($row['password'])) {
  123. if ($row['password']) {
  124. $salt = \fast\Random::alnum();
  125. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  126. $row->salt = $salt;
  127. } else {
  128. unset($row->password);
  129. }
  130. }
  131. if($row['mobile']){
  132. $exists = db('user')->where('mobile', $row['mobile'])->find();
  133. if ($exists) {
  134. abort(500,'手机号已经被使用');
  135. }
  136. }else{
  137. abort(500,'手机号不能为空');
  138. }
  139. //判断用户名,手机号重复
  140. });
  141. self::afterInsert(function ($row){
  142. $username = 'u' . (10000 + $row['id']);
  143. db('user')->where('id',$row['id'])->update(['username'=>$username]);
  144. $data = [
  145. 'user_id' => $row['id'],
  146. ];
  147. //注册钱包
  148. db('user_wallet')->insertGetId($data);
  149. //注册用户活跃
  150. db('user_active')->insertGetId($data);
  151. //注册用户权限
  152. db('user_power')->insertGetId($data);
  153. });*/
  154. }
  155. public function getGenderList()
  156. {
  157. return ['-1' => __('Gender -1'),'1' => __('Gender 1'), '0' => __('Gender 0')];
  158. }
  159. public function getIdcardStatusList()
  160. {
  161. return ['-1' => __('Idcard_status -1'), '0' => __('Idcard_status 0'), '1' => __('Idcard_status 1'), '2' => __('Idcard_status 2')];
  162. }
  163. public function getRealStatusList()
  164. {
  165. return ['-1' => __('Real_status -1'), '0' => __('Real_status 0'), '1' => __('Real_status 1'), '2' => __('Real_status 2')];
  166. }
  167. public function getStatusList()
  168. {
  169. return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1')];
  170. }
  171. public function getHideIsFinishinfoList()
  172. {
  173. return ['1' => __('Hide_is_finishinfo 1'), '0' => __('Hide_is_finishinfo 0')];
  174. }
  175. public function getIsActiveList()
  176. {
  177. return ['1' => __('Is_active 1'), '0' => __('Is_active 0')];
  178. }
  179. public function getOpenMatchVideoList()
  180. {
  181. return ['1' => __('Open_match_video 1'), '0' => __('Open_match_video 0')];
  182. }
  183. public function getOpenMatchAudioList()
  184. {
  185. return ['1' => __('Open_match_audio 1'), '0' => __('Open_match_audio 0')];
  186. }
  187. public function getJinyantypeList()
  188. {
  189. return ['1' => __('Jinyantype 1'), '2' => __('Jinyantype 2'), '3' => __('Jinyantype 3')];
  190. }
  191. public function getIsShouchongList()
  192. {
  193. return ['0' => __('Is_shouchong 0'), '1' => __('Is_shouchong 1')];
  194. }
  195. public function getYaoqingtimeTextAttr($value, $data)
  196. {
  197. $value = $value ? $value : (isset($data['yaoqingtime']) ? $data['yaoqingtime'] : '');
  198. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  199. }
  200. public function getGenderTextAttr($value, $data)
  201. {
  202. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  203. $list = $this->getGenderList();
  204. return isset($list[$value]) ? $list[$value] : '';
  205. }
  206. public function getIdcardStatusTextAttr($value, $data)
  207. {
  208. $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
  209. $list = $this->getIdcardStatusList();
  210. return isset($list[$value]) ? $list[$value] : '';
  211. }
  212. public function getRealStatusTextAttr($value, $data)
  213. {
  214. $value = $value ? $value : (isset($data['real_status']) ? $data['real_status'] : '');
  215. $list = $this->getRealStatusList();
  216. return isset($list[$value]) ? $list[$value] : '';
  217. }
  218. public function getLogintimeTextAttr($value, $data)
  219. {
  220. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  221. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  222. }
  223. public function getJointimeTextAttr($value, $data)
  224. {
  225. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  226. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  227. }
  228. public function getStatusTextAttr($value, $data)
  229. {
  230. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  231. $list = $this->getStatusList();
  232. return isset($list[$value]) ? $list[$value] : '';
  233. }
  234. public function getHideIsFinishinfoTextAttr($value, $data)
  235. {
  236. $value = $value ? $value : (isset($data['hide_is_finishinfo']) ? $data['hide_is_finishinfo'] : '');
  237. $list = $this->getHideIsFinishinfoList();
  238. return isset($list[$value]) ? $list[$value] : '';
  239. }
  240. public function getIsActiveTextAttr($value, $data)
  241. {
  242. $value = $value ? $value : (isset($data['is_active']) ? $data['is_active'] : '');
  243. $list = $this->getIsActiveList();
  244. return isset($list[$value]) ? $list[$value] : '';
  245. }
  246. public function getOpenMatchVideoTextAttr($value, $data)
  247. {
  248. $value = $value ? $value : (isset($data['open_match_video']) ? $data['open_match_video'] : '');
  249. $list = $this->getOpenMatchVideoList();
  250. return isset($list[$value]) ? $list[$value] : '';
  251. }
  252. public function getOpenMatchAudioTextAttr($value, $data)
  253. {
  254. $value = $value ? $value : (isset($data['open_match_audio']) ? $data['open_match_audio'] : '');
  255. $list = $this->getOpenMatchAudioList();
  256. return isset($list[$value]) ? $list[$value] : '';
  257. }
  258. public function getJinyantimeTextAttr($value, $data)
  259. {
  260. $value = $value ? $value : (isset($data['jinyantime']) ? $data['jinyantime'] : '');
  261. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  262. }
  263. public function getJinyantypeTextAttr($value, $data)
  264. {
  265. $value = $value ? $value : (isset($data['jinyantype']) ? $data['jinyantype'] : '');
  266. $list = $this->getJinyantypeList();
  267. return isset($list[$value]) ? $list[$value] : '';
  268. }
  269. public function getIsShouchongTextAttr($value, $data)
  270. {
  271. $value = $value ? $value : (isset($data['is_shouchong']) ? $data['is_shouchong'] : '');
  272. $list = $this->getIsShouchongList();
  273. return isset($list[$value]) ? $list[$value] : '';
  274. }
  275. protected function setYaoqingtimeAttr($value)
  276. {
  277. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  278. }
  279. protected function setLogintimeAttr($value)
  280. {
  281. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  282. }
  283. protected function setJointimeAttr($value)
  284. {
  285. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  286. }
  287. protected function setJinyantimeAttr($value)
  288. {
  289. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  290. }
  291. public function usergroup()
  292. {
  293. return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
  294. }
  295. public function userwallet()
  296. {
  297. return $this->belongsTo('Userwallet', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  298. }
  299. }