UserPhone.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2019/4/15
  6. * Time: 14:46
  7. */
  8. namespace app\common\model;
  9. use app\common\library\Redis;
  10. use app\common\service\LogService;
  11. use think\Model;
  12. class UserPhone extends Model
  13. {
  14. protected $table = 'user_phone';
  15. //当前链接的user_id分库
  16. protected $connectUserId = null;
  17. /**
  18. * 设置分库链接数据
  19. * @param $phone
  20. * @return $this
  21. */
  22. public function setConnect($phone)
  23. {
  24. if ($this->connectUserId != $phone) {
  25. $database = get_db_connect($this->table, $phone);
  26. $this->setTable($database['table']);
  27. $this->connect($database);
  28. $this->sequence('id');
  29. $this->connectUserId = $phone;
  30. }
  31. return $this;
  32. }
  33. /**
  34. * @param $phone
  35. * @return array
  36. * @throws \think\Exception
  37. */
  38. public function getInfoByPhone($phone)
  39. {
  40. $key = 'UPHONE:' . $phone;
  41. $redis = Redis::instance();
  42. LogService::info('[ UserPhone ] [ getInfoByPhone ] [ RedisKey ]' . $key);
  43. LogService::info('[ UserPhone ] [ getInfoByPhone ] [ Redis ]' . json_encode(print_r($redis, true)));
  44. $data = [];
  45. if ($redis->exists($key)) {
  46. $data = $redis->get($key);
  47. } else {
  48. $obj = $this->setConnect($phone)->where('phone', '=', $phone)->find();
  49. if($obj){
  50. $data_arr = $obj->toArray();
  51. $data = json_encode($data_arr);
  52. $redis->set($key, $data, 7 * 24 * 60 * 60);
  53. }
  54. }
  55. return $data;
  56. }
  57. /**
  58. * 更改手机号
  59. * @param $uid
  60. * @param $oldphone
  61. * @param $newphone
  62. * @return UserPhone
  63. */
  64. public function modifyPhone($uid, $oldphone, $newphone)
  65. {
  66. try{
  67. $this->setConnect($oldphone)->where(['phone' => $oldphone])->delete();
  68. $result = $this->setConnect($newphone)->save(['uid' => $uid, 'phone' => $newphone]);
  69. return $result;
  70. }catch (\Exception $e){
  71. LogService::error('[ UserPhone ]'. $e->getMessage());
  72. }
  73. }
  74. }