Card.php 7.2 KB

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