Wechat.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Service\Lib;
  3. use Dever;
  4. class Wechat
  5. {
  6. /**
  7. * 获取token
  8. *
  9. * @return mixed
  10. */
  11. public function token()
  12. {
  13. $appid = Dever::config('base')->applet['appid'];
  14. $secret = Dever::config('base')->applet['secret'];
  15. $wechat = Dever::db('service/token')->one();
  16. $update = false;
  17. if ($wechat && time() - $wechat['mdate'] >= $wechat['expires']) {
  18. $update = true;
  19. } elseif ($wechat) {
  20. return $wechat['value'];
  21. }
  22. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
  23. $result = Dever::json_decode(Dever::curl($url));
  24. if (isset($result['access_token'])) {
  25. $data['value'] = $result['access_token'];
  26. $data['expires'] = $result['expires_in'];
  27. $data['mdate'] = time();
  28. if ($update) {
  29. $data['where_id'] = $wechat['id'];
  30. Dever::db('service/token')->update($data);
  31. } else {
  32. Dever::db('service/token')->insert($data);
  33. }
  34. } else {
  35. Dever::alert($result);
  36. }
  37. return $data['value'];
  38. }
  39. /**
  40. * 发送模板消息 定时发送 提交订单后12小时发送一次,并且是没有提交问卷的情况下
  41. *
  42. * @return mixed
  43. */
  44. public function sendTemplateMsg()
  45. {
  46. $time = time();
  47. $this->order($time + 24*3600, 1);
  48. $this->order($time + 72*3600, 2);
  49. $this->order($time + 120*3600, 3);
  50. $this->order($time + 30 * 24 * 3600, 4, 'sms');
  51. $this->order($time + 180 * 24 * 3600, 5, 'sms');
  52. }
  53. private function order($time, $notice, $type = 'msg')
  54. {
  55. $where['cdate'] = $time;
  56. $where['notice'] = $notice - 1;
  57. $order = Dever::db('service/order')->getDataByTime($where);
  58. if ($order) {
  59. foreach ($order as $k => $v) {
  60. Dever::db('service/order')->update(array('where_id' => $v['id'], 'notice' => $notice));
  61. if ($type == 'msg') {
  62. $data = array
  63. (
  64. 'keyword1' => array
  65. (
  66. 'value' => $v['order_id'],
  67. ),
  68. 'keyword2' => array
  69. (
  70. 'value' => date('Y-m-d H:i:s', $v['cdate']),
  71. ),
  72. 'keyword3' => array
  73. (
  74. 'value' => $v['name'],
  75. ),
  76. 'keyword4' => array
  77. (
  78. 'value' => '【家芝】您的软装设计服务订单:'.$v['name'].',尚未提交问卷,请微信搜索“家芝”小程序-我的服务,完成问卷后才能为您设计。',
  79. ),
  80. );
  81. $this->msg($v['product_id'], $v['uid'], $data);
  82. } else {
  83. $this->sms($v['order_id'], $v['uid']);
  84. }
  85. }
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. public function sms($order_id, $uid)
  92. {
  93. $user = Dever::db('passport/user')->one($uid);
  94. if ($user && $user['mobile']) {
  95. Dever::setInput('skin', 'notice');
  96. Dever::load('passport/applet')->send($user['mobile'], $order_id);
  97. }
  98. }
  99. public function msg($product_id, $uid, $data = array(), $template_id = false)
  100. {
  101. $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $this->token();
  102. $user = Dever::db('passport/wechat')->one(array('uid' => $uid));
  103. $form = Dever::db('service/form_id')->state(array('product_id' => $product_id, 'uid' => $uid));
  104. if ($form) {
  105. $key = array_rand($form);
  106. $form_id = $form[$key]['form_id'];
  107. Dever::db('service/form_id')->update(array('where_id' => $form[$key]['id'], 'state' => 2));
  108. $param['touser'] = $user['openid'];
  109. $param['template_id'] = $template_id ? $template_id : 'FNjRnr2BzgAD_va-CqLZtu0b9FvMJwX0rvFYCM6UzIQ';
  110. $param['page'] = '/pages/servedetail?id=' . $product_id;
  111. $param['form_id'] = $form_id;
  112. $param['data'] = $data;
  113. Dever::curl($url, $param, 'post', true);
  114. }
  115. }
  116. }