12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- namespace app\common\model;
- use app\main\service\ApiService;
- use app\source\service\UserService;
- use think\Model;
- class Openid extends Model
- {
- // 表名
- protected $table = 'openid';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
-
- // 追加属性
- protected $append = [
- ];
- //当前链接的channel_openid分库
- protected $connectChannelOpenid = null;
- /**
- * 设置分库链接数据
- *
- * @param $channel_id
- * @param $openid
- * @return $this
- */
- public function setConnect($channel_id, $openid)
- {
- $channel_openid = $channel_id . '_' . $openid;
- if ($this->connectChannelOpenid != $channel_openid) {
- $database = get_db_connect($this->table, hash_code($channel_openid));
- $this->setTable($database['table']);
- $this->connect($database);
- $this->connectChannelOpenid = $channel_openid;
- }
- return $this;
- }
- /**
- * 根据渠道商ID,用户openid获取用户user_id
- *
- * @param $channel_id
- * @param $openid
- * @return bool|int
- */
- public function getUserId($channel_id, $openid)
- {
- if (ApiService::instance()->checkApiOn()) {
- return UserService::instance()->getUserInfoByChannelOpenid($channel_id, $openid)->id;
- }
- $channel_openid = $channel_id . '_' . $openid;
- $res = $this->setConnect($channel_id, $openid)->where(['channel_openid' => $channel_openid])->find();
- if ($res && isset($res['user_id'])) {
- return $res['user_id'];
- }
- return false;
- }
- }
|