dever 6 years ago
parent
commit
54a389b9c7
5 changed files with 485 additions and 36 deletions
  1. 13 4
      main/lib/Core.php
  2. 52 0
      youzan/config/wechat.php
  3. 261 8
      youzan/database/card.php
  4. 51 22
      youzan/database/card_list.php
  5. 108 2
      youzan/src/Card.php

+ 13 - 4
main/lib/Core.php

@@ -15,10 +15,19 @@ function callAction($send)
 		if ($send['s']) {
 			$param = Dever::load($send['a'] . '/' . $send['s'], $send['p']);
 		}
-		
-		$result = $wechat->curl($send['t'], $param, false);
-		if ($send['e']) {
-			Dever::load($send['a'] . '/' . $send['e'], $send['i'], $result, $send['p']);
+
+		if (isset($param['run_num']) && $param['run_num'] > 1) {
+			for ($i = 1; $i <= $param['run_num']; $i++) {
+				$result = $wechat->curl($send['t'], $param, false);
+				if ($send['e']) {
+					Dever::load($send['a'] . '/' . $send['e'], $send['i'], $result, $send['p']);
+				}
+			}
+		} else {
+			$result = $wechat->curl($send['t'], $param, false);
+			if ($send['e']) {
+				Dever::load($send['a'] . '/' . $send['e'], $send['i'], $result, $send['p']);
+			}
 		}
 
 		return $result;

+ 52 - 0
youzan/config/wechat.php

@@ -62,4 +62,56 @@ $config['card'] = array
 	),
 );
 
+$config['card_create'] = array
+(
+	'name' => '生成优惠券',
+	'method' => 'post',
+	'json' => false,
+	'url' => 'https://open.youzan.com/api/oauthentry/youzan.ump.promocard/3.0.0/add?',
+	'param' => array
+	(
+		'access_token' => 'token',
+		'at_least' => 'at_least',
+		'can_give_friend' => 'can_give_friend',
+		'date_type' => 'date_type',
+		'description' => 'description',
+		'discount' => 'discount',
+		'end_at' => 'end_at',
+		'expire_notice' => 'expire_notice',
+		'fixed_begin_term' => 'fixed_begin_term',
+		'fixed_term' => 'fixed_term',
+
+		'is_at_least' => 'is_at_least',
+		'is_forbid_preference' => 'is_forbid_preference',
+		'is_random' => 'is_random',
+		'is_share' => 'is_share',
+
+		'is_sync_weixin' => 'is_sync_weixin',
+		'mark_tag_ids' => 'mark_tag_ids',
+		'need_user_level' => 'need_user_level',
+		'preferential_type' => 'preferential_type',
+
+		'quota' => 'quota',
+		'range_type' => 'range_type',
+		'service_phone' => 'service_phone',
+		'specify_item_ids' => 'specify_item_ids',
+
+		'start_at' => 'start_at',
+		'title' => 'title',
+		'total' => 'total',
+		'value' => 'value',
+
+		'value_random_to' => 'value_random_to',
+		'weixin_color' => 'weixin_color',
+		'weixin_color_rgb' => 'weixin_color_rgb',
+		'weixin_sub_title' => 'weixin_sub_title',
+		'weixin_title' => 'weixin_title',
+	),
+	//针对一些返回的名称,做转换
+	'response' => array
+	(
+		
+	),
+);
+
 return $config;

+ 261 - 8
youzan/database/card.php

@@ -1,11 +1,23 @@
 <?php
 
+$type = array
+(
+	1 => '否',
+	2 => '是',
+);
+
+$range_type = array
+(
+	1 => '全部商品可用',
+	2 => '指定商品可用',
+);
+
 return array
 (
 	# 表名
 	'name' => 'card',
 	# 显示给用户看的名称
-	'lang' => '优惠券码',
+	'lang' => '优惠券管理',
 	'order' => 100,
 	# 数据结构
 	'struct' => array
@@ -31,12 +43,19 @@ return array
 			'list'		=> true,
 		),
 
+		'hr1'		=> array
+		(
+			'name' 		=> '基本信息',
+			'class'		=> '',//本项必须填写
+			'attr'		=> '',
+		),
+
 		'key'		=> array
 		(
 			'type' 		=> 'varchar-20',
-			'name' 		=> '活动key-通过key值搜索数据',
+			'name' 		=> '优惠券key-通过key值搜索数据',
 			'default' 	=> '',
-			'desc' 		=> '活动key-通过key值搜索数据',
+			'desc' 		=> '优惠券key-通过key值搜索数据',
 			'match' 	=> 'is_string',
 			'update'	=> 'text',
 			'list'		=> true,
@@ -46,25 +65,255 @@ return array
 		'name'		=> array
 		(
 			'type' 		=> 'varchar-120',
-			'name' 		=> '标题',
+			'name' 		=> '名称',
 			'default' 	=> '',
-			'desc' 		=> '标题',
+			'desc' 		=> '名称',
 			'match' 	=> 'is_string',
 			'update'	=> 'text',
 			'list'		=> true,
 		),
 
-		'card_id'		=> array
+		'description'		=> array
 		(
 			'type' 		=> 'varchar-120',
-			'name' 		=> '活动id',
+			'name' 		=> '使用说明',
 			'default' 	=> '',
-			'desc' 		=> '活动id',
+			'desc' 		=> '使用说明',
 			'match' 	=> 'is_string',
+			'update'	=> 'textarea',
+			//'list'		=> true,
+		),
+
+		'create_num'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '生成数量',
+			'default' 	=> '',
+			'desc' 		=> '生成数量',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'text',
+			'list'		=> true,
+			'edit'		=> true,
+		),
+
+		'edate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '优惠券结束时间',
+			'match' 	=> 'is_numeric',
+			'desc' 		=> '优惠券结束时间',
+			'update'	=> 'date',
+			//'list'		=> 'date("Y-m-d H:i:s", {sdate})',
+			'callback'	=> 'maketime',
+		),
+
+		'sdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '优惠券生效时间',
+			'match' 	=> 'is_numeric',
+			'desc' 		=> '优惠券生效时间',
+			'update'	=> 'date',
+			//'list'		=> 'date("Y-m-d H:i:s", {sdate})',
+			'callback'	=> 'maketime',
+		),
+
+		'hr2'		=> array
+		(
+			'name' 		=> '基本设置',
+			'class'		=> '',//本项必须填写
+			'attr'		=> '',
+		),
+
+		'quota'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '每人限领个数',
+			'default' 	=> '-1',
+			'desc' 		=> '每人限领个数',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+		),
+
+		'total'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '总库存',
+			'default' 	=> '1',
+			'desc' 		=> '总库存',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+		),
+
+		'value'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '优惠券面值-如果填写了以下随机面额,则本字段为随机面额最小值',
+			'default' 	=> '0.01',
+			'desc' 		=> '优惠券面值',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+		),
+
+		'value_random_to'		=> array
+		(
+			'type' 		=> 'varchar-20',
+			'name' 		=> '优惠券随机面额-随机面额最大值',
+			'default' 	=> '',
+			'desc' 		=> '优惠券随机面额',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+		),
+
+		'specify_item_ids'		=> array
+		(
+			'type' 		=> 'varchar-600',
+			'name' 		=> '可选商品ID-需要指定可用商品ID,多个用半角逗号分隔,如不指定则为全部商品可用',
+			'default' 	=> '',
+			'desc' 		=> '可选商品ID',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
+		'at_least'		=> array
+		(
+			'type' 		=> 'varchar-20',
+			'name' 		=> '优惠券使用价格-订单满这个价格,优惠券才可以使用',
+			'default' 	=> '',
+			'desc' 		=> '订单满这个价格,优惠券才可以使用',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
+		'discount'		=> array
+		(
+			'type' 		=> 'varchar-20',
+			'name' 		=> '优惠券使用折扣-如填写88,就是8.8折',
+			'default' 	=> '',
+			'desc' 		=> '优惠券使用折扣',
+			'match' 	=> 'option',
 			'update'	=> 'text',
 			//'list'		=> true,
 		),
 		
+		'expire_notice'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '是否发送提醒-是否到期前4天发送提醒',
+			'default' 	=> '1',
+			'desc' 		=> '是否发送提醒',
+			'match' 	=> 'option',
+			'update'	=> 'select',
+			'option'	=> $type,
+		),
+
+		'is_forbid_preference'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '是否仅原价购买商品时可用',
+			'default' 	=> '1',
+			'desc' 		=> '是否仅原价购买商品时可用',
+			'match' 	=> 'option',
+			'update'	=> 'select',
+			'option'	=> $type,
+		),
+
+		'is_share'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '是否可分享领取链接',
+			'default' 	=> '1',
+			'desc' 		=> '是否可分享领取链接',
+			'match' 	=> 'option',
+			'update'	=> 'select',
+			'option'	=> $type,
+		),
+
+		'mark_tag_ids'		=> array
+		(
+			'type' 		=> 'varchar-600',
+			'name' 		=> '标签设置-领取优惠券要给用户打上的标签的ID,多个用半角逗号分隔',
+			'default' 	=> '',
+			'desc' 		=> '标签设置',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
+		'need_user_level'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '是否限制领用者的等级-小于0表示不限制,大于0 表示领用者必须是这个等级ID',
+			'default' 	=> '-1',
+			'desc' 		=> '是否限制领用者的等级',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+		),
+
+		'hr3'		=> array
+		(
+			'name' 		=> '微信设置',
+			'class'		=> '',//本项必须填写
+			'attr'		=> '',
+		),
+
+		'is_sync_weixin'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '是否同步微信卡券',
+			'default' 	=> '1',
+			'desc' 		=> '是否同步微信卡券',
+			'match' 	=> 'option',
+			'update'	=> 'select',
+			'option'	=> $type,
+		),
+
+		'weixin_title'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '卡券标题',
+			'default' 	=> '',
+			'desc' 		=> '卡券标题',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
+		'weixin_sub_title'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '卡券副标题',
+			'default' 	=> '',
+			'desc' 		=> '卡券副标题',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
+		'weixin_color_rgb'		=> array
+		(
+			'type' 		=> 'varchar-20',
+			'name' 		=> '卡券颜色的值-例如:#55bd47',
+			'default' 	=> '',
+			'desc' 		=> '卡券颜色的值',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
+		'service_phone'		=> array
+		(
+			'type' 		=> 'varchar-20',
+			'name' 		=> '卡券的客服电话',
+			'default' 	=> '',
+			'desc' 		=> '卡券的客服电话',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
 		'state'		=> array
 		(
 			'type' 		=> 'tinyint-1',
@@ -92,5 +341,9 @@ return array
 		//'delete' => false,
 		//'insert' => false,
 		//'edit' => false,
+		'list_button' => array
+		(
+			'oper' => array('立刻生成', '"youzan/card.create?id={id}"'),
+		),
 	),
 );

+ 51 - 22
youzan/database/card_log.php → youzan/database/card_list.php

@@ -1,18 +1,16 @@
 <?php
 
-$status = array
+$type = array
 (
-	1 => '发送中',
-	2 => '发送成功',
-	3 => '发送失败',
+	1 => '未使用',
+	2 => '已使用',
 );
-
 return array
 (
 	# 表名
-	'name' => 'card_log',
+	'name' => 'card_list',
 	# 显示给用户看的名称
-	'lang' => '优惠券发送日志',
+	'lang' => '单个优惠券列表',
 	'order' => 90,
 	# 数据结构
 	'struct' => array
@@ -48,24 +46,37 @@ return array
 			'list'		=> 'Dever::load("youzan/card-one#name", {card_id})',
 		),
 
-		'touser'		=> array
+		'user'		=> array
 		(
 			'type' 		=> 'varchar-120',
-			'name' 		=> '接收者',
+			'name' 		=> '使用者',
 			'default' 	=> '',
-			'desc' 		=> '接收者',
+			'desc' 		=> '使用者',
 			'match' 	=> 'is_string',
 			'update'	=> 'text',
+			'search'	=> 'fulltext',
 			'list'		=> true,
 		),
 
-		'num'		=> array
+		'group_id'		=> array
 		(
-			'type' 		=> 'int-11',
-			'name' 		=> '发送次数',
-			'default' 	=> '0',
-			'desc' 		=> '发送次数',
+			'type' 		=> 'varchar-20',
+			'name' 		=> '优惠券组id',
+			'default' 	=> '',
+			'desc' 		=> '优惠券组id',
+			'match' 	=> 'option',
+			//'search'	=> 'order',
+			'list'		=> true,
+		),
+
+		'code_url'		=> array
+		(
+			'type' 		=> 'varchar-200',
+			'name' 		=> '优惠券地址',
+			'default' 	=> '',
+			'desc' 		=> '优惠券地址',
 			'match' 	=> 'option',
+			'search'	=> 'fulltext',
 			//'search'	=> 'order',
 			'list'		=> true,
 		),
@@ -73,27 +84,28 @@ return array
 		'result'		=> array
 		(
 			'type' 		=> 'text-255',
-			'name' 		=> '微信返回信息',
+			'name' 		=> '有赞返回信息',
 			'default' 	=> '',
-			'desc' 		=> '微信返回信息',
+			'desc' 		=> '有赞返回信息',
 			'match' 	=> 'option',
 			'update'	=> 'textarea',
 			'list'		=> true,
 			'modal'		=> '查看详情',
 		),
-
+		
 		'status'		=> array
 		(
 			'type' 		=> 'tinyint-1',
-			'name' 		=> '状态',
+			'name' 		=> '领取状态',
 			'default' 	=> '1',
-			'desc' 		=> '状态',
+			'desc' 		=> '领取状态',
 			'match' 	=> 'is_numeric',
 			'update'	=> 'select',
-			'option'	=> $status,
+			'option'	=> $type,
 			'list'		=> true,
+			//'edit'		=> true,
 		),
-		
+
 		'state'		=> array
 		(
 			'type' 		=> 'tinyint-1',
@@ -122,4 +134,21 @@ return array
 		'insert' => false,
 		'edit' => false,
 	),
+
+	'request' => array
+	(
+		'getAll' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'status' => 'yes',
+				'cate_id' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('id' => 'desc'),
+			'col' => '*',
+		),
+	),
 );

+ 108 - 2
youzan/src/Card.php

@@ -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);
 		}
 	}
+	*/
 }