where($w)->find(); //获取会员卡信息 //获取token if ($wxconfig['type'] == 1) { $appid = $wxconfig['appid']; $secret = $wxconfig['appsercet']; $json_token = http_request('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret); $escape_token = json_decode($json_token);//转义 $token = $escape_token->{'access_token'};//2个小时换一次,一天2000次 return $token;//返回token值 } elseif ($wxconfig['type'] == 2) { $tokens = get_authorizer_access_token($merchantid); return $tokens; } } private function getVipGrede($vip) { $one = M('retail_vip_integral') ->where(array('min_integral' => array('ELT', $vip['grade_integral']), 'merchantid' => $vip['merchantid'])) ->order('min_integral desc') ->find(); if ($one) { return $one; } else { $one = array(); $one['cardname'] = '默认会员'; return $one; } } /* * ************************************2020-1-10公众号最新版推送*********************************** */ //公共数组 private function getArr($vip) { $vipArr = array( 'card' => $vip['cardnum'] ? $vip['cardnum'] : $vip['mobile_phone'], //会员卡号 'name' => $vip['name'] ? $vip['name'] : ($vip['nickname'] ? $vip['nickname'] : '用户'), // 会员名称 'mobile_phone' => $vip['mobile_phone'] ? $vip['mobile_phone'] : $vip['cardnum'], //手机号 'balance' => round($vip['balance'], 2) . '元', //当前余额 'grade_name' => $this->getVipGrede($vip)['cardname'], //会员等级 'index_time' => date('Y-m-d H:i:s', $vip['addtime']),//首次注册时间 'integral' => round($vip['integral']), 'recharge_balance' => '充值:' . round($vip['recharge_balance'], 2) . '元获得:' . round($vip['all_money'], 2) . '元', 'recharge_state' => "充值成功", 'flower_balance' => round($vip['flower_balance'], 2) . '元', 'add_time' => date('Y-m-d H:i:s', time()), 'store_name' => $vip['store_name'], 'order_number' => $vip['order_number'], 'pay_type' => $vip['pay_type'], 'start_time' => $vip['start_time'] ? date('Y-m-d H:i:s', $vip['start_time']) : '具体请到会员卡内查看', 'end_time' => $vip['end_time'] ? date('Y-m-d H:i:s', $vip['end_time']) : '具体请到会员卡内查看', 'coupon_name' => $vip['coupon_name'] ? $vip['coupon_name'] : '优惠券', 'refund_balance' => round($vip['refund_balance'], 2) . '元', 'pickup_type_name' => $vip['pickup_type_name'] ? $vip['pickup_type_name'] : '', 'user_select_time'=>$vip['user_select_time']?date('Y-m-d H:i:s',$vip['user_select_time']):'', 'user_name'=>$vip['user_name']?$vip['user_name']:'', 'user_address'=>$vip['user_address']?$vip['user_address']:'' ); return $vipArr; } //推送数据公共方法 private function WxPushData($template, $wxconfig, $vip, $vipArr) { if (!empty($template) && !empty($wxconfig)) { $template_id = $template['model_id']; if (empty($template_id)) { return ''; } $fields_json = json_decode($template['fields_json'], true); $url_title = 'http'; if ($_SERVER['HTTP'] == 'on') { $url_title .= 's'; } $default_url = $url_title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . '/Vip/Index/index?merchantid=' . $vip['merchantid']; $data = array(); $data['touser'] = $vip['openid']; $data['template_id'] = $template['model_id']; $data['url'] = $template['url'] ? $template['url'] : $default_url; $data['color'] = '#FF0000'; $data['data'] = array(); if (!empty($fields_json['first'])) { $data['data']["first"] = ['value' => $fields_json['first'], 'color' => $fields_json['first_color'] ? $fields_json['first_color'] : '#070707']; } foreach ($fields_json['keyword_list'] as $val) { $data['data'][$val['key']] = array('value' => strval($vipArr[$val['value']]), 'color' => $val['color']); } if (!empty($fields_json['remark'])) { $data['data']["remark"] = ['value' => $fields_json['remark'], 'color' => $fields_json['remark_color'] ? $fields_json['remark_color'] : '#070707']; // array_push($data['data'],"remark",array('value' => $fields_json['remark'], 'color' => $fields_json['remark_color'] ? $fields_json['remark_color'] : '#070707')); } // var_dump($data); if ($wxconfig['type'] == 2) { $options['appid'] = $wxconfig['authorizer_appid']; $access_token = $this->getToKen($vip['merchantid']); $wx = new \Util\Wx\Wechat($options); // var_dump($wx); $result = $wx->sendTemplateMessages($data, $access_token); } else { $options['appid'] = $wxconfig['appid']; $options['appsecret'] = $wxconfig['appsecret']; $wx = new \Util\Wx\Wechat($options); $result = $wx->sendTemplateMessage($data); } return $result; } } // 最新版注册会员模板2020-1-10 public function membershipsuccess($vipid) { $MerchantTemplateModel = D('MerchantTemplate'); $vip = D('Vip')->getFind(array('id' => $vipid)); $wxconfig = M('wxconfig')->where(array('merchantid' => $vip['merchantid']))->find(); $template = $MerchantTemplateModel->getFind(array('merchant_id' => $vip['merchantid'], 'type' => 1, 'status' => 1)); $vipArr = $this->getArr($vip); if (!empty($template) && !empty($wxconfig)) { $result = $this->WxPushData($template, $wxconfig, $vip, $vipArr); return $result; } $map['merchantid'] = $vip['merchantid']; $template = M('mark')->where($map)->find(); if (!empty($template)) { $templateid = $template['vip_mark']; $merchant = M('retail_merchant')->where(array('id' => $vip['merchantid']))->find(); $storename = $merchant['name']; $vipname = $vip['name'] ? $vip['name'] : '用户'; $cardnum = $vip['cardnum']; $openid = $vip['openid']; if ($templateid && $wxconfig) { // 存在才可以发送模板消息 $str = '亲爱的' . $vipname . ',您的会员卡办理成功'; $data = array(); $data['touser'] = $openid; $data['template_id'] = trim($templateid); $data['topcolor'] = "#00FF00"; $title = 'http'; if ($_SERVER['HTTP'] == 'on') { $title .= 's'; } $data['url'] = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . '/Vip/Index/index?merchantid=' . $vip['merchantid']; $data['data'] = array( 'first' => array('value' => $str), 'keyword1' => array('value' => '储值卡'), 'keyword2' => array('value' => $cardnum), 'remark' => array('value' => $storename . ':感谢您的使用。') ); if ($wxconfig['type'] == 2) { $options['appid'] = $wxconfig['appid']; $access_token = $this->getToKen($vip['merchantid']); $wx = new \Util\Wx\Wechat($options); $re = $wx->sendTemplateMessages($data, $access_token); } else { $options['appid'] = $wxconfig['appid']; $options['appsecret'] = $wxconfig['appsecret']; $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessage($data); } } } } // 充值成功推送通知 $storeid,$vid,$money,$allmoney,$paytype 2020-1-10 public function rechargesuccess($storeid, $vipid, $money, $allmoney, $paytype) { $MerchantTemplateModel = D('MerchantTemplate'); $vip = D('Vip')->getFind(array('id' => $vipid)); $wxconfig = M('wxconfig')->where(array('merchantid' => $vip['merchantid']))->find(); $template = $MerchantTemplateModel->getFind(array('merchant_id' => $vip['merchantid'], 'type' => 2, 'status' => 1)); $store = D('Store')->getFind(array('id' => $storeid)); $vip['recharge_balance'] = $money; $vip['all_money'] = $allmoney; $vip['pay_type'] = $paytype; $vipArr = $this->getArr($vip); if (!empty($template) && !empty($wxconfig)) { $result = $this->WxPushData($template, $wxconfig, $vip, $vipArr); return $result; } $w['merchantid'] = $vip['merchantid']; $template = M('mark')->where($w)->find(); $templateid = $template['recharge_mark'];// $templateid ='bKbLeWL9s6iY5DEW6-NdT5Gf5I1vg-RoErEYPTBLReA';// 模板id $storename = $store['storetag']; $cardnum = $vip['cardnum']; $openid = $vip['openid'];// $openid = 'owUCP1Edg8nBJgIREvQzrg8Dv8ts';// 测试用 $vipname = $vip['name'] ? $vip['name'] : $vip['nickname'] ? $vip['nickname'] : '用户'; $result['temp'] = $template; $result['con'] = $wxconfig; if ($templateid && $wxconfig) { // 存在才可以发送模板消息 $str = '亲爱的' . $vipname . ',您已成功进行会员卡充值'; $data = array(); $data['touser'] = $openid; $data['template_id'] = trim($templateid); $data['topcolor'] = "#00FF00"; // $data['color'] = "#FF0000"; $title = 'http'; if ($_SERVER['HTTP'] == 'on') { $title .= 's'; } $data['url'] = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . '/Vip/Index/index?merchantid=' . $vip['merchantid']; $data['data'] = array( 'first' => array('value' => $str), 'accountType' => array('value' => '会员卡'), 'account' => array('value' => $cardnum), 'amount' => array('value' => $allmoney . '元'), 'result' => array('value' => '充值成功'), 'remark' => array('value' => $storename . '提示:充值消费' . $money . '元') ); if ($wxconfig['type'] == 2) { $options['appid'] = $wxconfig['appid']; $access_token = $this->getToKen($vip['merchantid']); $wx = new \Util\Wx\Wechat($options); $re = $wx->sendTemplateMessages($data, $access_token); } else { $options['appid'] = $wxconfig['appid']; $options['appsecret'] = $wxconfig['appsecret']; $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessage($data); } } } // 新版消费成功推送 消费说明 2020-1-10 public function paysuccess($storeid, $vipid, $money, $paytype, $payinfo = array()) { $vip = D('Vip')->getFind(array('id' => $vipid)); $wxconfig = M('wxconfig')->where(array('merchantid' => $vip['merchantid']))->find(); $store = D('Store')->getFind(array('id' => $storeid)); $MerchantTemplateModel = D('MerchantTemplate'); $template = $MerchantTemplateModel->getFind(array('merchant_id' => $vip['merchantid'], 'type' => 3, 'status' => 1)); $vip['flower_balance'] = $money; $vip['store_name'] = $store['storetag']; $vip['order_number'] = $payinfo['ordernum']; $vip['pay_type'] = $paytype; $vipArr = $this->getArr($vip); if (!empty($template) && !empty($wxconfig)) { $result = $this->WxPushData($template, $wxconfig, $vip, $vipArr); return $result; } $w['merchantid'] = $vip['merchantid']; $template = M('mark')->where($w)->find(); $templateid = $template['pay_mark']; $storename = $store['storetag']; $openid = $vip['openid']; if ($templateid && $wxconfig) { // 存在才可以发送模板消息 $str = $paytype ? $paytype : '消费'; $data = array(); $data['touser'] = $openid; $data['template_id'] = trim($templateid); $data['topcolor'] = "#00FF00"; $title = 'http'; if ($_SERVER['HTTP'] == 'on') { $title .= 's'; } $data['url'] = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . '/Vip/Index/index?merchantid=' . $vip['merchantid']; $data['data'] = array( 'time' => array('value' => date("Y-m-d H:i:s", time())), 'productType' => array('value' => $str), 'name' => array('value' => strval(round($money, 2))), 'remark' => array('value' => $storename . ':谢谢惠顾,欢迎下次光临。') ); if ($wxconfig['type'] == 2) { $options['appid'] = $wxconfig['appid']; $access_token = $this->getToKen($vip['merchantid']); $wx = new \Util\Wx\Wechat($options); $res = $wx->sendTemplateMessages($data, $access_token); } else { $options['appid'] = $wxconfig['appid']; $options['appsecret'] = $wxconfig['appsecret']; $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessage($data); } } } // 优惠券发放 public function launchcoupon($vipid, $first, $remark, $Ad = array()) { $vip = D('Vip')->getFind(array('id' => $vipid)); $wxconfig = M('wxconfig')->where(array('merchantid' => $vip['merchantid']))->find(); $MerchantTemplateModel = D('MerchantTemplate'); $template = $MerchantTemplateModel->getFind(array('merchant_id' => $vip['merchantid'], 'type' => 4, 'status' => 1)); $vip['start_time'] = $Ad['starttime']; $vip['end_time'] = $Ad['endtime']; $vip['coupon_name'] = $Ad['coupon_name']; $vipArr = $this->getArr($vip); if (!empty($template) && !empty($wxconfig)) { $result = $this->WxPushData($template, $wxconfig, $vip, $vipArr); return $result; } $w['merchantid'] = $vip['merchantid']; $template = M('mark')->where($w)->find(); $templateid = $template['launchcoupon_mark']; $openid = $vip['openid']; if ($templateid && $openid && $wxconfig) { $data = array(); $data['touser'] = $openid; $data['template_id'] = trim($templateid); $data['topcolor'] = "#00FF00"; $title = 'http'; if ($_SERVER['HTTP'] == 'on') { $title .= 's'; } $data['url'] = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . '/Vip/Index/index?merchantid=' . $vip['merchantid']; $data['data'] = array( 'first' => array('value' => $first), 'keyword1' => array('value' => '赠送优惠券'), 'keyword2' => array('value' => date("Y-m-d H:i:s", time())), 'remark' => array('value' => $remark) ); if ($wxconfig['type'] == 2) { $options['appid'] = $wxconfig['appid']; $access_token = $this->getToKen($vip['merchantid']); $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessages($data, $access_token); } else { $options['appid'] = $wxconfig['appid']; $options['appsecret'] = $wxconfig['appsecret']; $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessage($data); } } } //代金券 public function payCoupon($vipid, $first, $remark, $Ad = array()) { $vip = D('Vip')->getFind(array('id' => $vipid)); $wxconfig = M('wxconfig')->where(array('merchantid' => $vip['merchantid']))->find(); $MerchantTemplateModel = D('MerchantTemplate'); $template = $MerchantTemplateModel->getFind(array('merchant_id' => $vip['merchantid'], 'type' => 8, 'status' => 1)); $vip['start_time'] = $Ad['starttime']; $vip['end_time'] = $Ad['endtime']; $vip['coupon_name'] = $Ad['coupon_name']; $vipArr = $this->getArr($vip); if (!empty($template) && !empty($wxconfig)) { $result = $this->WxPushData($template, $wxconfig, $vip, $vipArr); return $result; } $w['merchantid'] = $vip['merchantid']; $template = M('mark')->where($w)->find(); $templateid = $template['launchcoupon_mark']; $openid = $vip['openid']; if ($templateid && $openid && $wxconfig) { $data = array(); $data['touser'] = $openid; $data['template_id'] = trim($templateid); $data['topcolor'] = "#00FF00"; $title = 'http'; if ($_SERVER['HTTP'] == 'on') { $title .= 's'; } $data['url'] = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . '/Vip/Index/index?merchantid=' . $vip['merchantid']; $data['data'] = array( 'first' => array('value' => $first), 'keyword1' => array('value' => '赠代金券'), 'keyword2' => array('value' => date("Y-m-d H:i:s", time())), 'remark' => array('value' => $remark) ); if ($wxconfig['type'] == 2) { $options['appid'] = $wxconfig['appid']; $access_token = $this->getToKen($vip['merchantid']); $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessages($data, $access_token); } else { $options['appid'] = $wxconfig['appid']; $options['appsecret'] = $wxconfig['appsecret']; $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessage($data); } } } /* 退款成功推送 * @order_id 订单号 * @refund_money 退款金额 * @refund_time 退款时间 * */ public function refundsuccess($vipid, $order_id, $refund_money, $refund_time, $storeid) { $vip = D('Vip')->getFind(array('id' => $vipid)); $wxconfig = M('wxconfig')->where(array('merchantid' => $vip['merchantid']))->find(); $MerchantTemplateModel = D('MerchantTemplate'); $template = $MerchantTemplateModel->getFind(array('merchant_id' => $vip['merchantid'], 'type' => 5, 'status' => 1)); $vip['refund_balance'] = $refund_money; $vip['order_number'] = $order_id; $vip['pay_type'] = '原路返回'; $vipArr = $this->getArr($vip); if (!empty($template) && !empty($wxconfig)) { $result = $this->WxPushData($template, $wxconfig, $vip, $vipArr); return $result; } $store = D('Store')->getFind(array('id' => $storeid)); // 门店 $templatemap['merchantid'] = $vip['merchantid']; $templatemap['is_use'] = 1; $template = M('retail_template_refund')->where($templatemap)->find(); if (!empty($template)) { // 可以推送 $templateid = $template['template_id']; $merchant = M('retail_merchant')->where(array('id' => $vip['merchantid']))->find(); $storename = $store['storetag'] ? $store['storetag'] : $merchant['name']; $vipname = $vip['name'] ? $vip['name'] : ($vip['nickname'] ? $vip['nickname'] : '用户'); // 会员名称 $openid = $vip['openid']; // 会员openid } if ($templateid && $wxconfig) { // 存在才可以发送模板消息 $refund_time = date('Y-m-d H:i:s', $refund_time); $str = strtr($template['template_title'], array("{shopname}" => $storename, "{vipname}" => $vipname, "{phone}" => $vip['mobile_phone'])); $remark = strtr($template['template_remark'], array("{shopname}" => $storename, "{vipname}" => $vipname, "{phone}" => $vip['mobile_phone'])); // {shopname} {vipname} {phone} $data = array(); $data['touser'] = $openid; $data['template_id'] = trim($templateid); $data['topcolor'] = "#00FF00"; $titlecolor = $template['titlecolor'] ? $template['titlecolor'] : '#000000'; $bodycolor = $template['bodycolor'] ? $template['bodycolor'] : '#000000'; $remarkcolor = $template['remarkcolor'] ? $template['remarkcolor'] : '#000000'; $title = 'http'; if ($_SERVER['HTTP'] == 'on') { $title .= 's'; } $defaulturl = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . '/Vip/Index/index?merchantid=' . $vip['merchantid']; $data['url'] = $template['url'] ? $template['url'] : $defaulturl; switch ($template['type']) { case 1: $data['data'] = array( 'first' => array('value' => $str, 'color' => $titlecolor), 'keyword1' => array('value' => $refund_money . '元', 'color' => $bodycolor), 'keyword2' => array('value' => $refund_time, 'color' => $bodycolor), 'keyword3' => array('value' => '原路返回', 'color' => $bodycolor), 'remark' => array('value' => $remark, 'color' => $remarkcolor) ); break; case 2: $data['data'] = array( 'first' => array('value' => $str, 'color' => $titlecolor), 'keyword1' => array('value' => $order_id, 'color' => $bodycolor), 'keyword2' => array('value' => $refund_money . '元', 'color' => $bodycolor), 'keyword3' => array('value' => '退款成功', 'color' => $bodycolor), 'remark' => array('value' => $remark, 'color' => $remarkcolor) ); break; default: break; } if ($data['data']) { foreach ($data['data'] as $k => $v) { $data['data'][$k]['value'] = strval($v['value']); } } if ($wxconfig['type'] == 2) { $options['appid'] = $wxconfig['appid']; $access_token = $this->getToKen($vip['merchantid']); $wx = new \Util\Wx\Wechat($options); $re = $wx->sendTemplateMessages($data, $access_token); } else { $options['appid'] = $wxconfig['appid']; $options['appsecret'] = $wxconfig['appsecret']; $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessage($data); } } } /** * 活动推送 * @param $merchant_id * @param $openid * @param $money * @param $activity */ public function activitySocket($merchant_id, $openid, $money, $activity) { $map['merchantid'] = $merchant_id; $wxconfig = M('wxconfig')->where($map)->find(); $template = D('MerchantTemplate')->getFind(array('merchant_id' => $merchant_id, 'type' => 6, 'status' => 1)); if (!empty($template) && !empty($wxconfig)) { //新版推送 $title = 'http'; if ($_SERVER["HTTPS"] == "on") { $title = $title . 's'; } $template['url'] = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . $activity['url']; $vip = array('merchantid' => $merchant_id, 'openid' => $openid); $vipArr = $this->getArr($vip); $result = $this->WxPushData($template, $wxconfig, $vip, $vipArr); return $result; } $storename = M('retail_merchant')->where(array('id' => $merchant_id))->getField('name'); $templateid = M('mark')->where($map)->getField('pay_mark'); if ($templateid && $wxconfig) { // 存在才可以发送模板消息 $data = array(); $data['touser'] = $openid; $data['template_id'] = trim($templateid); $data['topcolor'] = "#00FF00"; $title = 'http'; if ($_SERVER["HTTPS"] == "on") { $title = $title . 's'; } $data['url'] = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/' . $activity['url']; file_put_contents('./555.txt', 'url:' . $data['url'] . ".\r\n", FILE_APPEND); $data['data'] = array( 'time' => array('value' => date("Y-m-d H:i:s", time())), 'productType' => array('value' => '消费'), 'name' => array('value' => $money), 'remark' => array('value' => $storename . ':恭喜您获得一个奖励,点击领奖!') ); if ($wxconfig['type'] == 2) { $options['appid'] = $wxconfig['appid']; $access_token = $this->getToKen($merchant_id); $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessages($data, $access_token); } else { $options['appid'] = $wxconfig['appid']; $options['appsecret'] = $wxconfig['appsecret']; $wx = new \Util\Wx\Wechat($options); $wx->sendTemplateMessage($data); } } } // 分期到账公众号消息推送 public function installAccountT($install_data, $vip_id, $merchant_id) { $MerchantTemplateModel = D('MerchantTemplate'); $vip = D('Vip')->getFind(array('id' => $vip_id)); if (empty($vip['openid'])) { return; } $wxconfig = M('wxconfig')->where(array('merchantid' => $vip['merchantid']))->find(); $template = $MerchantTemplateModel->getFind(array('merchant_id' => $vip['merchantid'], 'type' => 7, 'status' => 1)); if (!empty($template) && !empty($wxconfig)) { $result = $this->WxPushData($template, $wxconfig, $vip, $install_data['sendData']); return $result; } } /** * 公众号收银员线上订单推送 * @param $merchant_id * @param $type 9线上订单推送 * @param $data 推送的信息 * @param $openid 推送对象的openid * @return array|bool|mixed|string */ public function onlineOrderPush($merchant_id, $type, $data, $openid) { $wxconfig = M('wxconfig')->where(array('merchantid' => $merchant_id))->find(); $MerchantTemplateModel = new MerchantTemplateModel(); $template = $MerchantTemplateModel->getFind(array('merchant_id' => $merchant_id, 'type' => $type, 'status' => 1)); $vipArr = $this->getArr($data); if (!empty($template) && !empty($wxconfig)) { $info['merchantid'] = $merchant_id; $info['openid'] = $openid; $result = $this->WxPushData($template, $wxconfig, $info, $vipArr); return $result; } } }