作者 郭文星

123

@@ -263,6 +263,7 @@ class Api extends Controller @@ -263,6 +263,7 @@ class Api extends Controller
263 $rs = Db::name('order') 263 $rs = Db::name('order')
264 ->where(['order_no' => $data['out_trade_no']]) 264 ->where(['order_no' => $data['out_trade_no']])
265 ->update($order); 265 ->update($order);
  266 + SendMessage($cks['id']);
266 267
267 //生成订单日志 268 //生成订单日志
268 if ($rs) { 269 if ($rs) {
@@ -3,6 +3,10 @@ @@ -3,6 +3,10 @@
3 /** 3 /**
4 * 首字母排序A-Z(含汉字) 4 * 首字母排序A-Z(含汉字)
5 */ 5 */
  6 +
  7 +use app\admin\model\User;
  8 +use app\api\controller\v1\WxxcxPush;
  9 +
6 if (!function_exists('getFirstChar')) { 10 if (!function_exists('getFirstChar')) {
7 function getFirstChar($s) 11 function getFirstChar($s)
8 { 12 {
@@ -141,3 +145,235 @@ if (!function_exists('getOrderSn')) { @@ -141,3 +145,235 @@ if (!function_exists('getOrderSn')) {
141 return $orderid; 145 return $orderid;
142 } 146 }
143 } 147 }
  148 +
  149 + if(!function_exists('beingPushed')){
  150 + /**
  151 + * 发送模板消息
  152 + * @return void
  153 + * @throws \think\db\exception\DataNotFoundException
  154 + * @throws \think\db\exception\ModelNotFoundException
  155 + * @throws \think\exception\DbException
  156 + */
  157 + function SendMessage($order_id){
  158 + $order=\think\Db::name("order")->find($order_id);
  159 + $wxxcxpush=new WxxcxPush();
  160 + $user=new User();
  161 + $user=$user->find($order['user_id']);
  162 + $res=$wxxcxpush->Message($user['wx_xcx_openid'],$order_id);
  163 + if ($res !== false) {
  164 + return $res;
  165 + }
  166 + }
  167 + }
  168 +
  169 +
  170 +if (!function_exists('beingPushed')) {
  171 + /**
  172 + * 小程序模板推送选择类型
  173 + * @param $type @模板类型
  174 + * @param $openid @接收人ID
  175 + * @param $page @跳转页面
  176 + * @param $temp @具体提醒参数
  177 + * @throws \think\Exception
  178 + * @throws \think\exception\PDOException
  179 + */
  180 + function beingPushed($type, $openid, $page, $temp)
  181 + {
  182 + $data = [];
  183 + $data['touser'] = $openid;
  184 + $data['page'] = $page;
  185 + //模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  186 + switch ($type) {
  187 + case 1:
  188 + //待审批事项提醒(新版本的长期订阅)
  189 + $data['template_id'] = "ws6bz6-WyHHA-upU2oRzc_C5toDUexe9ZicQ-ITtImA";
  190 + $data['data'] = [
  191 + "thing10" => [
  192 + 'value' =>1,//审核状态(待处理)
  193 + ],
  194 + "thing12" => [
  195 + 'value' => 2,//申请人(测试黄超)
  196 + ],
  197 +
  198 + "thing8" => [
  199 + 'value' => 3,//原因(入州报备审核)
  200 + ]
  201 + ];
  202 + break;
  203 + case 2:
  204 + //审核结果通知(新版本的长期订阅)
  205 + $data['template_id'] = "Qt2xqfH8uQyTm1A0ReNxeybtAydAhfhi4HinnyB5f0s";
  206 + $data['data'] = [
  207 + "thing1" => [
  208 + 'value' => $temp['value1'],//提醒内容(已审核)
  209 + ],
  210 +// "name1" => [
  211 +// 'value' => $temp['value2'],//审核人
  212 +// ],
  213 + "thing4" => [
  214 + 'value' => $temp['value3'],//提示说明
  215 + ]
  216 + ];
  217 + break;
  218 + case 3:
  219 + //待审批事项提醒(老版本的一次性订阅)
  220 + $data['template_id'] = "vVOonN76AFjxgz77iG-hcWrH6HJ-vPGKyIyAK0VzPIk";
  221 + $data['data'] = [
  222 + "thing1" => [
  223 + 'value' => $temp['value1'],//待办事项
  224 + ],
  225 + "thing4" => [
  226 + 'value' => $temp['value2'],//流程状态
  227 + ],
  228 + "thing2" => [
  229 + 'value' => $temp['value3'],//申请人
  230 + ],
  231 + "time3" => [
  232 + 'value' => $temp['value4'],//申请时间
  233 + ],
  234 + "thing5" => [
  235 + 'value' => $temp['value5'],//备注描述
  236 + ]
  237 + ];
  238 + break;
  239 + case 4:
  240 + //审核结果通知(老版本的一次性订阅)
  241 + $data['template_id'] = "yJRK0DZFuEPqaWwyS0DegT0r2p5be0bm2eE59oroGME";
  242 + $data['data'] = [
  243 + "phrase2" => [
  244 + 'value' => $temp['value1'],//审核结果
  245 + ],
  246 + "name1" => [
  247 + 'value' => $temp['value2'],//审核人
  248 + ],
  249 + "date4" => [
  250 + 'value' => $temp['value3'],//审批时间
  251 + ]
  252 + ];
  253 + break;
  254 + case 5:
  255 +
  256 + //预约结果通知
  257 + $data['template_id'] = "oczyQ_SabASMQRRhDedXRGUEzCAuGh1xBw8sp2HVPvQ";
  258 + $data['data'] = [
  259 + "thing12" => [
  260 + 'value' => $temp['value1'],//备注
  261 + ],
  262 + "time10" => [
  263 + 'value' =>$temp['value2'],//日期
  264 + ],
  265 + "thing8" => [
  266 + 'value' => $temp['value3'],//就诊人
  267 + ]
  268 + ];
  269 + break;
  270 + }
  271 +
  272 + return sendSubscribeMessage($data);
  273 + }
  274 +}
  275 +
  276 +if (!function_exists('sendSubscribeMessage')) {
  277 + /**
  278 + * 小程序订阅消息推送
  279 + * @param $data
  280 + * @throws \think\Exception
  281 + * @throws \think\exception\PDOException
  282 + */
  283 + function sendSubscribeMessage($data)
  284 + {
  285 + //access_token
  286 + $access_token = getAccessToken();
  287 +
  288 + //请求url
  289 + $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token;
  290 +
  291 + //file_put_contents('notify.txt',$data.PHP_EOL,FILE_APPEND);
  292 + //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  293 + $data['miniprogram_state'] = 'formal';
  294 +
  295 + return curlPost($url, json_encode($data));
  296 + }
  297 +}
  298 +
  299 +if (!function_exists('getAccessToken')) {
  300 + /**
  301 + * 获取access_token
  302 + * @return mixed
  303 + * @throws \think\Exception
  304 + * @throws \think\exception\PDOException
  305 + */
  306 + function getAccessToken()
  307 + {
  308 + $wx_xcx_token = \think\Db::name("wechat_token")->where("type", "wx_xcx")->find();
  309 + if (empty($wx_xcx_token) || empty($wx_xcx_token['access_token']) || $wx_xcx_token['over_time'] <= time()) {
  310 + //微信小程序失效
  311 + //当前时间戳
  312 + $now_time = strtotime(date('Y-m-d H:i:s', time()));
  313 +
  314 + //获取新的access_token
  315 + $appid = config("site.wxxcx_AppID");
  316 + $secret = config("site.wxxcx_AppSecret");
  317 + $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
  318 + $res = json_decode(file_get_contents($url), true);
  319 +
  320 + $access_token = $res['access_token'];
  321 + if (!empty($access_token)) {
  322 + $time = time();
  323 + $update_data = [
  324 + "access_token" => $access_token,
  325 + "over_time" => $time + 7200,
  326 + "type" => "wx_xcx",
  327 + "updatetime" => $time
  328 + ];
  329 + if (!empty($wx_xcx_token)) {
  330 + //更新
  331 + \think\Db::name("wechat_token")->where("id", $wx_xcx_token['id'])->update($update_data);
  332 + } else {
  333 + //新增
  334 + \think\Db::name("wechat_token")->insertGetId($update_data);
  335 + }
  336 + }
  337 + } else {
  338 + $access_token = $wx_xcx_token['access_token'];
  339 + }
  340 + return $access_token;
  341 + }
  342 +}
  343 +
  344 +if (!function_exists('curlPost')) {
  345 + /**
  346 + * 发送post请求
  347 + */
  348 + function curlPost($url, $data)
  349 + {
  350 + $ch = curl_init();
  351 + $params[CURLOPT_URL] = $url; //请求url地址
  352 + $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
  353 + $params[CURLOPT_SSL_VERIFYPEER] = false;
  354 + $params[CURLOPT_SSL_VERIFYHOST] = false;
  355 + $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
  356 + $params[CURLOPT_POST] = true;
  357 + $params[CURLOPT_POSTFIELDS] = $data;
  358 + curl_setopt_array($ch, $params); //传入curl参数
  359 + $content = curl_exec($ch); //执行
  360 +// file_put_contents('notify.txt', $content . date('Y-m-d H:i:s', time()) . PHP_EOL, FILE_APPEND);
  361 + curl_close($ch); //关闭连接
  362 + return $content;
  363 + }
  364 +}
  365 +
  366 +if (!function_exists('array_callback')) {
  367 + /**
  368 + * 20190729 kevin
  369 + * 规范数据返回函数
  370 + * @param unknown $state
  371 + * @param unknown $msg
  372 + * @param unknown $data
  373 + * @return multitype:unknown
  374 + */
  375 + function array_callback($state = true, $msg = '', $data = array())
  376 + {
  377 + return array('state' => $state, 'msg' => $msg, 'data' => $data);
  378 + }
  379 +}
@@ -2,9 +2,11 @@ @@ -2,9 +2,11 @@
2 2
3 namespace app\api\controller; 3 namespace app\api\controller;
4 4
  5 +use app\admin\model\User;
5 use app\api\controller\inspection\Task; 6 use app\api\controller\inspection\Task;
6 use app\api\controller\tencentim\Openim; 7 use app\api\controller\tencentim\Openim;
7 use app\api\controller\v1\Base; 8 use app\api\controller\v1\Base;
  9 +use app\api\controller\v1\WxxcxPush;
8 use app\common\controller\Api; 10 use app\common\controller\Api;
9 use think\Db; 11 use think\Db;
10 12
@@ -297,7 +299,7 @@ class Car extends Base @@ -297,7 +299,7 @@ class Car extends Base
297 */ 299 */
298 public function switching_driver() 300 public function switching_driver()
299 { 301 {
300 - $res = Db::name("driver")->where("user_id", 10)->find(); 302 + $res = Db::name("driver")->where("user_id", $this->auth->id)->find();
301 $car = Db::name("car")->where("driver_id", $res['id'])->find(); 303 $car = Db::name("car")->where("driver_id", $res['id'])->find();
302 if ($car) { 304 if ($car) {
303 //本月收入 305 //本月收入
@@ -940,6 +942,8 @@ class Car extends Base @@ -940,6 +942,8 @@ class Car extends Base
940 $remarks = $this->request->param("remarks");//备注 942 $remarks = $this->request->param("remarks");//备注
941 $carmodel = Db::name("carmodel")->where("id", $carmodel_id)->find(); 943 $carmodel = Db::name("carmodel")->where("id", $carmodel_id)->find();
942 $specificaddress_id = $this->request->param("specificaddress_id");//区域id 944 $specificaddress_id = $this->request->param("specificaddress_id");//区域id
  945 + $driver= Db::name("driver")->where("id", $intended_driver_id)->find();
  946 + $car= Db::name("car")->where("driver_id", $driver['id'])->find();
943 $chartercar = Db::name("chartercar") 947 $chartercar = Db::name("chartercar")
944 ->where("specificaddress_id", $specificaddress_id) 948 ->where("specificaddress_id", $specificaddress_id)
945 ->where("carmodel_id", $carmodel_id) 949 ->where("carmodel_id", $carmodel_id)
@@ -1462,4 +1466,9 @@ class Car extends Base @@ -1462,4 +1466,9 @@ class Car extends Base
1462 $this->error("请求失败"); 1466 $this->error("请求失败");
1463 } 1467 }
1464 } 1468 }
  1469 +
  1470 + public function test(){
  1471 + $res=SendMessage(117);
  1472 + $this->success("123",$res);
  1473 + }
1465 } 1474 }
@@ -373,8 +373,8 @@ class WxxcxPush extends Base @@ -373,8 +373,8 @@ class WxxcxPush extends Base
373 373
374 //小程序发送一次性订阅消息 374 //小程序发送一次性订阅消息
375 public function Message($openid,$reservation_id){ 375 public function Message($openid,$reservation_id){
376 - $reservationmodel=new Reservation();  
377 - $reservation=$reservationmodel->find($reservation_id); 376 +
  377 + $reservation=Db::name("order")->find($reservation_id);
378 378
379 if(!empty($reservation)){ 379 if(!empty($reservation)){
380 // 启动事务 380 // 启动事务
@@ -383,11 +383,13 @@ class WxxcxPush extends Base @@ -383,11 +383,13 @@ class WxxcxPush extends Base
383 $createtime = time(); 383 $createtime = time();
384 //通知 384 //通知
385 $page = '/pageOne/moreRes/moreRes'; 385 $page = '/pageOne/moreRes/moreRes';
386 - $temp='ws6bz6-WyHHA-upU2oRzc_C5toDUexe9ZicQ-ITtImA'; 386 + $temp='oczyQ_SabASMQRRhDedXRGUEzCAuGh1xBw8sp2HVPvQ';
387 $insert_message_log_data = array( 387 $insert_message_log_data = array(
388 'value1' => "预约提醒", 388 'value1' => "预约提醒",
389 - 'value2' => date('Y-m-d',$reservation['appointment_time']),  
390 - 'value3' => $reservation['user_name'] 389 + 'value2' => "123123",
  390 + 'value3' => "123123123",
  391 + 'value4' => "123123123",
  392 + 'value5' => "123123123",
391 ); 393 );
392 394
393 $res = beingPushed(5, $openid, $page, $insert_message_log_data); 395 $res = beingPushed(5, $openid, $page, $insert_message_log_data);
  1 +2024-04-02 17:59:05《117》"{\"errcode\":43101,\"errmsg\":\"user refuse to accept the msg rid: 660bd6ea-622ec5d0-43cc7fb2\"}"
  2 +2024-04-02 18:00:07《117》"{\"errcode\":43101,\"errmsg\":\"user refuse to accept the msg rid: 660bd727-39dc0f13-306b3b82\"}"