| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace app\api\model\user;
- use app\common\library\helper;
- use app\common\library\wechat\WxBizDataCrypt;
- use think\facade\Cache;
- use app\common\exception\BaseException;
- use app\common\model\user\User as UserModel;
- use app\api\model\plus\agent\Referee as RefereeModel;
- use app\common\library\easywechat\AppWx;
- use app\common\model\user\Grade as GradeModel;
- use app\api\model\plus\invitationgift\Partake;
- use app\common\model\page\CenterMenu as CenterMenuModel;
- /**
- * 用户模型类
- */
- class User extends UserModel
- {
- private $token;
- /**
- * 隐藏字段
- */
- protected $hidden = [
- 'open_id',
- 'is_delete',
- 'app_id',
- 'create_time',
- 'update_time'
- ];
- /**
- * 获取用户信息
- */
- public static function getUser($token)
- {
- $userId = Cache::get($token);
- return (new static())->where(['user_id' => $userId])->with(['address', 'addressDefault', 'grade'])->order('user_id desc')->find();
- }
- /**
- * 获取openID
- */
- public function login($post)
- {
- // 微信登录 获取session_key
- $app = AppWx::getApp();
- $session = $app->auth->session($post['code']);
- if (empty($session['openid'])) {
- throw new BaseException(['msg' => 'code码失效']);
- }
- return $session;
- // // 自动注册用户
- // $refereeId = isset($post['referee_id']) ? $post['referee_id'] : null;
- // $userInfo = json_decode(htmlspecialchars_decode($post['user_info']), true);
- // //邀请好友
- // $invitation_id = isset($post['invitation_id']) ? $post['invitation_id'] : 0;
- // $user_id = $this->register($session['openid'], $userInfo, $refereeId, $session, $invitation_id);
- // // 生成token (session3rd)
- // $this->token = $this->token($session['openid']);
- // // 记录缓存, 7天
- // Cache::tag('cache')->set($this->token, $user_id, 86400 * 7);
- // return $user_id;
- }
- /**
- * 账号密码登陆
- */
- public function accountlogin($post)
- {
- if (empty($post['openid'])) {
- $this->error = '微信openid为空';
- return false;
- }
- if (empty($post['user_no']) || empty($post['password'])) {
- $this->error = '登陆账号密码为空';
- return false;
- }
- $where = [
- ['user_no','=',$post['user_no']],
- ['is_delete','=',0],
- ];
- $user = $this->where($where)->order('user_id desc')->find();
- if (empty($user)) {
- $this->error = '登录账号错误';
- return false;
- }
- if ($user['password'] != md5($post['password'])) {
- $this->error = '登陆密码错误';
- return false;
- }
- $info = [
- 'open_id' => $post['openid'],
- 'reg_source' => 'wx',
- ];
- $this->where('user_id',$user['user_id'])->update($info);
- $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 bindMobile($post)
- {
- try {
- // 微信登录 获取session_key
- $app = AppWx::getApp();
- $iv = $post['iv'];
- $encrypted_data = $post['encrypted_data'];
- $pc = new WxBizDataCrypt($app['config']['app_id'], $post['session_key']);
- $errCode = $pc->decryptData($encrypted_data, $iv, $data);
- if ($errCode == 0) {
- $data = json_decode($data, true);
- $this->save([
- 'mobile' => $data['phoneNumber']
- ]);
- return $data['phoneNumber'];
- } else {
- log_write('$errCode' . $errCode);
- return false;
- }
- } catch (\Exception $e) {
- $this->error = '获取手机号失败,请重试';
- return false;
- }
- }
- /**
- * 获取token
- */
- public function getToken()
- {
- return $this->token;
- }
- /**
- * 生成用户认证的token
- */
- private function token($openid)
- {
- $app_id = self::$app_id;
- // 生成一个不会重复的随机字符串
- $guid = \getGuidV4();
- // 当前时间戳 (精确到毫秒)
- $timeStamp = microtime(true);
- // 自定义一个盐
- $salt = 'token_salt';
- return md5("{$app_id}_{$timeStamp}_{$openid}_{$guid}_{$salt}");
- }
- /**
- * 自动注册用户
- */
- private function register($open_id, $data, $refereeId, $decryptedData, $invitation_id)
- {
- //通过unionid查询用户是否存在
- $user = null;
- if (isset($decryptedData['unionid']) && !empty($decryptedData['unionid'])) {
- $data['union_id'] = $decryptedData['unionid'];
- $user = self::detailByUnionid($decryptedData['unionid']);
- }
- if (!$user) {
- // 通过open_id查询用户是否已存在
- $user = self::detail(['open_id' => $open_id]);
- }
- 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'] = $refereeId;
- $data['reg_source'] = 'wx';
- //默认等级
- $data['grade_id'] = GradeModel::getDefaultGradeId();
- }
- $this->startTrans();
- try {
- // 保存/更新用户记录
- if (!$model->save(array_merge($data, [
- 'open_id' => $open_id,
- 'app_id' => self::$app_id
- ]))
- ) {
- throw new BaseException(['msg' => '用户注册失败']);
- }
- if (!$user && $refereeId > 0) {
- // 记录推荐人关系
- RefereeModel::createRelation($model['user_id'], $refereeId);
- //更新用户邀请数量
- (new UserModel())->setIncInvite($refereeId);
- //邀请好友送好礼
- $invitation_id > 0 && (new Partake())->addPartake($invitation_id, $refereeId, $model['user_id']);
- }
- $this->commit();
- } catch (\Exception $e) {
- $this->rollback();
- throw new BaseException(['msg' => $e->getMessage()]);
- }
- return $model['user_id'];
- }
- /**
- *统计被邀请人数
- */
- public function getCountInv($user_id)
- {
- return $this->where('referee_id', '=', $user_id)->count('user_id');
- }
- /**
- * 签到更新用户积分
- */
- public function setPoints($user_id, $days, $sign_conf, $sign_date)
- {
- $rank = $sign_conf['ever_sign'];
- if ($sign_conf['is_increase'] == 'true') {
- if ($days >= $sign_conf['no_increase']) {
- $days = $sign_conf['no_increase'] - 1;
- }
- $rank = ($days - 1) * $sign_conf['increase_reward'] + $rank;
- }
- //是否奖励
- if (isset($sign_conf['reward_data'])) {
- $arr = array_column($sign_conf['reward_data'], 'day');
- if (in_array($days, $arr)) {
- $key = array_search($days, $arr);
- if ($sign_conf['reward_data'][$key]['is_point'] == 'true') {
- $rank = $sign_conf['reward_data'][$key]['point'] + $rank;
- }
- }
- }
- // 新增积分变动明细
- $this->setIncPoints($rank, '用户签到:签到日期' . $sign_date);
- return $rank;
- }
- /**
- * 个人中心菜单列表
- */
- public static function getMenus($source, $user_id)
- {
- // 系统菜单
- $sys_menus = CenterMenuModel::getSysMenu();
- // 查询用户菜单
- $model = new CenterMenuModel();
- $user_menus = $model->getAll();
- $user_menu_tags = [];
- foreach ($user_menus as $menu) {
- $menu['sys_tag'] != '' && array_push($user_menu_tags, $menu['sys_tag']);
- }
- $save_data = [];
- foreach ($sys_menus as $menu) {
- if ($menu['sys_tag'] != '' && !in_array($menu['sys_tag'], $user_menu_tags)) {
- $save_data[] = array_merge($sys_menus[$menu['sys_tag']], [
- 'sort' => 100,
- 'app_id' => self::$app_id
- ]);
- }
- }
- if (count($save_data) > 0) {
- $model->saveAll($save_data);
- Cache::delete('center_menu_' . self::$app_id);
- $user_menus = $model->getAll();
- }
- $show_menus = [];
- foreach ($user_menus as $menus) {
- if (strpos($menus['icon'], 'http') !== 0) {
- $menus['icon'] = self::$base_url . $menus['icon'];
- }
- if ($menus['is_show'] == 1) {
- array_push($show_menus, $menus);
- }
- }
- return $show_menus;
- }
- /**
- * 修改会员信息
- */
- public function edit($data)
- {
- return $this->save($data);
- }
- }
|