User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class User extends Model
  5. {
  6. // 表名
  7. protected $name = 'user';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'real_status_text',
  17. 'gender_text',
  18. 'idcard_status_text',
  19. 'prevtime_text',
  20. 'logintime_text',
  21. 'jointime_text',
  22. 'status_text',
  23. 'open_match_video_text',
  24. 'open_match_audio_text',
  25. 'open_match_typing_text',
  26. 'free_video_text',
  27. 'free_audio_text',
  28. 'free_typing_text',
  29. 'is_kefu_text',
  30. 'frozentime_text',
  31. 'is_recommend_text',
  32. 'is_agent_text',
  33. ];
  34. protected static function init()
  35. {
  36. self::beforeUpdate(function ($row) {
  37. $changed = $row->getChangedData();
  38. //如果有修改密码
  39. if (isset($changed['password'])) {
  40. if ($changed['password']) {
  41. $salt = \fast\Random::alnum();
  42. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  43. $row->salt = $salt;
  44. } else {
  45. unset($row->password);
  46. }
  47. }
  48. //手机去重
  49. if (isset($changed['mobile'])) {
  50. if($changed['mobile']){
  51. $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  52. if ($exists) {
  53. abort(500,'手机号已经被使用');
  54. }
  55. }
  56. }
  57. });
  58. self::beforeInsert(function ($row){
  59. if (isset($row['password'])) {
  60. if ($row['password']) {
  61. $salt = \fast\Random::alnum();
  62. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  63. $row->salt = $salt;
  64. } else {
  65. unset($row->password);
  66. }
  67. }
  68. //判断用户名,手机号重复
  69. /*if($row['username']){
  70. $exists = db('user')->where('username', $row['username'])->find();
  71. if ($exists) {
  72. abort(500,'用户名已经被使用');
  73. }
  74. }else{
  75. abort(500,'用户名不能为空');
  76. }*/
  77. if($row['mobile']){
  78. $exists = db('user')->where('mobile', $row['mobile'])->find();
  79. if ($exists) {
  80. abort(500,'手机号已经被使用');
  81. }
  82. }else{
  83. abort(500,'手机号不能为空');
  84. }
  85. //判断用户名,手机号重复
  86. });
  87. self::afterInsert(function ($row){
  88. $data = [
  89. 'user_id' => $row['id'],
  90. ];
  91. db('user_wallet')->insertGetId($data);
  92. $username = 'u' . (10000 + $row['id']);
  93. db('user')->where('id',$row['id'])->update(['username'=>$username]);
  94. });
  95. }
  96. public function getRealStatusList()
  97. {
  98. return ['-1' => __('Real_status -1'), '0' => __('Real_status 0'), '1' => __('Real_status 1'), '2' => __('Real_status 2')];
  99. }
  100. public function getGenderList()
  101. {
  102. return ['1' => __('Gender 1'), '0' => __('Gender 0'), '-1' => __('Gender -1')];
  103. }
  104. public function getIdcardStatusList()
  105. {
  106. return ['-1' => __('Idcard_status -1'), '0' => __('Idcard_status 0'), '1' => __('Idcard_status 1'), '2' => __('Idcard_status 2')];
  107. }
  108. public function getStatusList()
  109. {
  110. return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1'), '2' => __('Status 2')];
  111. }
  112. public function getIsKefuList()
  113. {
  114. return ['1' => __('是'), '0' => __('否')];
  115. }
  116. public function getIsRecommendList()
  117. {
  118. return ['1' => __('是'), '0' => __('否')];
  119. }
  120. public function getIsAgentList()
  121. {
  122. return ['1' => __('是'), '0' => __('否')];
  123. }
  124. public function getOpenMatchVideoList()
  125. {
  126. return ['1' => __('Open_match_video 1'), '0' => __('Open_match_video 0')];
  127. }
  128. public function getOpenMatchAudioList()
  129. {
  130. return ['1' => __('Open_match_audio 1'), '0' => __('Open_match_audio 0')];
  131. }
  132. public function getOpenMatchTypingList()
  133. {
  134. return ['1' => __('Open_match_typing 1'), '0' => __('Open_match_typing 0')];
  135. }
  136. public function getFreeVideoList()
  137. {
  138. return ['1' => __('Free_video 1'), '0' => __('Free_video 0')];
  139. }
  140. public function getFreeAudioList()
  141. {
  142. return ['1' => __('Free_audio 1'), '0' => __('Free_audio 0')];
  143. }
  144. public function getFreeTypingList()
  145. {
  146. return ['1' => __('Free_typing 1'), '0' => __('Free_typing 0')];
  147. }
  148. public function getRealStatusTextAttr($value, $data)
  149. {
  150. $value = $value ? $value : (isset($data['real_status']) ? $data['real_status'] : '');
  151. $list = $this->getRealStatusList();
  152. return isset($list[$value]) ? $list[$value] : '';
  153. }
  154. public function getGenderTextAttr($value, $data)
  155. {
  156. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  157. $list = $this->getGenderList();
  158. return isset($list[$value]) ? $list[$value] : '';
  159. }
  160. public function getIdcardStatusTextAttr($value, $data)
  161. {
  162. $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
  163. $list = $this->getIdcardStatusList();
  164. return isset($list[$value]) ? $list[$value] : '';
  165. }
  166. public function getPrevtimeTextAttr($value, $data)
  167. {
  168. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  169. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  170. }
  171. public function getLogintimeTextAttr($value, $data)
  172. {
  173. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  174. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  175. }
  176. public function getJointimeTextAttr($value, $data)
  177. {
  178. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  179. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  180. }
  181. public function getStatusTextAttr($value, $data)
  182. {
  183. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  184. $list = $this->getStatusList();
  185. return isset($list[$value]) ? $list[$value] : '';
  186. }
  187. public function getIsKefuTextAttr($value, $data)
  188. {
  189. $value = $value ? $value : (isset($data['is_kefu']) ? $data['is_kefu'] : '');
  190. $list = $this->getStatusList();
  191. return isset($list[$value]) ? $list[$value] : '';
  192. }
  193. public function getIsRecommendTextAttr($value, $data)
  194. {
  195. $value = $value ? $value : (isset($data['is_recommend']) ? $data['is_recommend'] : '');
  196. $list = $this->getStatusList();
  197. return isset($list[$value]) ? $list[$value] : '';
  198. }
  199. public function getIsAgentTextAttr($value, $data)
  200. {
  201. $value = $value ? $value : (isset($data['is_agent']) ? $data['is_agent'] : '');
  202. $list = $this->getStatusList();
  203. return isset($list[$value]) ? $list[$value] : '';
  204. }
  205. public function getOpenMatchVideoTextAttr($value, $data)
  206. {
  207. $value = $value ? $value : (isset($data['open_match_video']) ? $data['open_match_video'] : '');
  208. $list = $this->getOpenMatchVideoList();
  209. return isset($list[$value]) ? $list[$value] : '';
  210. }
  211. public function getOpenMatchAudioTextAttr($value, $data)
  212. {
  213. $value = $value ? $value : (isset($data['open_match_audio']) ? $data['open_match_audio'] : '');
  214. $list = $this->getOpenMatchAudioList();
  215. return isset($list[$value]) ? $list[$value] : '';
  216. }
  217. public function getOpenMatchTypingTextAttr($value, $data)
  218. {
  219. $value = $value ? $value : (isset($data['open_match_typing']) ? $data['open_match_typing'] : '');
  220. $list = $this->getOpenMatchTypingList();
  221. return isset($list[$value]) ? $list[$value] : '';
  222. }
  223. public function getFreeVideoTextAttr($value, $data)
  224. {
  225. $value = $value ? $value : (isset($data['free_video']) ? $data['free_video'] : '');
  226. $list = $this->getFreeVideoList();
  227. return isset($list[$value]) ? $list[$value] : '';
  228. }
  229. public function getFreeAudioTextAttr($value, $data)
  230. {
  231. $value = $value ? $value : (isset($data['free_audio']) ? $data['free_audio'] : '');
  232. $list = $this->getFreeAudioList();
  233. return isset($list[$value]) ? $list[$value] : '';
  234. }
  235. public function getFreeTypingTextAttr($value, $data)
  236. {
  237. $value = $value ? $value : (isset($data['free_typing']) ? $data['free_typing'] : '');
  238. $list = $this->getFreeTypingList();
  239. return isset($list[$value]) ? $list[$value] : '';
  240. }
  241. public function getFrozentimeTextAttr($value, $data)
  242. {
  243. $value = $value ? $value : (isset($data['frozentime']) ? $data['frozentime'] : '');
  244. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  245. }
  246. protected function setPrevtimeAttr($value)
  247. {
  248. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  249. }
  250. protected function setLogintimeAttr($value)
  251. {
  252. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  253. }
  254. protected function setJointimeAttr($value)
  255. {
  256. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  257. }
  258. protected function setFrozentimeAttr($value)
  259. {
  260. return $value === '' ? 0 : ($value && !is_numeric($value) ? strtotime($value) : $value);
  261. }
  262. public function gonghui()
  263. {
  264. return $this->belongsTo('Gonghui', 'gh_id', 'id', [], 'LEFT')->setEagerlyType(0);
  265. }
  266. public function userwallet()
  267. {
  268. return $this->hasOne('Userwallet', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  269. }
  270. public function wallet()
  271. {
  272. return $this->belongsTo('Userwallet', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  273. }
  274. }