Explorar el Código

小程序发模板消息

lizhen hace 2 días
padre
commit
02478627c5

+ 75 - 0
application/admin/controller/Feedback.php

@@ -3,6 +3,12 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
+use app\common\library\Wechat;
+use think\Db;
+use think\exception\DbException;
+use think\exception\PDOException;
+use think\exception\ValidateException;
+use Exception;
 
 /**
  * 智能助手反馈
@@ -70,4 +76,73 @@ class Feedback extends Backend
         return $this->view->fetch();
     }
 
+    /**
+     * 编辑
+     *
+     * @param $ids
+     * @return string
+     * @throws DbException
+     * @throws \think\Exception
+     */
+    public function edit($ids = null)
+    {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
+            $this->error(__('You have no permission'));
+        }
+        if (false === $this->request->isPost()) {
+            $this->view->assign('row', $row);
+            return $this->view->fetch();
+        }
+        $params = $this->request->post('row/a');
+        if (empty($params)) {
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $params = $this->preExcludeFields($params);
+        $result = false;
+        Db::startTrans();
+        try {
+            //是否采用模型验证
+            if ($this->modelValidate) {
+                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
+                $row->validateFailException()->validate($validate);
+            }
+            $result = $row->allowField(true)->save($params);
+
+            Db::commit();
+
+            //如果已解决,发消息
+            if($params['status'] == 2){
+
+                $config = config('wxMiniProgram');
+                $wechat = new Wechat($config['appid'],$config['secret']);
+
+                //意见反馈
+                $template_id = $config['mini_msgid_feedback'];
+                $msg_info = mb_substr($row->info,0,12) . ((mb_strlen($row->info) > 12) ? '...' : '');
+                $data = [
+                    'thing4' => ['value' => $row->category ?: '问题反馈'],
+                    'thing5' => ['value' => $msg_info],
+                    'thing6' => ['value' => '已经处理完成了'],
+                ];
+                $msg_openid = Db::name('user')->where('id',$row->user_id)->value('wxmini_openid');
+
+                $rs = $wechat->send($template_id,$msg_openid,$data);
+            }
+
+        } catch (ValidateException|PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if (false === $result) {
+            $this->error(__('No rows were updated'));
+        }
+        $this->success();
+    }
+
 }

+ 36 - 1
application/api/controller/Demo.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\common\library\RsaUtil;
+use app\common\library\Wechat;
 
 /**
  * 示例接口
@@ -41,7 +42,9 @@ class Demo extends Api
      */
     public function test()
     {
-        $this->success('success');
+        $a = '我要反馈一';
+        $b = mb_substr($a,0,12) . ((mb_strlen($a) > 12) ? '...' : '');
+        echo $b;
     }
 
     /**
@@ -62,6 +65,38 @@ class Demo extends Api
         $this->success('返回成功', ['action' => 'test2']);
     }
 
+    public function send_minimsg(){
+        $config = config('wxMiniProgram');
+        $wechat = new Wechat($config['appid'],$config['secret']);
+
+        //活动开始
+        $template_id = $config['mini_msgid_order'];
+        $data = [
+            'thing4' => ['value' => '活动名称'],
+            'thing6' => ['value' => '活动地址'],
+            'date5'  => ['value' => '2025-06-27 14:00'],
+            'thing7' => ['value' => '明天开始,不要迟到哦'],
+        ];
+
+        $rs = $wechat->send($template_id,'odWcF7sjejGL7d-0DuCmbmbcH1yA',$data);
+        dump($rs);
+
+        //意见反馈
+        $template_id = $config['mini_msgid_feedback'];
+        $data = [
+            'thing4' => ['value' => '动画视频功能'],
+            'thing5' => ['value' => '无法上传'],
+            'thing6' => ['value' => '已经处理完毕了'],
+        ];
+
+        $rs = $wechat->send($template_id,'odWcF7sjejGL7d-0DuCmbmbcH1yA',$data);
+        dump($rs);
+
+
+    }
+
+
+
     /**
      * 需要登录且需要验证有相应组的权限
      *

+ 4 - 0
application/api/controller/Feedback.php

@@ -27,6 +27,10 @@ class Feedback extends Api
         $images = input('images','','trim');
         $is_hidden = input('is_hidden',0);
 
+        if(empty($info)){
+            $this->error('请输入问题描述');
+        }
+
         $images_arr = explode(',',$images);
         if(count($images_arr) > 9){
             $this->error('最多只能传9张图片');

+ 96 - 3
application/common/library/Wechat.php

@@ -3,15 +3,13 @@
 namespace app\common\library;
 
 use fast\Http;
-use think\Cache;
 use think\Session;
 
 /**
  * 微信授权
  *
  */
-class Wechat
-{
+class Wechat{
     private $app_id = '';
     private $app_secret = '';
 
@@ -184,4 +182,99 @@ class Wechat
         }
         return $jsticket;
     }*/
+
+    //获取类目
+    public function getcategory(){
+        $access_token = $this->getPublicAccessToken();
+        $params = [];
+
+        $ret = Http::sendRequest('https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token='.$access_token, $params, 'GET');
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+
+    //获取关键词列表
+    public function getpubtemplatekeywords($tid){
+        $access_token = $this->getPublicAccessToken();
+        $params = [
+            'tid' => $tid,
+        ];
+
+        $ret = Http::sendRequest('https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token='.$access_token, $params, 'GET');
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+
+    //获取所属类目下的公共模板
+    public function getpubtemplatetitles(){
+        $access_token = $this->getPublicAccessToken();
+        $params = [
+            'ids' => 698, //类目 id
+            'start' => '' . 0,
+            'limit' => 30,
+        ];
+
+        $ret = Http::sendRequest('https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token='.$access_token, $params, 'GET');
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+
+    //添加模板
+    public function addtemplate(){
+        $access_token = $this->getPublicAccessToken();
+        $params = [];
+
+        $params = [
+            'tid' => 515, //类目 id
+            'kidList' => '' . 0,
+            'sceneDesc' => 30,
+        ];
+
+        $ret = Http::sendRequest('https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$access_token, $params, 'GET');
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+
+    //发送订阅消息
+    //https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html
+    public function send($template_id,$openid,$data){
+        $access_token = $this->getPublicAccessToken();
+        $params = [
+            'template_id' => $template_id,
+            'page'        => 'pages/index/index',
+            'touser'      => $openid,
+            'data'        => $data, //模板内容,格式形如{ "phrase3": { "value": "审核通过" }, "name1": { "value": "订阅" }, "date2": { "value": "2019-12-25 09:42" } }
+            'miniprogram_state' => 'trial', //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
+            'lang'              => 'zh_CN',
+        ];
+
+        $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token, json_encode($params), 'POST');
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+
+
+
+
+
 }

+ 2 - 2
gitpullonline.sh

@@ -1,4 +1,4 @@
 sudo -uroot git reset --hard;
 sudo -uroot git pull;
-sudo -uroot chown -R www:www /www/wwwroot/shequ.huxiukeji.cn;
-sudo -uroot chmod -R 777 *;
+sudo -uroot chown -R www:www /home/wwwroot/shequ;
+sudo -uroot chmod -R 755 *;