<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class ShareController extends BaseController
{
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * 获取分享记录
	 */
	public function share_list()
	{
		if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid参数必须']);

		if (empty($this->data['page'])) apiReturn(['status' => -1, 'msg' => 'page参数必须']);

		$where = 'uu.admin_id = '.$this->data['business_id'].' AND us.admin_id = '.$this->data['business_id'].' AND uu.openid = "'.$this->data['openid'].'"';

		$field = 'uu.id user_id, uu.nickname unickname, uu.avatarurl uavatarurl, us.id share_id, us.nickname snickname, us.avatarurl savatarurl, am_share.int, am_share.create_time';

		$list = M('share')
				->join('am_users AS uu ON uu.id = am_share.user_id')
				->join('am_users AS us ON us.id = am_share.share_id')
				->field($field)
				->where($where)
				->order('am_share.create_time DESC')
				->page($this->data['page'])
				->select();

		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有分享记录']);

		foreach ($list as $k => &$v) {
			$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 分享配置
	 */
	public function share_config()
	{
		$config = M('share_config')->where(array('admin_id' => $this->data['business_id']))->find();

		if (empty($config)) {
			M('share_config')->add(array(
					'admin_id' => $this->data['business_id'],
					'create_time' => time()
				));
			$config = M('share_config')->field('num, acc, status')->where(array('admin_id' => $this->data['business_id']))->find();
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $config]);
	}
}