Card.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 获取优惠券
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Youzan\Src;
  8. use Dever;
  9. use Main\Lib\Wechat;
  10. use Main\Lib\Core;
  11. class Card
  12. {
  13. public $lock = 'cardv2';
  14. public function len($id = false)
  15. {
  16. $id = Dever::input('id', $id);
  17. $len = Dever::queue($this->lock . '_' . $id, 'len');
  18. return $len;
  19. }
  20. public function total()
  21. {
  22. $info = Dever::db('youzan/card')->state();
  23. if ($info) {
  24. foreach ($info as $k => $v) {
  25. $total = Dever::db('youzan/card_list')->total(array('option_state' => 1, 'option_status' => 1, 'option_card_id' => $v['id']));
  26. print_r($total);
  27. echo '<br />';
  28. Dever::lock($this->lock . '_' . $v['id'], $total);
  29. }
  30. }
  31. return true;
  32. }
  33. # 分配优惠券,此处需要做高并发处理
  34. public function get()
  35. {
  36. $key = Dever::input('key');
  37. $user = Dever::input('user');
  38. $msg_page = Dever::input('msg_page');
  39. $msg_project_id = Dever::input('msg_project_id');
  40. $project_id = Dever::input('project_id');
  41. $info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
  42. if ($info) {
  43. $one = Dever::db('youzan/card_list')->one(array('option_card_id' => $info['id'], 'option_user' => $user));
  44. if ($one) {
  45. return $one;
  46. } else {
  47. $state = Dever::lock($this->lock . '_' . $info['id']);
  48. if (!$state) {
  49. Dever::alert('error');
  50. } else {
  51. $list = Dever::db('youzan/card_list')->getAll(array('option_card_id' => $info['id'], 'option_status' => 1));
  52. $key = array_rand($list);
  53. if (isset($list[$key]) && $list[$key]) {
  54. $card = $list[$key];
  55. }
  56. }
  57. if (isset($card) && $card) {
  58. $one = Dever::db('youzan/card_list')->one(array('option_card_id' => $info['id'], 'option_user' => $user));
  59. if ($one) {
  60. return $one;
  61. }
  62. Dever::db('youzan/card_list')->update(array('where_id' => $card['id'], 'user' => $user, 'status' => 2));
  63. # 检查info,有无msg_key,有则发送模板消息
  64. if ($msg_project_id && $info['msg_key'] && $info['msg_page'] && $info['msg_content']) {
  65. if ($msg_page) {
  66. $info['msg_page'] = $msg_page;
  67. }
  68. $send = Dever::load('applet/msg')->getData($info['msg_content']);
  69. Dever::load('applet/msg')->sendOne($info['msg_key'], $msg_project_id, $user, $info['msg_page'], $send);
  70. }
  71. return $card;
  72. }
  73. }
  74. }
  75. Dever::alert('error');
  76. }
  77. public function create()
  78. {
  79. $id = Dever::input('id');
  80. $info = Dever::db('youzan/card')->one($id);
  81. if ($info) {
  82. Core::run($info['project_id'], 'card_create', 'card.createStart', 'card.createEnd', 'youzan', $id, false);
  83. }
  84. Dever::alert('提交成功,请到优惠券列表中查看');
  85. return;
  86. }
  87. public function createStart($id)
  88. {
  89. $info = Dever::db('youzan/card')->one($id);
  90. if ($info) {
  91. $info['title'] = $info['name'];
  92. if ($info['at_least'] > 0) {
  93. $info['is_at_least'] = 1;
  94. } else {
  95. $info['is_at_least'] = 0;
  96. }
  97. $info['can_give_friend'] = 0;
  98. $info['date_type'] = 1;
  99. $info['expire_notice'] = $info['expire_notice'] - 1;
  100. $info['fixed_begin_term'] = 0;
  101. //$info['fixed_term'] = 0;
  102. if ($info['fixed_term']) {
  103. $info['date_type'] = 2;
  104. }
  105. if (!$info['discount']) {
  106. $info['discount'] = 0;
  107. }
  108. if ($info['value_random_to']) {
  109. $info['is_random'] = 1;
  110. } else {
  111. $info['is_random'] = 0;
  112. }
  113. if ($info['need_user_level'] <= 0) {
  114. $info['need_user_level'] = 0;
  115. }
  116. if ($info['quota'] <= 0) {
  117. $info['quota'] = 0;
  118. }
  119. if ($info['specify_item_ids']) {
  120. $info['range_type'] = 'PART';
  121. } else {
  122. $info['range_type'] = 'ALL';
  123. }
  124. $info['preferential_type'] = 0;
  125. $info['is_sync_weixin'] = $info['is_sync_weixin'] - 1;
  126. $info['is_share'] = $info['is_share'] - 1;
  127. $info['is_forbid_preference'] = $info['is_forbid_preference'] - 1;
  128. $info['at_least'] = $info['at_least'];
  129. $info['start_at'] = date('Y-m-d H:i:s', $info['sdate']);
  130. $info['end_at'] = date('Y-m-d H:i:s', $info['edate']);
  131. if ($info['create_num'] > 1) {
  132. $info['run_num'] = $info['create_num'];
  133. } else {
  134. $info['run_num'] = 1;
  135. }
  136. return $info;
  137. } else {
  138. die;
  139. }
  140. }
  141. public function createEnd($project_id, $data, $id = false, $end = false)
  142. {
  143. if ($end) {
  144. # 如果执行结束,则加到库存里
  145. $this->total();
  146. } else {
  147. $info = Dever::db('youzan/card')->one($id);
  148. //$data['response']['promocard']['group_id'] = 1;
  149. //$data['response']['promocard']['fetch_url'] = rand(1,100);
  150. if ($info && isset($data['response']['promocard']['group_id'])) {
  151. $update['card_id'] = $info['id'];
  152. $update['project_id'] = $info['project_id'];
  153. $update['user'] = '';
  154. $update['status'] = 1;
  155. $update['group_id'] = $data['response']['promocard']['group_id'];
  156. $update['code_url'] = $data['response']['promocard']['fetch_url'];
  157. $update['cdate'] = time();
  158. $update['result'] = json_encode($data);
  159. Dever::db('youzan/card_list')->insert($update);
  160. }
  161. }
  162. }
  163. /*
  164. # 发送优惠券
  165. public function send()
  166. {
  167. $key = Dever::input('key');
  168. $project_id = Dever::input('project_id');
  169. $touser = Dever::input('touser');
  170. return $this->sendOne($key, $project_id, $touser);
  171. }
  172. # 发送优惠券
  173. public function sendOne($key, $project_id, $touser, $state = true)
  174. {
  175. if ($project_id > 0 && $key && $touser) {
  176. $info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
  177. if ($info) {
  178. Dever::setInput('mobile', $touser);
  179. Dever::load('youzan/user.reg');
  180. $update = array();
  181. $update['project_id'] = $info['project_id'];
  182. $update['card_id'] = $info['id'];
  183. $update['touser'] = $touser;
  184. $where = array();
  185. $where['option_card_id'] = $info['id'];
  186. //$where['option_project_id'] = $info['project_id'];
  187. $where['option_touser'] = $update['touser'];
  188. $id = Dever::upinto('youzan/card_log', $where, $update);
  189. return Core::run($info['project_id'], 'card', 'card.sendAction', 'card.sendLog', 'youzan', $id, $state);
  190. }
  191. }
  192. return false;
  193. }
  194. public function sendAction($id)
  195. {
  196. $info = Dever::db('youzan/card_log')->one($id);
  197. if ($info) {
  198. $msg = Dever::db('youzan/card')->one($info['card_id']);
  199. $info['mobile'] = $info['touser'];
  200. $info['coupon_group_id'] = $msg['card_id'];
  201. return $info;
  202. } else {
  203. die;
  204. }
  205. }
  206. public function sendLog($project_id, $data, $id = false)
  207. {
  208. $info = Dever::db('youzan/card_log')->one($id);
  209. if ($info) {
  210. $update['where_id'] = $info['id'];
  211. $update['result'] = json_encode($data);
  212. if (!isset($data['error_response'])) {
  213. $update['status'] = 2;
  214. } else {
  215. $update['status'] = 3;
  216. }
  217. $update['num'] = $info['num'] + 1;
  218. Dever::db('youzan/card_log')->update($update);
  219. }
  220. }
  221. */
  222. }