* @version : HuoMp 1.0 */ namespace huoMpMsg\controller; use huo\controller\common\Base; use huo\controller\member\MemCache; use huo\controller\wap\Option; use huo\model\member\MemoauthModel; use huo\model\user\UserModel; use huolib\constant\OptionConst; use huomp\controller\member\Member; use huomp\model\weixin\MpConfModel; use huomp\model\weixin\OaMpModel; use huoMpMsg\status\OfficialAccountStatus; use think\Log; class OaOut extends Base { const GOLD_NAME = '积分'; const RP_NAME = '红包'; const GOLD_UNIT = '积分'; const RP_UNIT = '元'; private $tpl_id = 'QnxQn_ylFFKbhRNG84hdtU9Lp9gbUXsU1WB1KeNXaJw'; private $settle_tpl_id = 'Qp_aC8WXKSAw_ZlTRkx6vFespGXK5GRGnOWnpcdf6Ug'; private $wx_app_id = 'wx34b5f6e35fae9903'; public function __construct($wx_app_id = '') { if (!empty($wx_app_id)) { $this->wx_app_id = $wx_app_id; $_conf_id = (new MpConfModel())->getIdByMpId($wx_app_id); $this->setTplData($_conf_id); } } protected function setTplData($conf_id) { $_setting_name = OptionConst::WEIXIN_TEMPLATE_MSG.$conf_id; $_m = new Option(); $_item = $_m->getOptionData($_setting_name, 1, true); if (!empty($_item['option_value'])) { $_option_value = json_decode($_item['option_value'], true); unset($_item); if (!empty($_option_value['tpl_id'])) { $this->tpl_id = $_option_value['tpl_id']; } if (!empty($_option_value['settle_tpl_id'])) { $this->settle_tpl_id = $_option_value['settle_tpl_id']; } } } /** * 发送模板金币变动信息 * * @param int $mem_id 玩家ID * @param int $app_id 应用ID * @param int $change_gold 改变的金额 * @param int $time 改变的时间戳 * * @return array */ public function sendGoldChangeMsg($mem_id, $app_id, $change_gold, $time = 0) { $_type_name = self::GOLD_NAME; $_oa_id = (new OaMpModel())->getOaByApp($app_id); $_gold = MemCache::ins()->getMyItg($mem_id); return $this->sendChangeMsg($mem_id, $_oa_id, $change_gold, $_gold, $time, $_type_name); } /** * 发送模板红包变动信息 * * @param int $mem_id 玩家ID * @param int $app_id 应用ID * @param int $change_money 改变的金额 * @param int $time 改变的时间戳 * * @return array */ public function sendMoneyChangeMsg($mem_id, $app_id, $change_money, $time = 0) { $_type_name = self::RP_NAME; $_oa_id = (new OaMpModel())->getOaByApp($app_id); $_balance = (new Member())->getBalance($mem_id); return $this->sendChangeMsg($mem_id, $_oa_id, $change_money, $_balance, $time, $_type_name); } /** * 发送模板红包变动信息 * * @param int $mem_id 玩家ID * @param int $wx_app_id 公众号应用ID * @param int $change_money 改变的金额 * @param int $time 改变的时间戳 * * @return array */ public function sendWxMoneyChangeMsg($mem_id, $wx_app_id, $change_money, $time = 0) { $_type_name = self::RP_NAME; $_oa_id = $wx_app_id; $_balance = (new Member())->getBalance($mem_id); return $this->sendChangeMsg($mem_id, $_oa_id, $change_money, $_balance, $time, $_type_name); } /** * 发送模板红包变动信息 * * @param int $agent_id 玩家渠道ID * @param int $change_money 改变的金额 * @param int $time 改变的时间戳 * * @return array */ public function sendAdminMoneyChangeMsg($agent_id, $change_money, $time = 0) { $_type_name = self::RP_NAME; $_oa_id = $this->wx_app_id; $_mem_id = (new UserModel())->getMemIdById($agent_id); $_balance = (new Member())->getBalance($_mem_id); return $this->sendChangeMsg($_mem_id, $_oa_id, $change_money, $_balance, $time, $_type_name); } /** * 发送模板帐户资金变动提醒信息 * * @param int $mem_id 玩家ID * @param string $oa_id 公众号ID * @param double $change_money 改变金额 * @param double $balance 余额 * @param integer $time 修改时间 * @param string $type_name 名称 * * @return array */ public function sendChangeMsg($mem_id, $oa_id, $change_money, $balance, $time, $type_name = '') { $_wx_app_id = $oa_id; $_mc_model = new MpConfModel(); $_conf_id = $_mc_model->getIdByMpId($_wx_app_id); $this->setTplData($_conf_id); $_open_id = (new MemoauthModel())->getOpenIdByMpMemId($_conf_id, $mem_id); if (empty($_open_id)) { $_code = OfficialAccountStatus::OPENID_IS_NULL; Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&conf_id=".$_conf_id."&code=".$_code.'&msg=' .OfficialAccountStatus::getMsg($_code) ."&mem_id=".$mem_id.'&oa_id='.$oa_id.'&change_money='.$change_money.'&balance='.$balance.'&type_name=' .$type_name, LOG::WECHAT, true ); return $this->huoError($_code, OfficialAccountStatus::getMsg($_code)); } $_template_id = $this->getTplData($_wx_app_id, ''); $_wx_app_secret = $_mc_model->getSecretByMpId($_wx_app_id); $_time = !empty($time) ? $time : time(); if (self::GOLD_NAME == $type_name) { $change_money = $change_money.self::GOLD_UNIT; $balance = $balance.self::GOLD_UNIT; } elseif (self::RP_NAME == $type_name) { $change_money = '¥'.$change_money.self::RP_UNIT; $balance = '¥'.$balance.self::RP_UNIT; } $_data = [ 'first' => [ 'value' => '您好,您有新的余额变动', 'color' => '#173177' ], 'date' => [ 'value' => date('Y-m-d H:i:s', $_time), 'color' => '#173177' ], 'adCharge' => [ 'value' => $change_money, 'color' => '#173177' ], 'type' => [ 'value' => $type_name, 'color' => '#173177' ], 'cashBalance' => [ 'value' => $balance, 'color' => '#173177' ], 'remark' => [ 'value' => '点击"详情"立即查阅您的帐户财务记录。', 'color' => '#173177' ], ]; $_url = $this->getUrl($_wx_app_id); $_rs = (new OfficialAccountApi())->sendTemplateMsg( $_wx_app_id, $_wx_app_secret, $_open_id, $_template_id, $_url, [], $_data ); return $this->huoReturn($_rs); } /** * 发送模板红包变动信息 * * @param int $agent_id 玩家渠道ID * @param int $change_money 改变的金额 * @param int $time 改变的时间戳 * * @return array */ public function sendAdminSettleMsg($agent_id, $change_money, $time = 0) { $_type_name = self::RP_NAME; $_oa_id = $this->wx_app_id; $_mem_id = (new UserModel())->getMemIdById($agent_id); return $this->sendSettleMsg($_mem_id, $_oa_id, $change_money, $time, $_type_name); } /** * 发送模板提现到账提醒信息 * * @param int $mem_id 玩家ID * @param string $oa_id 公众号ID * @param double $change_money 改变金额 * @param integer $time 修改时间 * @param string $type_name 名称 * * @return array */ public function sendSettleMsg($mem_id, $oa_id, $change_money, $time, $type_name = self::RP_NAME) { $_wx_app_id = $oa_id; $_mc_model = new MpConfModel(); $_conf_id = $_mc_model->getIdByMpId($_wx_app_id); $_oauth_data = (new MemoauthModel())->getDataByMpMemId($_conf_id, $mem_id); $_open_id = $_oauth_data['openid']; $_nickname = $_oauth_data['nickname']; if (empty($_open_id)) { $_code = OfficialAccountStatus::OPENID_IS_NULL; Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&msg='.OfficialAccountStatus::getMsg($_code) ."&mem_id=".$mem_id.'&oa_id='.$oa_id.'&change_money='.$change_money.'&type_name' .$type_name, LOG::ERROR ); return $this->huoError($_code, OfficialAccountStatus::getMsg($_code)); } $_template_id = $this->getSettleTplData($_wx_app_id, ''); $_wx_app_secret = $_mc_model->getSecretByMpId($_wx_app_id); $_time = !empty($time) ? $time : time(); if (self::GOLD_NAME == $type_name) { $change_money = $change_money.self::GOLD_UNIT; } elseif (self::RP_NAME == $type_name) { $change_money = $change_money.self::RP_UNIT; } $_data = [ 'first' => [ 'value' => '尊敬的'.$_nickname.',您提现已处理成功。', 'color' => '#173177' ], 'keyword1' => [ 'value' => date('Y-m-d H:i:s', $_time), 'color' => '#173177' ], 'keyword2' => [ 'value' => '¥'.$change_money, 'color' => '#173177' ], 'remark' => [ 'value' => '请留意提现卡号余额变动。', 'color' => '#173177' ], ]; $_url = $this->getUrl($_wx_app_id); $_rs = (new OfficialAccountApi())->sendTemplateMsg( $_wx_app_id, $_wx_app_secret, $_open_id, $_template_id, $_url, [], $_data ); return $this->huoReturn($_rs); } /** * @param string $mp_id * @param string $type * * @return string */ public function getTplData($mp_id, $type = '') { return $this->tpl_id; } /** * @param string $mp_id * @param string $type * * @return string */ public function getSettleTplData($mp_id, $type = '') { return $this->settle_tpl_id; } /** * @param string $mp_id * * @return string */ public function getUrl($mp_id) { return MPAPISITE.'/index/income/list/'.$mp_id; } public function getJsapiTicket($oa_id) { $_wx_app_id = $oa_id; $_mc_model = new MpConfModel(); $_mp_data = $_mc_model->getDataByMpId($_wx_app_id); $_access_token = Common::getAccessToken($_mp_data['mp_id'], $_mp_data['app_secret']); if (empty($_access_token)) { $_code = OfficialAccountStatus::ACCESS_TOKEN_IS_NULL; Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&oa_id=".$oa_id."&code=".$_code.'&msg=' .OfficialAccountStatus::getMsg($_code).'&mp_data='.json_encode($_mp_data), LOG::ERROR ); return $this->huoError($_code, OfficialAccountStatus::getMsg($_code)); } $_ticket = Common::getJsapiTicket($_access_token); if (empty($_ticket)) { $_code = OfficialAccountStatus::TICKET_IS_NULL; Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&oa_id=".$oa_id."&code=".$_code.'&msg=' .OfficialAccountStatus::getMsg($_code).'&access_token='.$_access_token, LOG::ERROR ); return $this->huoError($_code, OfficialAccountStatus::getMsg($_code)); } $_rdata = [ 'code' => OfficialAccountStatus::NO_ERROR, 'msg' => OfficialAccountStatus::getMsg(OfficialAccountStatus::NO_ERROR), 'data' => $_ticket ]; return $this->huoReturn($_rdata); } }