Card.php 6.3 KB

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