1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2019/4/15
- * Time: 14:46
- */
- namespace app\common\model;
- use app\common\library\Redis;
- use app\common\service\LogService;
- use think\Model;
- class UserPhone extends Model
- {
- protected $table = 'user_phone';
- //当前链接的user_id分库
- protected $connectUserId = null;
- /**
- * 设置分库链接数据
- * @param $phone
- * @return $this
- */
- public function setConnect($phone)
- {
- if ($this->connectUserId != $phone) {
- $database = get_db_connect($this->table, $phone);
- $this->setTable($database['table']);
- $this->connect($database);
- $this->sequence('id');
- $this->connectUserId = $phone;
- }
- return $this;
- }
- /**
- * @param $phone
- * @return array
- * @throws \think\Exception
- */
- public function getInfoByPhone($phone)
- {
- $key = 'UPHONE:' . $phone;
- $redis = Redis::instance();
- LogService::info('[ UserPhone ] [ getInfoByPhone ] [ RedisKey ]' . $key);
- LogService::info('[ UserPhone ] [ getInfoByPhone ] [ Redis ]' . json_encode(print_r($redis, true)));
- $data = [];
- if ($redis->exists($key)) {
- $data = $redis->get($key);
- } else {
- $obj = $this->setConnect($phone)->where('phone', '=', $phone)->find();
- if($obj){
- $data_arr = $obj->toArray();
- $data = json_encode($data_arr);
- $redis->set($key, $data, 7 * 24 * 60 * 60);
- }
- }
- return $data;
- }
- /**
- * 更改手机号
- * @param $uid
- * @param $oldphone
- * @param $newphone
- * @return UserPhone
- */
- public function modifyPhone($uid, $oldphone, $newphone)
- {
- try{
- $this->setConnect($oldphone)->where(['phone' => $oldphone])->delete();
- $result = $this->setConnect($newphone)->save(['uid' => $uid, 'phone' => $newphone]);
- return $result;
- }catch (\Exception $e){
- LogService::error('[ UserPhone ]'. $e->getMessage());
- }
- }
- }
|