123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- /**
- * OaOut.php UTF-8
- * 消息对外
- *
- * @date : 2018/9/13 22:09
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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);
- }
- }
|