$store_id, 'type' => 1]); $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value']; if ((int)$store_id === 0 || (\Yii::$app->prod_is_dandianpu() && !$self_mini)) { $wechat_config = (object)[ 'app_id' => Option::get('one_store_wechat_appid', 0, 'saas')['value'], 'app_secret' => Option::get('one_store_wechat_secret', 0, 'saas')['value'], ]; } if (!$wechat_config) { throw new \Exception('Wechat App Is Null'); } if (empty($wechat_config->app_secret)) { return [ 'code' => 1, 'msg' => '使用三方配置信息' ]; } else { $config = [ 'app_id' => $wechat_config->app_id, 'secret' => $wechat_config->app_secret, 'response_type' => 'array' ]; } $wechat = Factory::miniProgram($config); return $wechat; } /** * @description: 获取公众号配置 * @param {*} * @return {*} */ public static function getWx($store_id = null) { $store_id = $store_id ? $store_id : get_store_id(); $wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => 2]); if (!$wechat_config) { throw new \Exception('Wechat App Is Null'); } $config = [ 'app_id' => $wechat_config->app_id, 'secret' => $wechat_config->app_secret, 'response_type' => 'array' ]; $wechat = Factory::officialAccount($config); return $wechat; } /** * @description: 拉取小程序模板ID * @param {int} $tid * @param {array} $kidList * @param {string} $sceneDesc 场景描述 * @param {string} $template_id 删除原来的模板ID * @return {*} */ public static function getTamplateId($tid, array $kidList, $sceneDesc = '', $template_id = '', $type = 'miapp') { $app = $type == 'miapp' ? self::getMini() : self::getWx(); if (isset($app['code']) && $app['code'] === 1) { $client = self::isv_function(); if (is_array($client) && $client['code'] !== 0) { return [ 'code' => 1, 'msg' => '小程序信息错误' ]; } $data = [ 'tid' => $tid, 'kidList' => $kidList, 'sceneDesc' => $sceneDesc ]; /*$data = [ 'ids' => '311,2,670', 'start' => 390, 'limit' => 30 ];*/ $res = $client->httpPostJson("wxaapi/newtmpl/addtemplate", $data); //生活服务->百货/超市/便利店 物流服务 => 查件 商家自营 => 3C数码 //生活服务->百货/超市/便利店 物流服务 => 查件 商家自营 => 办公/文具 //$res = $client->httpGet("wxaapi/newtmpl/getcategory", $data); // $res = $client->httpGet("wxaapi/newtmpl/getpubtemplatetitles", $data); if ($res['errcode'] == 0 && $template_id) { $client->httpPostJson("wxaapi/newtmpl/deltemplate", ['priTmplId' => $template_id]); } } else { $res = $app->subscribe_message->addTemplate($tid, $kidList, $sceneDesc); $res = is_array($res) ? $res : Json::decode($res); if ($res['errcode'] == 0 && $template_id) { $app->subscribe_message->deleteTemplate($template_id); } } if ($res['errcode'] == 0 || !empty($res['priTmplId'])) { return [ 'code' => 0, 'template_id' => $res['priTmplId'] ]; } else { return [ 'code' => $res['errcode'], 'msg' => $res['errmsg'] ]; } } /** * @description: 获取发送模板ID * @param {array} $sendType * @param {*} $type wxaapi公众号 miapp小程序 * @return {*} */ public static function getSendTamplateId( array $sendType, $type = 'miapp') { $self_mini = 0; if (intval(get_store_id()) > 0) { $self_mini = \app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value']; } if ((\Yii::$app->prod_is_dandianpu() && !$self_mini) || (int)get_store_id() === 0) { $setting = Notice::findOne(['store_id' => 0]); } else { $setting = Notice::findOne(['store_id' => get_store_id()]); } //$setting = Notice::findOne(['store_id' => get_store_id()]); if (!in_array($type, ['wxaapi' , 'miapp'])) { return []; } if ($setting) { $setting_arr = $setting->toArray(); $return_arr = []; foreach ($setting_arr as $k => $v) { if ($v && in_array($k, $sendType)) { $v_arr = Json::decode($v); if ($v_arr[$type]['is_open']) { $return_arr[] = $v_arr[$type]['template_id']; } } } return $return_arr; } else { return []; } } public static function sendMine2($user_id, $template_id, $page, array $data_info, $type = 'wxaapi') { return self::sendMine($user_id, $template_id, $page, $data_info, $type); } /** * @description: 发送模板消息 * @param int|string $user_id|$openid * @param int $template_id * @param string $page * @param array $data * @return {*} */ public static function sendMine($user_id, $template_id, $page, array $data_info, $type = 'miapp') { try { $user = User::findOne($user_id); if($user && ($user->wechat_open_id || $user->wechat_platform_open_id)){ $openid = $type == 'miapp' ? $user->wechat_open_id : $user->wechat_platform_open_id; }elseif(!empty($user_id) && is_string($user_id)){ $openid = $user_id; } if ($openid) { $data = [ 'template_id' => $template_id, 'touser' => $openid, 'page' => $page, ]; if ($page) { $data['page'] = $page; } $data['data'] = $data_info; $app = $type == 'miapp' ? self::getMini($user->store_id) : self::getWx($user->store_id); if (isset($app['code']) && $app['code'] === 1) { $client = self::isv_function($user->store_id); if (is_array($client)) { throw new \Exception('参数配置错误'); } $res = $client->httpPostJson("cgi-bin/message/subscribe/send", $data); } else { if($type == 'wxaapi'){ $data['page'] = \Yii::$app->request->hostInfo . '/h5/#/' . $page; $client = new BaseClient($app); //订阅消息 $res = $client->httpPostJson("cgi-bin/message/subscribe/bizsend", $data); debug_log([$res, $type, $data], __CLASS__ . '.log'); if ($res['errcode'] != 0) { //模板消息 $data['url'] = $data['page']; $res = $client->httpPostJson("cgi-bin/message/template/send", $data); debug_log([$res, $type, $data], __CLASS__ . '.log'); } // debug_log([$res, $type, $data], __CLASS__ . '.log'); }else{ $res = $app->subscribe_message->send($data); $res = is_array($res) ? $res : Json::decode($res); debug_log([$res, $type, $data], __CLASS__ . '.log'); } } if ($res['errcode'] != 0) { $form = new NoticeErr(); $form->store_id = $user->store_id ?? get_store_id(); $form->user_id = $user_id; $form->data = Json::encode($data); $form->err_msg = Json::encode($res); $form->addtime = time(); $form->type = $type == 'miapp' ? 1 : 2; $form->save(); } } } catch (\Exception $e) { \Yii::error('line->>>' . $e->getLine()); \Yii::error($e->getMessage()); \Yii::error($e); \Yii::error('模板消息发送失败'); } } /** * @description: 发送短信 * @param {*} $mobile 手机号 * @param {*} $tpl 短信模板code * @param {*} $msg 模板变量 * @param {*} $content 变量 * @return array */ public static function sendSms($mobile = null, $tpl = null,$msg = null, $content = null, $store_id = -1) { \Yii::error([$mobile, $tpl,$msg, $content, $store_id]); $sms_setting = SmsSetting::findOne(['store_id' => $store_id]); if (!$sms_setting) { $sms_setting = SmsSetting::findOne(['store_id' => 0]); if (!$sms_setting) { $sms_setting = SmsSetting::findOne(['store_id' => -1]); } } if (is_array($msg) && is_array($content)) { foreach ($msg as $index => $item) { $content_sms[$item] = $content[$index]; } } else { $content_sms[$msg] = $content; } /** * @var EasySms $sms */ \Yii::error([$sms_setting]); $sms = Sms::initSms($sms_setting); try { $data = [ 'content' => $content, 'template' => $tpl ]; if ($msg) { if (is_array($msg) && is_array($content)) { foreach ($msg as $index => $item) { $data['data'][$item] = $content[$index]; } } else { $data['data'] = [ $msg => $content ]; } } $result = $sms->send($mobile, $data); \Yii::error([$result, $mobile, $data]); if ($result['aliyun']['status'] != 'success') { $result_twice = $sms->send($mobile, $data); if ($result_twice['aliyun']['status'] != 'success') { $form = new NoticeErr(); $form->store_id = $store_id; $form->user_id = 0; $form->data = Json::encode($data); $form->err_msg = Json::encode($result_twice['aliyun']['status']); $form->type = 3; $form->addtime = time(); $form->save(); return [ 'code' => 1, 'msg' => $result_twice['aliyun']['status']['Message'] ]; } } } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } if (is_array($content_sms)) { foreach ($content_sms as $k => $v) { $content_sms[$k] = strval($v); } $content_sms = json_encode($content_sms, JSON_UNESCAPED_UNICODE); } return [ 'code' => 0, 'msg' => '成功' ]; } /** * @description: 发送短信 * @param {*} $mobile 手机号 * @param {*} $tpl 短信模板code * @param {*} $msg 模板变量 * @param {*} $content 变量 * @return array */ public static function sendAdoptSms($mobile = null, $tpl = null,$msg = null, $content = null,$day1 = null,$day2 = null) { $sms_setting = SmsSetting::findOne(['store_id' => get_store_id()]); $content_sms[$msg] = $content; /** * @var EasySms $sms */ $sms = Sms::initSms($sms_setting); try { $data = [ 'content' => $content, 'template' => $tpl ]; if ($msg) { $data['data'] = [ $msg => $content, 'day1'=>$day1, 'day2'=>$day2, ]; } $result = $sms->send($mobile, $data); if ($result['aliyun']['status'] != 'success') { $result_twice = $sms->send($mobile, $data); if ($result_twice['aliyun']['status'] != 'success') { $form = new NoticeErr(); $form->store_id = get_store_id(); $form->user_id = 0; $form->data = Json::encode($data); $form->err_msg = Json::encode($result_twice['aliyun']['status']); $form->type = 3; $form->addtime = time(); $form->save(); return [ 'code' => 1, 'msg' => $result_twice['aliyun']['status']['Message'] ]; } } } catch (\Exception $e) { \Yii::error(['ALIYUN SMS SUPPLIER NOTIFY EXCEPTION <=============> ', $e->getMessage()]); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } if (is_array($content_sms)) { foreach ($content_sms as $k => $v) { $content_sms[$k] = strval($v); } $content_sms = json_encode($content_sms, JSON_UNESCAPED_UNICODE); } return [ 'code' => 0, 'msg' => '成功' ]; } public static function isv_function($store_id = null) { $store_id = $store_id ? $store_id : get_store_id(); try { $store_mini = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0, 'is_use' => 1])->orderBy('id desc')->select('appid, authorizer_refresh_token')->one(); if (empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)) { return [ 'code' => 1, 'msg' => "信息配置不完整" ]; } $authorizer_refresh_token = json_decode($store_mini->authorizer_refresh_token, true); //获取三方平台配置信息 $config = [ 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'], 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'], 'token' => Option::get("platform_token", 0, 'saas')['value'], 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value'] ]; $openPlatform = Factory::openPlatform($config); $mini_program = $openPlatform->miniProgram($store_mini->appid, $store_mini->authorizer_refresh_token); return new BaseClient($mini_program); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }