|  | @@ -12,7 +12,112 @@ use Main\Lib\Core;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  class Card
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -	# 发送单条模板消息
 | 
	
		
			
				|  |  | +	public function get()
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		$key = Dever::input('key');
 | 
	
		
			
				|  |  | +		$user = Dever::input('user');
 | 
	
		
			
				|  |  | +		$project_id = Dever::input('project_id');
 | 
	
		
			
				|  |  | +		$info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
 | 
	
		
			
				|  |  | +		if ($info) {
 | 
	
		
			
				|  |  | +			$one = Dever::db('youzan/card_list')->one(array('option_card_id' => $info['id'], 'option_user' => $user));
 | 
	
		
			
				|  |  | +			if (!$one) {
 | 
	
		
			
				|  |  | +				$one = Dever::db('youzan/card_list')->getAll(array('option_card_id' => $info['id'], 'option_status' => 1));
 | 
	
		
			
				|  |  | +				$key = array_rand($one);
 | 
	
		
			
				|  |  | +				if (isset($one[$key]) && $one[$key]) {
 | 
	
		
			
				|  |  | +					Dever::db('youzan/card_list')->update(array('where_id' => $one[$key]['id'], 'user' => $user, 'status' => 2));
 | 
	
		
			
				|  |  | +					return $one[$key];
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		Dever::alert('error');
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	public function create()
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		$id = Dever::input('id');
 | 
	
		
			
				|  |  | +		$info = Dever::db('youzan/card')->one($id);
 | 
	
		
			
				|  |  | +		if ($info) {
 | 
	
		
			
				|  |  | +			Core::run($info['project_id'], 'card_create', 'card.createStart', 'card.createEnd', 'youzan', $id, false);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		Dever::alert('提交成功,请到优惠券列表中查看');
 | 
	
		
			
				|  |  | +		return;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	public function createStart($id)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		$info = Dever::db('youzan/card')->one($id);
 | 
	
		
			
				|  |  | +		if ($info) {
 | 
	
		
			
				|  |  | +			$info['title'] = $info['name'];
 | 
	
		
			
				|  |  | +			if ($info['at_least'] > 0) {
 | 
	
		
			
				|  |  | +				$info['is_at_least'] = 1;
 | 
	
		
			
				|  |  | +			} else {
 | 
	
		
			
				|  |  | +				$info['is_at_least'] = 0;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			$info['can_give_friend'] = 0;
 | 
	
		
			
				|  |  | +			$info['date_type'] = 1;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			$info['expire_notice'] = $info['expire_notice'] - 1;
 | 
	
		
			
				|  |  | +			$info['fixed_begin_term'] = 0;
 | 
	
		
			
				|  |  | +			$info['fixed_term'] = 0;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if (!$info['discount']) {
 | 
	
		
			
				|  |  | +				$info['discount'] = 10;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if ($info['value_random_to']) {
 | 
	
		
			
				|  |  | +				$info['is_random'] = 1;
 | 
	
		
			
				|  |  | +			} else {
 | 
	
		
			
				|  |  | +				$info['is_random'] = 0;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			if ($info['need_user_level'] <= 0) {
 | 
	
		
			
				|  |  | +				$info['need_user_level'] = 0;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if ($info['quota'] <= 0) {
 | 
	
		
			
				|  |  | +				$info['quota'] = 0;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if ($info['specify_item_ids']) {
 | 
	
		
			
				|  |  | +				$info['range_type'] = 'PART';
 | 
	
		
			
				|  |  | +			} else {
 | 
	
		
			
				|  |  | +				$info['range_type'] = 'ALL';
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			$info['preferential_type'] = 2;
 | 
	
		
			
				|  |  | +			
 | 
	
		
			
				|  |  | +			$info['is_sync_weixin'] = $info['is_sync_weixin'] - 1;
 | 
	
		
			
				|  |  | +			$info['is_share'] = $info['is_share'] - 1;
 | 
	
		
			
				|  |  | +			$info['is_forbid_preference'] = $info['is_forbid_preference'] - 1;
 | 
	
		
			
				|  |  | +			$info['at_least'] = $info['at_least'];
 | 
	
		
			
				|  |  | +			$info['start_at'] = date('Y-m-d H:i:s', $info['sdate']);
 | 
	
		
			
				|  |  | +			$info['end_at'] = date('Y-m-d H:i:s', $info['edate']);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if ($info['create_num'] > 1) {
 | 
	
		
			
				|  |  | +				$info['run_num'] = $info['create_num'];
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			return $info;
 | 
	
		
			
				|  |  | +		} else {
 | 
	
		
			
				|  |  | +			die;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	public function createEnd($project_id, $data, $id = false)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		$info = Dever::db('youzan/card')->one($id);
 | 
	
		
			
				|  |  | +		if ($info && isset($data['response']['promocard']['group_id'])) {
 | 
	
		
			
				|  |  | +			$update['card_id'] = $info['id'];
 | 
	
		
			
				|  |  | +			$update['project_id'] = $info['project_id'];
 | 
	
		
			
				|  |  | +			$update['user'] = '';
 | 
	
		
			
				|  |  | +			$update['status'] = 1;
 | 
	
		
			
				|  |  | +			$update['group_id'] = $data['response']['promocard']['group_id'];
 | 
	
		
			
				|  |  | +			$update['code_url'] = $data['response']['promocard']['fetch_url'];
 | 
	
		
			
				|  |  | +			$update['result'] = json_encode($data);
 | 
	
		
			
				|  |  | +			Dever::db('youzan/card_list')->insert($update);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	/*
 | 
	
		
			
				|  |  | +	# 发送优惠券
 | 
	
		
			
				|  |  |  	public function send()
 | 
	
		
			
				|  |  |  	{
 | 
	
		
			
				|  |  |  		$key = Dever::input('key');
 | 
	
	
		
			
				|  | @@ -21,7 +126,7 @@ class Card
 | 
	
		
			
				|  |  |  		return $this->sendOne($key, $project_id, $touser);
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	# 发送单条模板消息
 | 
	
		
			
				|  |  | +	# 发送优惠券
 | 
	
		
			
				|  |  |  	public function sendOne($key, $project_id, $touser, $state = true)
 | 
	
		
			
				|  |  |  	{
 | 
	
		
			
				|  |  |  		if ($project_id > 0 && $key && $touser) {
 | 
	
	
		
			
				|  | @@ -75,4 +180,5 @@ class Card
 | 
	
		
			
				|  |  |  			Dever::db('youzan/card_log')->update($update);
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +	*/
 | 
	
		
			
				|  |  |  }
 |