123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2019/4/15
- * Time: 14:37
- */
- namespace app\common\service;
- use app\admin\controller\auth\User;
- use app\main\constants\PayConstants;
- use app\main\service\FinancialService;
- use app\main\service\UserService;
- use think\Config;
- use think\Log;
- class ClientuserService
- {
- protected static $self = null;
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- public function getUserPhoneModel()
- {
- return model('UserPhone');
- }
- public function getUserModel()
- {
- return model('User');
- }
- /**
- * 给用户绑定手机号
- * @param $uid
- * @param $phone
- * @return bool|false|int
- */
- public function bindUserByPhone($uid, $phone)
- {
- try {
- $data = $this->getUserPhoneModel()->setConnect($phone)->getInfoByPhone($phone);
- if (!$data) {
- // 维护关系表
- $this->getUserPhoneModel()->setConnect($phone)->save(['uid' => $uid, 'phone' => $phone]);
- //给USER表维护冗余字段mobile
- $userInfo = $this->getUserModel()->getUserInfo($uid);
- if (empty($userInfo['mobile'])) {
- UserService::instance()->update(['mobile' => $phone, 'bindtime' => time()], ['id' => $uid]);
- }
- //手机号绑定成功,赠送书币
- $kandian = empty(Config::get('site.phone_bind_credit')) ? 0 : intval(Config::get('site.phone_bind_credit'));
- UserService::instance()->addFreeKanDian($uid, $kandian, time(), 'APP用户绑定手机号赠送书币', PayConstants::BUSINESS_APP);
- return $userInfo;
- } else {
- return false;
- }
- } catch (\Exception $exception) {
- Log::error($exception->getMessage());
- return false;
- }
- }
- /**
- * @param $uid
- * @param $oldphone
- * @param $newphone
- * @return bool
- */
- public function modifyPhone($uid, $oldphone, $newphone)
- {
- try {
- // 修改User 表中的冗余字段mobile
- UserService::instance()->update(['mobile' => $newphone], ['id' => $uid]);
- // 修改关联表user_phone 中的phone{1.删除记录;2.重新插入一条记录[原因:以手机号作为分库标准]}
- $result = $this->getUserPhoneModel()->modifyPhone($uid, $oldphone, $newphone);
- return $result;
- } catch (\Exception $e) {
- Log::error('[ ClientUserService ]' . $e->getMessage());
- }
- }
- /**
- *
- * @param $uid
- * @param $gender
- */
- public function modifyGender($uid, $gender)
- {
- try {
- // 修改User 表中的sex 字段
- UserService::instance()->update(['sex' => $gender], ['id' => $uid]);
- return true;
- } catch (\Exception $e) {
- Log::error('[ ClientUserService ]' . $e->getMessage());
- return false;
- }
- }
- /**
- * 通过手机号注册用户
- * @param $phone
- * @return bool|false|int
- */
- public function createUserByPhone($phone)
- {
- $channel_id = Config::get('site.fake_channel_id') ?? 0;
- // 伪造OpenID
- $open_id = self::getRandOpenid();
- Log::info('[ USERPHONE ] Open_id:' . $open_id);
- try {
- $user_id = UserService::instance()->fakeUserInfo($channel_id, $open_id, $phone, time(), PayConstants::BUSINESS_APP);
- Log::info('[ USERPHONE ] $uid:' . $user_id);
- Log::info('[ USERPHONE ] getUserDBIndex:' . UserService::instance()->getUserDBIndex($user_id));
- $this->getUserPhoneModel()->setConnect($phone)->save(['uid' => $user_id, 'phone' => $phone]);
- return $user_id;
- } catch (\Exception $exception) {
- Log::error($exception->getMessage());
- return false;
- }
- }
- /**
- * 获取用户信息 通过手UID
- * @param $uid
- * @return mixed
- */
- public function getUserInfoByUid($uid)
- {
- $userInfo = $this->getUserModel()->getUserInfo($uid);
- return $userInfo;
- }
- /**
- * 获取用户信息 通过手机号
- * @param $phone
- * @return array
- * @throws \think\Exception
- */
- public function getUserByPhone($phone)
- {
- $json_str = $this->getUserPhoneModel()->setConnect($phone)->getInfoByPhone($phone);
- if($json_str){
- return json_decode($json_str);
- }else{
- return false;
- }
- }
- //获取一个随机字符串28位
- public static function getRandOpenid()
- {
- // 字符串长度
- $len = 28;
- $string = '';
- for ($i = 1; $i < $len; $i++) {
- $randstr = chr(rand(65, 90)); //指定为字母
- $string .= $randstr;
- }
- return substr(md5($string), 0, 28);
- }
- /**
- * 封装成接口需要的数据
- * @param $userinfo
- * @param $type
- * @return false|string
- */
- public function packageUserInfo($userinfo, $type = '')
- {
- $obj = [];
- unset($userinfo['isvip']);
- $obj = $userinfo;
- $obj['uid'] = $userinfo['id'] ?? '';
- $obj['token'] = $userinfo['openid'] ?? '';
- $obj['is_vip'] = $userinfo['vip_endtime'] > time() ? TRUE : FALSE;
- $obj['balance'] = FinancialService::instance()->getTotalKandianAndFreeKandian($userinfo['id'])->data;
- $signTodayData = model('Sign')->setConnect($obj['uid'])->userSignToday();
- if($signTodayData){
- $obj['is_today_signed'] = 1;
- }else{
- $obj['is_today_signed'] = 0;
- }
- if ($type == 'json') {
- return json_encode($obj);
- } else {
- return $obj;
- }
- }
- }
|