Openid.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\common\model;
  3. use app\main\service\ApiService;
  4. use app\source\service\UserService;
  5. use think\Model;
  6. class Openid extends Model
  7. {
  8. // 表名
  9. protected $table = 'openid';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. // 追加属性
  16. protected $append = [
  17. ];
  18. //当前链接的channel_openid分库
  19. protected $connectChannelOpenid = null;
  20. /**
  21. * 设置分库链接数据
  22. *
  23. * @param $channel_id
  24. * @param $openid
  25. * @return $this
  26. */
  27. public function setConnect($channel_id, $openid)
  28. {
  29. $channel_openid = $channel_id . '_' . $openid;
  30. if ($this->connectChannelOpenid != $channel_openid) {
  31. $database = get_db_connect($this->table, hash_code($channel_openid));
  32. $this->setTable($database['table']);
  33. $this->connect($database);
  34. $this->connectChannelOpenid = $channel_openid;
  35. }
  36. return $this;
  37. }
  38. /**
  39. * 根据渠道商ID,用户openid获取用户user_id
  40. *
  41. * @param $channel_id
  42. * @param $openid
  43. * @return bool|int
  44. */
  45. public function getUserId($channel_id, $openid)
  46. {
  47. if (ApiService::instance()->checkApiOn()) {
  48. return UserService::instance()->getUserInfoByChannelOpenid($channel_id, $openid)->id;
  49. }
  50. $channel_openid = $channel_id . '_' . $openid;
  51. $res = $this->setConnect($channel_id, $openid)->where(['channel_openid' => $channel_openid])->find();
  52. if ($res && isset($res['user_id'])) {
  53. return $res['user_id'];
  54. }
  55. return false;
  56. }
  57. }