Users.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * 短信服务
  4. * @author wesmiler
  5. */
  6. namespace app\index\service;
  7. use app\index\model\UserModel;
  8. class Users
  9. {
  10. /**
  11. * 更新数据
  12. * @param $params
  13. * @return int|string
  14. * @throws \think\Exception
  15. * @throws \think\exception\PDOException
  16. */
  17. public static function saveData($params){
  18. $id = isset($params['id'])? intval($params['id']) : 0;
  19. $data = [
  20. 'mobile'=> isset($params['mobile'])? trim($params['mobile']) : '',
  21. 'user_login'=> isset($params['mobile'])? trim($params['mobile']) : '',
  22. 'user_nickname'=> isset($params['nickname'])? trim($params['nickname']) : '',
  23. 'user_type'=> isset($params['user_type'])? intval($params['user_type']) : 2,
  24. 'user_status'=> isset($params['user_status'])? intval($params['user_status']) : 1,
  25. 'avatar'=> isset($params['avatar'])? trim($params['avatar']) : '',
  26. ];
  27. $userPass = isset($params['password'])? trim($params['password']) : '';
  28. if($userPass){
  29. $data['user_pass'] = cmf_password($userPass);
  30. }
  31. if($id){
  32. return UserModel::where(['id'=> $id])->update($data);
  33. }else{
  34. return UserModel::insertGetId($data);
  35. }
  36. }
  37. /**
  38. * 获取会员信息
  39. * @param $where 条件
  40. * @param string $field 字段
  41. * @return array|false|\PDOStatement|string|Model
  42. */
  43. public static function getInfo($where, $field = "")
  44. {
  45. $field = $field ? $field : 'id,openid,user_nickname,user_type,avatar,user_login,user_status,mobile,balance,user_status';
  46. $info = UserModel::where($where)->field($field)->find();
  47. return $info ? $info->toArray() : [];
  48. }
  49. /**
  50. * 用户登录
  51. * @param $params 参数,mobile-必填,password-必填
  52. * @return array|int
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. * @throws \think\exception\DbException
  56. */
  57. public static function login($params){
  58. $mobile = isset($params['mobile'])? trim($params['mobile']) : '';
  59. $password = isset($params['password'])? trim($params['password']) : '';
  60. $userInfo = UserModel::where(['mobile|user_login'=> $mobile])
  61. ->field('id,user_login,mobile,user_pass,user_nickname,user_status,avatar')
  62. ->find();
  63. if(is_null($userInfo) || empty($userInfo)){
  64. return 1015;
  65. }
  66. $userPassword = isset($userInfo['user_pass'])? $userInfo['user_pass'] : '';
  67. if(!cmf_compare_password($password, $userPassword)){
  68. return 1016;
  69. }
  70. $userStatus = isset($userInfo['user_status'])? intval($userInfo['user_status']) : 0;
  71. if($userStatus != 1){
  72. return 1017;
  73. }
  74. $userInfo['mobile'] = formatName($userInfo['mobile']);
  75. $userInfo['user_login'] = formatName($userInfo['user_login']);
  76. unset($userInfo['user_pass']);
  77. session('UID', $userInfo['id']);
  78. $token = md5($userInfo['id'].'qxq');
  79. PRedis::set('tokens:'.$token, $userInfo, 24*3600);
  80. return ['token'=> $token, 'userInfo'=> $userInfo];
  81. }
  82. }