User.php 11 KB

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