123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | 获取优惠券
- |--------------------------------------------------------------------------
- */
- namespace Youzan\Src;
- use Dever;
- use Main\Lib\Wechat;
- use Main\Lib\Core;
- class Card
- {
- public function test1()
- {
- Dever::import('queue');
- return Dever::len(DEVER_PROJECT . '_' . DEVER_APP_NAME . '_' . 'lock');
- }
- public function test2()
- {
- $id = Dever::queue('lock1', 11);
- echo 22222;
- return $id;
- }
- public function test3()
- {
- return Dever::queue('lock1');
- }
- # 分配优惠券,此处需要做高并发处理
- public function get()
- {
- $key = Dever::input('key');
- $user = Dever::input('user');
- $msg_page = Dever::input('msg_page');
- $msg_project_id = Dever::input('msg_project_id');
- $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) {
-
- //$id = Dever::queue('lock');
- $id = false;
- 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']) {
- if ($msg_page) {
- $info['msg_page'] = $msg_page;
- }
- $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 $card;
- }
- }
- }
- 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, true);
- }
- 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'] = 0;
- }
- 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'] = 0;
-
- $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);
- $id = Dever::db('youzan/card_list')->insert($update);
- Dever::queue('lock', $id);
- }
- }
- /*
- # 发送优惠券
- public function send()
- {
- $key = Dever::input('key');
- $project_id = Dever::input('project_id');
- $touser = Dever::input('touser');
- return $this->sendOne($key, $project_id, $touser);
- }
- # 发送优惠券
- public function sendOne($key, $project_id, $touser, $state = true)
- {
- if ($project_id > 0 && $key && $touser) {
- $info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
- if ($info) {
- Dever::setInput('mobile', $touser);
- Dever::load('youzan/user.reg');
- $update = array();
- $update['project_id'] = $info['project_id'];
- $update['card_id'] = $info['id'];
- $update['touser'] = $touser;
- $where = array();
- $where['option_card_id'] = $info['id'];
- //$where['option_project_id'] = $info['project_id'];
- $where['option_touser'] = $update['touser'];
- $id = Dever::upinto('youzan/card_log', $where, $update);
- return Core::run($info['project_id'], 'card', 'card.sendAction', 'card.sendLog', 'youzan', $id, $state);
- }
- }
- return false;
- }
- public function sendAction($id)
- {
- $info = Dever::db('youzan/card_log')->one($id);
- if ($info) {
- $msg = Dever::db('youzan/card')->one($info['card_id']);
- $info['mobile'] = $info['touser'];
- $info['coupon_group_id'] = $msg['card_id'];
- return $info;
- } else {
- die;
- }
- }
- public function sendLog($project_id, $data, $id = false)
- {
- $info = Dever::db('youzan/card_log')->one($id);
- if ($info) {
- $update['where_id'] = $info['id'];
- $update['result'] = json_encode($data);
- if (!isset($data['error_response'])) {
- $update['status'] = 2;
- } else {
- $update['status'] = 3;
- }
- $update['num'] = $info['num'] + 1;
- Dever::db('youzan/card_log')->update($update);
- }
- }
- */
- }
|