Card.php 6.0 KB

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