dever 6 gadi atpakaļ
vecāks
revīzija
11671d4465
3 mainītis faili ar 39 papildinājumiem un 6 dzēšanām
  1. 9 0
      youzan/config/base.php
  2. 1 0
      youzan/database/card_list.php
  3. 29 6
      youzan/src/Card.php

+ 9 - 0
youzan/config/base.php

@@ -5,6 +5,15 @@ $config['base'] = array
 (
 	# 后台头部菜单
 	'top' => 'main/project_id-7',
+
+	# 定义队列
+	'queue' => array
+	(
+		'method' => 'redis',
+		'host' => '172.26.102.182',
+		'port' => '6379',
+		'key' => false,
+	),
 );
 
 # 模板配置

+ 1 - 0
youzan/database/card_list.php

@@ -149,6 +149,7 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
+			'limit' => '0,100',
 			'order' => array('id' => 'desc'),
 			'col' => '*',
 		),

+ 29 - 6
youzan/src/Card.php

@@ -12,6 +12,17 @@ use Main\Lib\Core;
 
 class Card
 {
+	public function test1()
+	{
+		return Dever::queue('test', 111);
+	}
+
+	public function test2()
+	{
+		return Dever::queue('test');
+	}
+
+	# 分配优惠券,此处需要做高并发处理
 	public function get()
 	{
 		$key = Dever::input('key');
@@ -23,10 +34,21 @@ class Card
 		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));
+				
+				$id = Dever::queue('lock');
+				if ($id) {
+					$card =  Dever::db('youzan/card_list')->one($id);
+				} /*else {
+					$list = Dever::db('youzan/card_list')->getAll(array('option_card_id' => $info['id'], 'option_status' => 1));
+					$key = array_rand($list);
+					if (isset($list[$key]) && $list[$key]) {
+						$card = $list[$key];
+					}
+				}
+				*/
+
+				if (isset($card) && $card) {
+					Dever::db('youzan/card_list')->update(array('where_id' => $card['id'], 'user' => $user, 'status' => 2));
 
 					# 检查info,有无msg_key,有则发送模板消息
 					if ($msg_project_id && $info['msg_key'] && $info['page'] && $info['msg_content']) {
@@ -36,7 +58,7 @@ class Card
 						$send = Dever::load('applet/msg')->getData($info['msg_content']);
 						Dever::load('applet/msg')->sendOne($info['msg_key'], $msg_project_id, $user, $info['msg_page'], $send);
 					}
-					return $one[$key];
+					return $card;
 				}
 			}
 		}
@@ -123,7 +145,8 @@ class Card
 			$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);
+			$id = Dever::db('youzan/card_list')->insert($update);
+			Dever::queue('lock', $id);
 		}
 	}