DinnerUser.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class DinnerUser extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'phone' => input('phone','','trim'),
  12. 'name' => input('name','','trim'),
  13. 'password' => input('password','','trim'),
  14. 'enable' => input('enable/d',1),
  15. 'company_id' => input('companyId/d',0),
  16. ];
  17. $logdata = json_encode($data);
  18. if($data['id'] > 0){
  19. $result = validate('DinnerUser')->check($data,[],'edit');
  20. }else{
  21. $result = validate('DinnerUser')->check($data,[],'add');
  22. }
  23. if(true !== $result){
  24. HelpHander::error(validate('DinnerUser')->getError());
  25. }
  26. $id = $data['id'];
  27. unset($data['id']);
  28. if($id > 0){
  29. if($data['password']){
  30. $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
  31. }else{
  32. unset($data['password']);
  33. }
  34. $data['update_time'] = date('Y-m-d H:i:s');
  35. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  36. }else{
  37. $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
  38. $data['create_time'] = date('Y-m-d H:i:s');
  39. $ret = $this->allowField(true)->save($data);
  40. }
  41. if(!$ret){
  42. HelpHander::error('操作失败');
  43. }
  44. if($id > 0){
  45. $content = '修改点餐用户';
  46. }else{
  47. $content = '添加点餐用户';
  48. }
  49. model('ActionLog')->add(20,$content,0,$logdata);
  50. return true;
  51. }
  52. public function info($id){
  53. $info = $this->where('id',$id)->find();
  54. if(!$info){
  55. HelpHander::error('数据不存在');
  56. }
  57. return $info->toArray();
  58. }
  59. public function lists(){
  60. $page = input('page/d',1);
  61. $size = input('size/d',10);
  62. $title = input('title','','trim');
  63. $enable = input('enable/d',-1);
  64. $companyId = input('companyId/d',0);
  65. if($companyId > 0){
  66. $map[] = ['company_id','=',$companyId];
  67. }
  68. $map[] = ['del','=',0];
  69. if($title != ''){
  70. $map[] = ['phone|name','like','%'.$title.'%'];
  71. }
  72. if($enable >= 0){
  73. $map[] = ['enable','=',$enable];
  74. }
  75. $lists = $this
  76. ->where($map)
  77. ->page($page,$size)
  78. ->order('id desc')
  79. ->select();
  80. $lists = $lists?$lists->toArray():[];
  81. foreach ($lists as $k=>$v){
  82. $lists[$k]['company'] = '';
  83. if($v['company_id'] > 0){
  84. $lists[$k]['company'] = Db::name('dinner_company')->where('id',$v['company_id'])->value('title');
  85. }
  86. }
  87. $total = $this->where($map)->count();
  88. $data = [
  89. 'total' => $total,
  90. 'list' => $lists
  91. ];
  92. return $data;
  93. }
  94. public function del($id){
  95. $ret = $this->where('id',$id)->setField('del',1);
  96. if(!$ret){
  97. HelpHander::error('删除失败');
  98. }
  99. $logdata = json_encode(['id' => $id]);
  100. model('ActionLog')->add(20,'删除点餐用户',0,$logdata);
  101. return true;
  102. }
  103. public function login($phone,$password){
  104. $user = $this->where('phone',$phone)->where('del',0)->find();
  105. if(empty($user)){
  106. HelpHander::error('账号或密码错误');
  107. }
  108. if($user['enable'] != 1){
  109. HelpHander::error('账号被禁用');
  110. }
  111. if(!password_verify($password, $user['password'])){
  112. HelpHander::error('账号或密码错误');
  113. }
  114. $token = md5($user['id'].'-'.time().rand(1000,9999));
  115. $ret = Db::name('dinner_user')->where('id',$user['id'])->update([
  116. 'login_time' => date('Y-m-d H:i:s'),
  117. 'token' => $token
  118. ]);
  119. if(!$ret){
  120. HelpHander::error('登录失败');
  121. }
  122. return ['user_id' => $user['id'], 'token' => $token];
  123. }
  124. public function modifyPass($data){
  125. $result = validate('DinnerUser')->check($data,[],'modifyPass');
  126. if(true !== $result){
  127. HelpHander::error(validate('DinnerUser')->getError());
  128. }
  129. $user = Db::name('dinner_user')->where('id',$data['userId'])->where('del',0)->find();
  130. if(!$user){
  131. HelpHander::error('用户不存在');
  132. }
  133. if(!password_verify($data['oldPass'], $user['password'])){
  134. HelpHander::error('旧密码错误');
  135. }
  136. $ret = Db::name('dinner_user')->where('id',$user['id'])->update([
  137. 'update_time' => date('Y-m-d H:i:s'),
  138. 'password' => password_hash($data['pass'],PASSWORD_DEFAULT)
  139. ]);
  140. if(!$ret){
  141. HelpHander::error('操作失败');
  142. }
  143. return true;
  144. }
  145. }