Card.php 6.7 KB

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