| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace addons\DepositPresale\common\helpers\wechat;
- use addons\DepositPresale\common\enums\DepositPresaleEnum;
- use common\models\user\UserExtra;
- use common\models\user\UserInfo;
- use EasyWeChat\Factory;
- use common\models\common\MallSetting;
- use Yii;
- class WechatTemplate
- {
- private $wechat = null;
- private $mall_id;
- private $openid;
- private $notice_wechat; //['order_create'=>['tem_key' => 'OPENTM401424715', 'name' => '订单生成通知', 'tempid'=>'Fpjmfna8511']];
- const PLATFORM_MP_MX = 'mp-wx'; //小程序
- const PLATFORM_WECHAT = 'wechat'; //公众号
- const ERRCODE_INVALID_TEMPLATE_ID = 40037; //微信公众号没有该模板id
- const ORDER_CREATE = 'order_create'; //订单创建
- const ORDER_PAY_SUCCESS = 'order_pay_success'; //订单支付成功
- const ORDER_CANCEL = 'order_cancel'; //订单取消通知
- const ORDER_SENT = 'order_sent'; //订单发货提现
- const ORDER_CONFIRMED = 'order_confirmed'; //订单确认收货
- const GROUP_BY_SUCCESS = 'group_buy_success'; //拼团成功通知
- const GROUP_BY_FAILED = 'group_buy_failed'; //拼团失败通知
- const BALANCE_CHANE = 'balance_change'; //账户余额通知
- const INCOME_CHANGE = 'income_change'; //账户余额通知
- const SCORE_CHANGE = 'score_change'; //积分交易提醒
- const ORDER_REFUND_AGREE = 'order_refund_agree'; //退货申请通过提醒
- const ORDER_REFUND_REFUSE = 'order_refund_refuse'; //售后申请审核通知
- const ORDER_REFUND_MONEY = 'order_refund_money'; //退款成功通知
- const USER_REGISTER = 'userRegister'; //下线加入通知
- const UP_LEVEL = 'upLevel'; //会员等级变更通知
- const E_COUPON_SEND = 'e_coupon_send'; //电子券通知
- const ACHIEVEMENT_GRANT_PUSH = 'achievement_grant_push'; //定期业绩通知
- const ORDER_DEPOSIT = 'order_deposit'; //订单支付定金后通知
- const ORDER_FINAL_PAYMENT = 'order_final_payment'; //订单支付尾款后通知
- public function __construct($mall_id)
- {
- try {
- $this->mall_id = $mall_id;
- $config = Yii::$app->services->setting->addons->get($this->mall_id, DepositPresaleEnum::ADDONS_NAME, 'basic.notice_wechat');
- if (empty($config)) {
- $this->notice_wechat = false;
- return false;
- }
- if (is_string($config)) {
- $data = json_decode($config, true);
- } else {
- if(is_array($config)) {
- $data = $config;
- }else{
- $this->notice_wechat = false;
- return false;
- }
-
- }
- if (empty($data['is_logging'])) {
- $this->notice_wechat = false;
- return false;
- }
- $template_message = $data['template_message']['value'];
- if (empty($template_message)) {
- $this->notice_wechat = false;
- return false;
- }
- $data = array_merge($data, $template_message);
- $this->notice_wechat = $data;
- //获取accsee_token app_id等信息
- $wechat = MallSetting::getConfByGroup($this->mall_id, 'wechat', true);
- if (empty($wechat)) {
- $this->notice_wechat = false;
- } else {
- unset($wechat['name']);
- unset($wechat['qrcode']);
- $wechat_config = [];
- foreach ($wechat as $name => $setting) {
- $wechat_config[$name] = $setting['value'];
- }
- $this->wechat = Factory::officialAccount($wechat_config);
- }
- } catch (\Exception $e) {
- Yii::error('插件DepositPresale:微信模板配置出现问题--'.$e->getMessage);
- }
- }
- /**
- * 获取商城是否打开推送
- */
- public function is_logging()
- {
- if ($this->wechat === null) return false;
- return empty($this->notice_wechat) ? false : ($this->notice_wechat['is_logging'] == 1 ? true : false);
- }
- /**
- * 用户是否开启推送
- */
- public function is_notice($user_id)
- {
- $user_extra = UserExtra::find()->where(['user_id' => $user_id])->one();
- return empty($user_extra) ? false : ($user_extra['is_wechat_notice'] == 1 ? true : false);
- }
- /**
- * 用户是否公众号用户
- */
- public function is_platform_wechat($user_id)
- {
- $user_info = UserInfo::find()->where(['status' => 1, 'user_id' => $user_id, 'platform' => self::PLATFORM_WECHAT])->one();
- return !empty($user_info) && $this->openid = $user_info->openid;
- }
- /**
- * 发送模板消息
- * @param $user_id 用户ID
- * @param $template_key 模板key
- * @param $url 跳转链接
- * @param $data 发生的数据
- * @param $platform 平台
- */
- public function sendTemplate($user_id, $template_key, $url, $data, $platform = "wechat", $miniapp_page = null)
- {
- if ($this->wechat == null) {
- return;
- }
- try {
- $template_id = $this->checkTemplate($template_key);
- if ($template_id == false) {
- return;
- }
- //公众号
- if ($platform == self::PLATFORM_WECHAT) {
- $result = $this->sendWechat($this->openid, $template_id, $url, $data);
- }
- //小程序
- if ($platform == self::PLATFORM_MP_MX) {
- $result = $this->sendMiniApp($this->openid, $template_id, $url, $data, $miniapp_page);
- }
- //微信公众号数据没有该模板,删除本地数据库中的数据
- if ($result['errcode'] != 0) {
- if ($result['errcode'] == self::ERRCODE_INVALID_TEMPLATE_ID) {
- $this->wechat->template_message->deletePrivateTemplate($template_id);
- }
- \Yii::error('发送模板消息失败, error_code=' . $result['errcode'] . ',error_message=' . $result['errmsg']);
- }
- } catch (\Exception $e) {
- \Yii::error('发送模板消息失败,失败原因:' . $e->getMessage() . ',错误行数:' . $e->getLine());
- }
- }
- /**
- * 有模板返回模板,没有模板消息通过template_id_short获取
- * @param string $template_type 触发发送模板消息的事件类型
- * @return void
- */
- public function checkTemplate($template_type)
- {
- try {
- $template_massage = $this->notice_wechat;
- if (!isset($this->notice_wechat[$template_type])) {
- \Yii::error('发送模板消息失败,触发事件不存在');
- return false;
- }
- //获取
- if (!isset($this->notice_wechat[$template_type]['tem_key'])) {
- $data = $this->wechat->template_message->addTemplate($this->notice_wechat[$template_type]['tem_key']);
- /*
- * $data=[
- * 'errcode'=>0,
- * 'errmsg'=>"ok",
- * "template_id"=>"2O0LuGyxows93Y6D6LMKrF09P5tpfoNift3n7AjwT5o",
- * ];
- */
- if ($data['errcode'] == 0) {
- $template_massage[$template_type]['tempid'] = $data['template_id'];
- $setting = ['notice_wechat' => ['template_message' => json_encode($template_massage, JSON_UNESCAPED_UNICODE)]];
- \Yii::$app->services->setting->mall->set($this->mall_id, $setting);
- return $data['template_id'];
- } else {
- \Yii::error('发送模板消息失败,获取template_id失败,失败原因:' . $data['errmsg']);
- return false;
- }
- }
- return $this->notice_wechat[$template_type]['tem_key'];
- } catch (\Exception $e) {
- \Yii::error('发送模板消息失败,获取template_id失败,失败原因:' . $e->getMessage() . ',错误行数:' . $e->getLine());
- return false;
- }
- }
- /**
- * 跳转平台
- */
- public function getPlatForm()
- {
- return $this->notice_wechat['is_miniapp_priority'] == 1 ? 'mp-wx' : 'wechat';
- }
- /**
- * @param $touser
- * @param $template_id
- * @param $url
- * @param $data
- * [
- * 'touser' => 'owdRztxk_XOtVZ0xglXuGv_MsRbA',
- * 'template_id' => '7rTBQadzdA4qn2RSR0_6lYUJxBlR6HloEMl03AuPRK4',
- * 'url' => 'http://jxmall.com',
- * 'data' => [
- * 'productType' => '商品名称',
- * 'name' => '小米手机',
- * 'number' => 12,
- * 'expDate' => '永久有效',
- * 'remark' => '恭喜你,购买成功'
- * ],
- * ]
- *
- * @return array
- * {
- * "errcode": 0,
- * "errmsg": "ok",
- * "msgid": 1552413574407520257
- * }
- */
- public function sendWechat($touser, $template_id, $url, $data)
- {
- return $this->wechat->template_message->send([
- 'touser' => $touser,
- 'template_id' => $template_id,
- 'url' => $url,
- 'data' => $data
- ]);
- }
- /**
- * @param $touser
- * @param $template_id
- * @param $url
- * @param $data
- * [
- * 'touser' => 'user-openid',
- * 'template_id' => 'template-id',
- * 'url' => 'https://abc.org',
- * 'miniprogram' => [
- * 'appid' => 'xxxxxxx',
- * 'pagepath' => 'pages/xxx',
- * ],
- * 'data' => [
- * 'key1' => 'VALUE',
- * 'key2' => 'VALUE2',
- * ...
- * ],
- * ]
- * @param $pagepath
- */
- public function sendMiniApp($touser, $template_id, $url, $data, $pagepath)
- {
- $app_id = isset(\Yii::$app->params['wechatMiniProgramConfig']['app_id']) ? \Yii::$app->params['wechatMiniProgramConfig']['app_id'] : "";
- return $this->wechat->template_message->send([
- 'touser' => $touser,
- 'template_id' => $template_id,
- 'url' => $url,
- 'miniprogram' => [
- 'appid' => $app_id,
- 'pagepath' => $pagepath,
- ],
- 'data' => $data
- ]);
- }
- }
|