Card.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. # 发送单条模板消息
  14. public function send()
  15. {
  16. $key = Dever::input('key');
  17. $project_id = Dever::input('project_id');
  18. $touser = Dever::input('touser');
  19. return $this->sendOne($key, $project_id, $touser);
  20. }
  21. # 发送单条模板消息
  22. public function sendOne($key, $project_id, $touser, $state = true)
  23. {
  24. if ($project_id > 0 && $key && $touser) {
  25. $info = Dever::db('youzan/card')->one(array('option_key' => $key, 'option_project_id' => $project_id));
  26. if ($info) {
  27. Dever::setInput('mobile', $touser);
  28. Dever::load('youzan/user.reg');
  29. $update = array();
  30. $update['project_id'] = $info['project_id'];
  31. $update['card_id'] = $info['id'];
  32. $update['touser'] = $touser;
  33. $where = array();
  34. $where['option_card_id'] = $info['id'];
  35. //$where['option_project_id'] = $info['project_id'];
  36. $where['option_touser'] = $update['touser'];
  37. $id = Dever::upinto('youzan/card_log', $where, $update);
  38. return Core::run($info['project_id'], 'card', 'card.sendAction', 'card.sendLog', 'youzan', $id, $state);
  39. }
  40. }
  41. return false;
  42. }
  43. public function sendAction($id)
  44. {
  45. $info = Dever::db('youzan/card_log')->one($id);
  46. if ($info) {
  47. $msg = Dever::db('youzan/card')->one($info['card_id']);
  48. $info['mobile'] = $info['touser'];
  49. $info['coupon_group_id'] = $msg['card_id'];
  50. return $info;
  51. } else {
  52. die;
  53. }
  54. }
  55. public function sendLog($project_id, $data, $id = false)
  56. {
  57. $info = Dever::db('youzan/card_log')->one($id);
  58. if ($info) {
  59. $update['where_id'] = $info['id'];
  60. $update['result'] = json_encode($data);
  61. if (!isset($data['error_response'])) {
  62. $update['status'] = 2;
  63. } else {
  64. $update['status'] = 3;
  65. }
  66. $update['num'] = $info['num'] + 1;
  67. Dever::db('youzan/card_log')->update($update);
  68. }
  69. }
  70. }