| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?php
- namespace app\api\model\user;
- use app\api\model\plus\agent\Referee as RefereeModel;
- use app\api\model\plus\invitationgift\Partake;
- use app\common\model\user\User;
- use think\facade\Cache;
- use app\common\exception\BaseException;
- use app\common\model\user\User as UserModel;
- use app\common\model\user\Sms as SmsModel;
- use app\common\model\user\Grade as GradeModel;
- use think\facade\Db;
- /**
- * 公众号用户模型类
- */
- class UserOpen extends UserModel
- {
- private $token;
- /**
- * 隐藏字段
- */
- protected $hidden = [
- 'open_id',
- 'is_delete',
- 'app_id',
- 'create_time',
- 'update_time'
- ];
- /**
- * 用户登录
- */
- public function login($userInfo, $referee_id)
- {
- // 自动注册用户
- $user_id = $this->register($userInfo, $referee_id);
- // 生成token (session3rd)
- $this->token = $this->token($userInfo['openid']);
- // 记录缓存, 7天
- Cache::set($this->token, $user_id, 86400 * 7);
- return $user_id;
- }
- /**
- * 获取token
- */
- public function getToken()
- {
- return $this->token;
- }
- /**
- * 生成用户认证的token
- */
- private function token($openid)
- {
- return md5($openid . 'token_salt');
- }
- /**
- * 自动注册用户
- */
- private function register($userInfo, $referee_id = 0)
- {
- $data = [];
- //通过unionid查询用户是否存在
- $user = null;
- if (isset($userInfo['unionid']) && !empty($userInfo['unionid'])) {
- $data['union_id'] = $userInfo['unionid'];
- $user = self::detailByUnionid($userInfo['unionid']);
- }
- // 查询用户是否已存在
- if (!$user) {
- $user = self::detail(['appopen_id' => $userInfo['openid']]);
- }
- if ($user) {
- $model = $user;
- // 只修改union_id
- if (isset($data['union_id'])) {
- $data = [
- 'union_id' => $data['union_id'],
- ];
- }else{
- return $user['user_id'];
- }
- } else {
- $model = $this;
- $data['referee_id'] = $referee_id;
- $data['reg_source'] = 'app';
- //默认等级
- $data['grade_id'] = GradeModel::getDefaultGradeId();
- }
- $data['appopen_id'] = $userInfo['openid'];
- // 用户昵称
- if (!$user) {
- $data['nickName'] = preg_replace('/[\xf0-\xf7].{3}/', '', $userInfo['nickname']);
- }
- $data['avatarUrl'] = $userInfo['headimgurl'];
- $data['gender'] = $userInfo['sex'];
- $data['province'] = $userInfo['province'];
- $data['country'] = $userInfo['country'];
- $data['city'] = $userInfo['city'];
- $data['reg_source'] = 'app';
- try {
- $this->startTrans();
- // 保存/更新用户记录
- if (!$model->save(array_merge($data, [
- 'app_id' => self::$app_id
- ]))
- ) {
- throw new BaseException(['msg' => '用户注册失败']);
- }
- if (!$user && $referee_id > 0) {
- // 记录推荐人关系,
- RefereeModel::createRelation($model['user_id'], $referee_id);
- //更新用户邀请数量
- (new UserModel())->where('user_id', '=', $referee_id)->inc('total_invite')->update();
- }
- $this->commit();
- } catch (\Exception $e) {
- $this->rollback();
- throw new BaseException(['msg' => $e->getMessage()]);
- }
- return $model['user_id'];
- }
- /**
- * 手机号密码用户登录
- */
- public function phoneLogin($data)
- {
- $where = [
- ['user_no','=',$data['mobile']],
- ['is_delete','=',0],
- ];
- $user = $this->where($where)->order('user_id desc')->find();
- if (empty($user)) {
- $this->error = '登录账号错误';
- return false;
- }
- if ($user['password'] != md5($data['password'])) {
- $this->error = '登陆密码错误';
- return false;
- }
- if (!empty($data['openid'])) {
- $weixin = [
- // 'nickName' => authcode($data['nickName'], 'DECODE'),
- // 'avatarUrl' => authcode($data['avatarUrl'], 'DECODE'),
- 'mpopen_id' => authcode($data['openid'], 'DECODE'),
- ];
- // $mpopen_id = authcode($data['openid'], 'DECODE'),
- if ($weixin['mpopen_id'] != $user['mpopen_id']) {
-
- if ($this->where('user_id',$user['user_id'])->update(['mpopen_id'=>$weixin['mpopen_id']])) {
- $ceshi = [
- 'test' => json_encode($weixin,JSON_UNESCAPED_UNICODE),
- 'name' => '正确',
- 'time' => time(),
- ];
- Db::name('user_ceshi')->insertGetId($ceshi);
- // 生成token (session3rd)
- $this->token = $this->token($user['user_no']);
- // 记录缓存, 30天
- Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30);
- return $user['user_id'];
- }else{
- $ceshi = [
- 'test' => json_encode($weixin,JSON_UNESCAPED_UNICODE),
- 'name' => '微信错误日志',
- 'time' => time(),
- ];
- Db::name('user_ceshi')->insertGetId($ceshi);
- $this->error = '会员绑定微信信息失败';
- return false;
- }
- }else{
- // 生成token (session3rd)
- $this->token = $this->token($user['user_no']);
- // 记录缓存, 30天
- Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30);
- return $user['user_id'];
- }
-
- }else{
- // 生成token (session3rd)
- $this->token = $this->token($user['user_no']);
- // 记录缓存, 30天
- Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30);
- return $user['user_id'];
- }
-
- }
- /**
- * 手机号验证码登录
- */
- public function smslogin($data)
- {
- $where = [
- [ 'user_no','=',$data['mobile']],
- ['is_delete','=',0],
- ];
- $user = $this->where($where)->order('user_id desc')->find();
- if (empty($user)) {
- $this->error = '登录账号错误';
- return false;
- }
- if (empty($user['mobile'])) {
- $this->error = '您的账号未绑定手机号';
- return false;
- }
- $vall = [
- 'mobile' => $user['mobile'],
- 'code' => $data['code'],
- ];
- if (!$this->check($vall)) {
- return false;
- }
- if (!empty($data['openid'])) {
- $weixin = [
- // 'nickName' => authcode($data['nickName'], 'DECODE'),
- // 'avatarUrl' => authcode($data['avatarUrl'], 'DECODE'),
- 'mpopen_id' => authcode($data['openid'], 'DECODE'),
- ];
- if ($weixin['mpopen_id'] != $user['mpopen_id']) {
- if ($this->where('user_id',$user['user_id'])->update(['mpopen_id'=> $weixin['mpopen_id']])) {
- $ceshi = [
- 'test' => json_encode($weixin,JSON_UNESCAPED_UNICODE),
- 'name' => '正确',
- 'time' => time(),
- ];
- Db::name('user_ceshi')->insertGetId($ceshi);
- // 生成token (session3rd)
- $this->token = $this->token($user['user_no']);
- // 记录缓存, 30天
- Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30);
- return $user['user_id'];
- }else{
- $ceshi = [
- 'test' => json_encode($weixin,JSON_UNESCAPED_UNICODE),
- 'name' => '微信错误日志',
- 'time' => time(),
- ];
- Db::name('user_ceshi')->insertGetId($ceshi);
- $this->error = '会员绑定微信信息失败';
- return false;
- }
- }else{
- // 生成token (session3rd)
- $this->token = $this->token($user['user_no']);
- // 记录缓存, 30天
- Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30);
- return $user['user_id'];
- }
- }else{
- // 生成token (session3rd)
- $this->token = $this->token($user['user_no']);
- // 记录缓存, 30天
- Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30);
- return $user['user_id'];
- }
-
- }
- /*
- *重置密码
- */
- public function resetpassword($data)
- {
- $where = [[ 'user_no','=',$data['mobile']]];
- $user = $this->where($where)->order('user_id desc')->find();
- if (empty($user)) {
- $this->error = '登录账号错误';
- return false;
- }
- if ($user['is_delete'] == 1) {
- $this->error = '手机号被禁止或删除,请联系客服';
- return false;
- }
- if (empty($user['mobile'])) {
- $this->error = '您的账号未绑定手机号';
- return false;
- }
- $vall = [
- 'mobile' => $user['mobile'],
- 'code' => $data['code'],
- ];
- if (!$this->check($vall)) {
- return false;
- }
- if (md5($data['password']) == $user['password']) {
- $this->error = '登陆密码与原密码相同';
- return false;
- }
- return $this->where('user_id', $user['user_id'])->update([
- 'password' => md5($data['password'])
- ]);
- }
- /*
- *手机号注册
- */
- public function phoneRegister($data)
- {
- if (!$this->check($data)) {
- return false;
- }
- $user = $this->where('user_no',$data['user_no'])->find();
- if (empty($user)) {
- $vall = [
- 'mobile' => $data['mobile'],
- 'user_no' => $data['user_no']??'',
- 'reg_source' => isset($data['reg_source'])?$data['reg_source']:'app',
- 'grade_id' => GradeModel::getDefaultGradeId(),
- 'app_id' => self::$app_id,
- 'password' => md5($data['password'])
- ];
- if (empty($data['nickName'])) {
- $vall['nickName'] = $data['user_no']??'';
- }
- if (!empty($data['openid'])) {
- // $vall['avatarUrl'] = authcode($data['avatarUrl'], 'DECODE');
- // $vall['nickName'] = authcode($data['nickName'], 'DECODE');
- $vall['mpopen_id'] = authcode($data['openid'], 'DECODE');
- }
- $this->save($vall);
- if ($data['referee_id'] > 0) {
- // 记录推荐人关系
- RefereeModel::createRelation($this['user_id'], $data['referee_id']);
- //更新用户邀请数量
- (new UserModel())->setIncInvite($data['referee_id']);
- //邀请好友送好礼
- $data['invitation_id'] > 0 && (new Partake())->addPartake($data['invitation_id'], $data['referee_id'], $this['user_id']);
- }
- return true;
- } else {
- $this->error = '会员号已存在';
- return false;
- }
- }
- /**
- * 验证
- */
- private function check($data)
- {
- //判断验证码是否过期、是否正确
- $sms_model = new SmsModel();
- $sms_record_list = $sms_model
- ->where('mobile', '=', $data['mobile'])
- ->order(['create_time' => 'desc'])
- ->limit(1)->select();
- if (count($sms_record_list) == 0) {
- $this->error = '未查到短信发送记录';
- return false;
- }
- $sms_model = $sms_record_list[0];
- if ((time() - strtotime($sms_model['create_time'])) / 60 > 30) {
- $this->error = '短信验证码超时';
- return false;
- }
- if ($sms_model['code'] != $data['code']) {
- $this->error = '验证码不正确';
- return false;
- }
- return true;
- }
- /**
- * 手机号验证码登录
- */
- public function smsloginWx($data)
- {
- $user = $this->where('user_no', $data['mobile'])->find();
- if (empty($user)) {
- $vall = [
- 'mobile' => $data['mobile'],
- 'user_no' => $data['mobile'],
- 'reg_source' => 'wx',
- 'grade_id' => GradeModel::getDefaultGradeId(),
- 'app_id' => self::$app_id,
- 'password' => md5('123456'),
- 'nickName' => $data['mobile'],
- 'openid' => $data['openid'], // authcode($data['openid'], 'DECODE'),
- ];
- if (!$this->save($vall)) {
- $this->error = '登录失败';
- return false;
- }
- $user = $this->where('user_no', $data['mobile'])->find();
- }
- // 判断会员号如果是手机号,则查询是否有有此会员号
- $pattern = '/^1\d{10}$/';
- if (!preg_match($pattern, $data['mobile'])) {
- $data['mobile'] = $user['mobile'];
- }
- if (!$this->check($data)) {
- return false;
- }
- $info = [
- 'open_id' => $data['openid'],
- 'reg_source' => 'wx',
- ];
- $this->where('user_id', $user['user_id'])->update($info);
- // 生成token (session3rd)
- $this->token = $this->token($user['user_no']);
- // 记录缓存, 30天
- Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30);
- return $user['user_id'];
- }
- }
|