Card.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 function test1()
  14. {
  15. Dever::import('queue');
  16. return Dever::len(DEVER_PROJECT . '_' . DEVER_APP_NAME . '_' . 'lock');
  17. }
  18. # 分配优惠券,此处需要做高并发处理
  19. public function get()
  20. {
  21. $key = Dever::input('key');
  22. $user = Dever::input('user');
  23. $msg_page = Dever::input('msg_page');
  24. $msg_project_id = Dever::input('msg_project_id');
  25. $project_id = Dever::input('project_id');
  26. $info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
  27. if ($info) {
  28. $one = Dever::db('youzan/card_list')->one(array('option_card_id' => $info['id'], 'option_user' => $user));
  29. if (!$one) {
  30. $id = Dever::queue('lock');
  31. if ($id) {
  32. $card = Dever::db('youzan/card_list')->one($id);
  33. } /*else {
  34. $list = Dever::db('youzan/card_list')->getAll(array('option_card_id' => $info['id'], 'option_status' => 1));
  35. $key = array_rand($list);
  36. if (isset($list[$key]) && $list[$key]) {
  37. $card = $list[$key];
  38. }
  39. }
  40. */
  41. if (isset($card) && $card) {
  42. Dever::db('youzan/card_list')->update(array('where_id' => $card['id'], 'user' => $user, 'status' => 2));
  43. # 检查info,有无msg_key,有则发送模板消息
  44. if ($msg_project_id && $info['msg_key'] && $info['page'] && $info['msg_content']) {
  45. if ($msg_page) {
  46. $info['msg_page'] = $msg_page;
  47. }
  48. $send = Dever::load('applet/msg')->getData($info['msg_content']);
  49. Dever::load('applet/msg')->sendOne($info['msg_key'], $msg_project_id, $user, $info['msg_page'], $send);
  50. }
  51. return $card;
  52. }
  53. }
  54. }
  55. Dever::alert('error');
  56. }
  57. public function create()
  58. {
  59. $id = Dever::input('id');
  60. $info = Dever::db('youzan/card')->one($id);
  61. if ($info) {
  62. Core::run($info['project_id'], 'card_create', 'card.createStart', 'card.createEnd', 'youzan', $id, false);
  63. }
  64. Dever::alert('提交成功,请到优惠券列表中查看');
  65. return;
  66. }
  67. public function createStart($id)
  68. {
  69. $info = Dever::db('youzan/card')->one($id);
  70. if ($info) {
  71. $info['title'] = $info['name'];
  72. if ($info['at_least'] > 0) {
  73. $info['is_at_least'] = 1;
  74. } else {
  75. $info['is_at_least'] = 0;
  76. }
  77. $info['can_give_friend'] = 0;
  78. $info['date_type'] = 1;
  79. $info['expire_notice'] = $info['expire_notice'] - 1;
  80. $info['fixed_begin_term'] = 0;
  81. $info['fixed_term'] = 0;
  82. if (!$info['discount']) {
  83. $info['discount'] = 0;
  84. }
  85. if ($info['value_random_to']) {
  86. $info['is_random'] = 1;
  87. } else {
  88. $info['is_random'] = 0;
  89. }
  90. if ($info['need_user_level'] <= 0) {
  91. $info['need_user_level'] = 0;
  92. }
  93. if ($info['quota'] <= 0) {
  94. $info['quota'] = 0;
  95. }
  96. if ($info['specify_item_ids']) {
  97. $info['range_type'] = 'PART';
  98. } else {
  99. $info['range_type'] = 'ALL';
  100. }
  101. $info['preferential_type'] = 0;
  102. $info['is_sync_weixin'] = $info['is_sync_weixin'] - 1;
  103. $info['is_share'] = $info['is_share'] - 1;
  104. $info['is_forbid_preference'] = $info['is_forbid_preference'] - 1;
  105. $info['at_least'] = $info['at_least'];
  106. $info['start_at'] = date('Y-m-d H:i:s', $info['sdate']);
  107. $info['end_at'] = date('Y-m-d H:i:s', $info['edate']);
  108. if ($info['create_num'] > 1) {
  109. $info['run_num'] = $info['create_num'];
  110. }
  111. return $info;
  112. } else {
  113. die;
  114. }
  115. }
  116. public function createEnd($project_id, $data, $id = false)
  117. {
  118. $info = Dever::db('youzan/card')->one($id);
  119. if ($info && isset($data['response']['promocard']['group_id'])) {
  120. $update['card_id'] = $info['id'];
  121. $update['project_id'] = $info['project_id'];
  122. $update['user'] = '';
  123. $update['status'] = 1;
  124. $update['group_id'] = $data['response']['promocard']['group_id'];
  125. $update['code_url'] = $data['response']['promocard']['fetch_url'];
  126. $update['result'] = json_encode($data);
  127. $id = Dever::db('youzan/card_list')->insert($update);
  128. Dever::queue('lock', $id);
  129. }
  130. }
  131. /*
  132. # 发送优惠券
  133. public function send()
  134. {
  135. $key = Dever::input('key');
  136. $project_id = Dever::input('project_id');
  137. $touser = Dever::input('touser');
  138. return $this->sendOne($key, $project_id, $touser);
  139. }
  140. # 发送优惠券
  141. public function sendOne($key, $project_id, $touser, $state = true)
  142. {
  143. if ($project_id > 0 && $key && $touser) {
  144. $info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
  145. if ($info) {
  146. Dever::setInput('mobile', $touser);
  147. Dever::load('youzan/user.reg');
  148. $update = array();
  149. $update['project_id'] = $info['project_id'];
  150. $update['card_id'] = $info['id'];
  151. $update['touser'] = $touser;
  152. $where = array();
  153. $where['option_card_id'] = $info['id'];
  154. //$where['option_project_id'] = $info['project_id'];
  155. $where['option_touser'] = $update['touser'];
  156. $id = Dever::upinto('youzan/card_log', $where, $update);
  157. return Core::run($info['project_id'], 'card', 'card.sendAction', 'card.sendLog', 'youzan', $id, $state);
  158. }
  159. }
  160. return false;
  161. }
  162. public function sendAction($id)
  163. {
  164. $info = Dever::db('youzan/card_log')->one($id);
  165. if ($info) {
  166. $msg = Dever::db('youzan/card')->one($info['card_id']);
  167. $info['mobile'] = $info['touser'];
  168. $info['coupon_group_id'] = $msg['card_id'];
  169. return $info;
  170. } else {
  171. die;
  172. }
  173. }
  174. public function sendLog($project_id, $data, $id = false)
  175. {
  176. $info = Dever::db('youzan/card_log')->one($id);
  177. if ($info) {
  178. $update['where_id'] = $info['id'];
  179. $update['result'] = json_encode($data);
  180. if (!isset($data['error_response'])) {
  181. $update['status'] = 2;
  182. } else {
  183. $update['status'] = 3;
  184. }
  185. $update['num'] = $info['num'] + 1;
  186. Dever::db('youzan/card_log')->update($update);
  187. }
  188. }
  189. */
  190. }