文档手册

微信- 从服务端获取粉丝-用户名是微信用户无头像的处理办法

2024-12-19 20:31:04

文件 :dayrui\App\Weixin\Models/User.php

public function insert_user($data) {

        $save = [
            'uid' => 0,
            'username' => '',
            'openid' => $data['openid'],
            'nickname' => dr_emoji2html($data['nickname']?$data['nickname']:'微信用户'),
            'sex' => $data['sex'],
            'city' => $data['city'],
            'province' => $data['province'],
            'country' => $data['country'],
            'headimgurl' => (string)$data['headimgurl'],
            'subscribe' => (int)$data['subscribe_time'],
            'subscribe_time' => SYS_TIME,
            'unionid' => (string)$data['unionid'],
            'remark' => dr_emoji2html($data['remark']),
            'groupids' => $this->_get_groupids($data['tagid_list']),
            'content' => dr_array2string($data),
        ];

        $oauth = $this->db->table('member_oauth')->where('oid', $data['openid'])->get()->getRowArray();
        if ($oauth) {
            $save['uid'] = $oauth['uid'];
            if($save['nickname'] == "微信用户"){
                $save['nickname'] = $oauth['nickname'];
            }  
            if(empty($save['headimgurl'])){
                $save['headimgurl'] = $oauth['avatar'];
            }          
            $save['username'] = \Phpcmf\Service::M('member')->username($save['uid']);
        }

        $row = $this->db->table(weixin_wxtable('user'))->where('openid', $data['openid'])->get()->getRowArray();
        if ($row) {
            $rt = $this->table(weixin_wxtable('user'))->update($row['id'], $save);
            if ($rt['code']) {
                $save['id'] = $row['id'];
                return $save;
            }
        } else {
            $rt = $this->table(weixin_wxtable('user'))->insert($save);
            if ($rt['code']) {
                $save['id'] = $rt['code'];
                return $save;
            }
        }

        return [];
    }