<?php

namespace app\common\model;

use think\Model;

/**
 * 模型
 */
class UserLike extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';


    /**
     * 获取喜欢列表
     */
    public function getUserList($user_id,$pageStart=1,$pageNum=10) {
        $where = [];
        $where["a.fans_id"] = $user_id;
        $list = $this->alias("a")
            ->field("a.id,a.user_id,a.fans_id,u.nickname,u.avatar,u.age,u.constellation,u.wechat,hobby_ids,profession,u.copy_mobile,u.mobile")
            ->join("hx_user u","u.id = a.user_id","left")
            ->where($where)
            ->limit($pageStart,$pageNum)
            ->select();
        if($list) {
            $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
                wordwrap(config('public_key'), 64, PHP_EOL, true) .
                PHP_EOL."-----END PUBLIC KEY-----";
            foreach($list as $k => &$v) {
                if ($v['wechat']) {
                    $wechat = "";
                    openssl_public_encrypt($v['wechat'], $wechat, $public_key);
                    $v['wechat'] = base64_encode($wechat);
                } else {
                    $v['wechat'] = '';
                }
                $mobile = "";
//                openssl_private_encrypt($data['mobile'], $mobile, $private_key); // 使用私钥加密数据
                openssl_public_encrypt($v['mobile'], $mobile, $public_key);
                $v['mobile'] = base64_encode($mobile);

                if($v['hobby_ids']) {
                    $list[$k]['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($v['hobby_ids']);
                } else {
                    $list[$k]['hobby_ids'] = [];
                }
            }
        }
        return $list;
    }

    /**
     * 获取喜欢列表
     */
    public function getFansList($user_id,$pageStart=1,$pageNum=10) {
        $where = [];
        $where["a.user_id"] = $user_id;
        $list = $this->alias("a")
            ->field("a.id,a.fans_id as user_id,u.nickname,u.avatar,u.age,u.constellation,u.wechat,hobby_ids,profession,u.copy_mobile,u.mobile")
            ->join("hx_user u","u.id = a.fans_id","left")
            ->where($where)
            ->limit($pageStart,$pageNum)
            ->select();
        if($list) {
            $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
                wordwrap(config('public_key'), 64, PHP_EOL, true) .
                PHP_EOL."-----END PUBLIC KEY-----";
            foreach($list as $k => &$v) {
                if ($v['wechat']) {
                    $wechat = "";
                    openssl_public_encrypt($v['wechat'], $wechat, $public_key);
                    $v['wechat'] = base64_encode($wechat);
                } else {
                    $v['wechat'] = '';
                }
                $mobile = "";
//                openssl_private_encrypt($data['mobile'], $mobile, $private_key); // 使用私钥加密数据
                openssl_public_encrypt($v['mobile'], $mobile, $public_key);
                $v['mobile'] = base64_encode($mobile);

                if($v['hobby_ids']) {
                    $list[$k]['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($v['hobby_ids']);
                } else {
                    $list[$k]['hobby_ids'] = [];
                }
            }
        }
        return $list;
    }
}