123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace Service\Lib;
- use Dever;
- class Wechat
- {
- /**
- * 获取token
- *
- * @return mixed
- */
- public function token()
- {
- $appid = Dever::config('base')->applet['appid'];
- $secret = Dever::config('base')->applet['secret'];
- $wechat = Dever::db('service/token')->one();
- $update = false;
- if ($wechat && time() - $wechat['mdate'] >= $wechat['expires']) {
- $update = true;
- } elseif ($wechat) {
- return $wechat['value'];
- }
- $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
- $result = Dever::json_decode(Dever::curl($url));
- if (isset($result['access_token'])) {
- $data['value'] = $result['access_token'];
- $data['expires'] = $result['expires_in'];
- $data['mdate'] = time();
- if ($update) {
- $data['where_id'] = $wechat['id'];
- Dever::db('service/token')->update($data);
- } else {
- Dever::db('service/token')->insert($data);
- }
- } else {
- Dever::alert($result);
- }
-
- return $data['value'];
- }
- /**
- * 发送模板消息 定时发送 提交订单后12小时发送一次,并且是没有提交问卷的情况下
- *
- * @return mixed
- */
- public function sendTemplateMsg()
- {
- $time = time();
- $this->order($time + 24*3600, 1);
- $this->order($time + 72*3600, 2);
- $this->order($time + 120*3600, 3);
- $this->order($time + 30 * 24 * 3600, 4, 'sms');
- $this->order($time + 180 * 24 * 3600, 5, 'sms');
- }
- private function order($time, $notice, $type = 'msg')
- {
- $where['cdate'] = $time;
- $where['notice'] = $notice - 1;
- $order = Dever::db('service/order')->getDataByTime($where);
- if ($order) {
- foreach ($order as $k => $v) {
- Dever::db('service/order')->update(array('where_id' => $v['id'], 'notice' => $notice));
- if ($type == 'msg') {
- $data = array
- (
- 'keyword1' => array
- (
- 'value' => $v['order_id'],
- ),
- 'keyword2' => array
- (
- 'value' => date('Y-m-d H:i:s', $v['cdate']),
- ),
- 'keyword3' => array
- (
- 'value' => $v['name'],
- ),
- 'keyword4' => array
- (
- 'value' => '【家芝】您的软装设计服务订单:'.$v['name'].',尚未提交问卷,请微信搜索“家芝”小程序-我的服务,完成问卷后才能为您设计。',
- ),
- );
- $this->msg($v['product_id'], $v['uid'], $data);
- } else {
- $this->sms($v['order_id'], $v['uid']);
- }
-
- }
- return true;
- } else {
- return false;
- }
- }
- public function sms($order_id, $uid)
- {
- $user = Dever::db('passport/user')->one($uid);
- if ($user && $user['mobile']) {
- Dever::setInput('skin', 'notice');
- Dever::load('passport/applet')->send($user['mobile'], $order_id);
- }
- }
- public function msg($product_id, $uid, $data = array(), $template_id = false)
- {
- $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $this->token();
- $user = Dever::db('passport/wechat')->one(array('uid' => $uid));
- $form = Dever::db('service/form_id')->state(array('product_id' => $product_id, 'uid' => $uid));
- if ($form) {
- $key = array_rand($form);
- $form_id = $form[$key]['form_id'];
- Dever::db('service/form_id')->update(array('where_id' => $form[$key]['id'], 'state' => 2));
- $param['touser'] = $user['openid'];
- $param['template_id'] = $template_id ? $template_id : 'FNjRnr2BzgAD_va-CqLZtu0b9FvMJwX0rvFYCM6UzIQ';
- $param['page'] = '/pages/servedetail?id=' . $product_id;
- $param['form_id'] = $form_id;
- $param['data'] = $data;
- Dever::curl($url, $param, 'post', true);
- }
- }
- }
|