123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- /**
- * MiniMessage.php UTF-8
- * 小程序消息
- *
- * @date : 2018/9/14 20:52
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HuoMp 1.0
- */
- namespace huoMpMsg\controller;
- use huo\controller\game\GameCache;
- use huo\controller\member\MemCache;
- use huo\controller\pay\SdkOrderCache;
- use huo\model\order\OrderModel;
- use huolib\constant\CacheConst;
- use huolib\constant\CommonConst;
- use huolib\constant\OauthConst;
- use huolib\constant\OrderConst;
- use huomp\logic\game\OaMpLogic;
- use huomp\model\weixin\MpConfModel;
- use huomp\model\weixin\OaMpModel;
- use huoMpMsg\constant\OfficialAccountConst;
- use huoMpMsg\model\MpMsgLogModel;
- use huoMpMsg\status\OfficialAccountStatus;
- use think\Cache;
- use think\Log;
- class MiniMessage {
- /**
- * 接收微信数据
- *
- * @param string $mp_id 小程序ID
- * @param array $param
- *
- * @return bool
- */
- public function receive($mp_id, $param) {
- if (isset($param['MsgType']) && 'event' == $param['MsgType'] && 'user_enter_tempsession' == $param['Event']) {
- /* 直接进来客服 查询是否有订单需要支付 获取缓存信息 */
- $this->sendOrderEnter($mp_id, $param['FromUserName']);
- return true;
- } else if (isset($param['MsgType']) && 'text' == $param['MsgType']
- && false !== strpos(
- $param['Content'], OfficialAccountConst::MSG_RECEIVE_TEXT_1
- )) {
- /* 回复1 获取二维码 */
- $this->sendOaQrCode($mp_id, $param['FromUserName']);
- } else if (isset($param['MsgType']) && 'text' == $param['MsgType']
- && false !== strpos(
- $param['Content'], OfficialAccountConst::MSG_RECEIVE_TEXT_2
- )) {
- /* 回复2 获取最近一笔订单,已支付提示已支付,未支付弹出支付链接*/
- $this->sendOrder($mp_id, $param['FromUserName']);
- } else {
- $_p_content = isset($param['Content']) ? $param['Content'] : '';
- $_msg = (new OaReturnMsg())->getKeyReturnMsg($mp_id, $_p_content);
- if (!empty($_msg)) {
- (new OaMessage())->sendTextMsg($mp_id, $param['FromUserName'], $_msg);
- }
- }
- $_data['mp_id'] = $mp_id;
- $_data['wx_msg_id'] = get_val($param, 'MsgId', 0);
- $_data['source_id'] = get_val($param, 'ToUserName', '');
- $_data['open_id'] = get_val($param, 'FromUserName', '');
- $_data['msg_type'] = get_val($param, 'MsgType', '');
- $_data['pic_url'] = get_val($param, 'PicUrl', '');
- $_data['media_id'] = get_val($param, 'MediaId', '');
- $_data['title'] = get_val($param, 'Title', '');
- $_data['mini_app_id'] = get_val($param, 'AppId', '');
- $_data['page_path'] = get_val($param, 'PagePath', '');
- $_data['thumb_url'] = get_val($param, 'ThumbUrl', '');
- $_data['thumb_media_id'] = get_val($param, 'ThumbMediaId', '');
- $_data['text'] = get_val($param, 'Content', '');
- $_data['wx_create_time'] = get_val($param, 'CreateTime', 0);
- $_model = new MpMsgLogModel();
- $_rs = $_model->hasMsg($_data['wx_msg_id']);
- if (empty($_rs)) {
- $_model->insertLog($_data);
- }
- return true;
- }
- /**
- * 小游戏客服充值进入客服触发调用
- *
- * @param string $mp_id
- * @param string $open_id
- *
- * @return bool
- */
- public function sendOrderEnter($mp_id, $open_id) {
- $_mem_id = MemCache::ins()->getMemIdByOpenId(OauthConst::OAUTH_MP, $open_id);
- if (empty($_mem_id)) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__
- ."&step=1&mp_id=$mp_id&open_id=$open_id&mem_id=$_mem_id&err_msg=玩家ID不存在",
- Log::ERROR
- );
- return false;
- }
- $_order_id = Cache::get(CacheConst::CACHE_CUSTOMER_ORDER_PAY_PREFIX.$_mem_id);
- /* 不存在最近订单 不做任何处理 */
- if (empty($_order_id)) {
- return true;
- }
- return $this->sendOrderData($mp_id, $open_id, $_order_id);
- }
- /**
- * 小游戏客服充值
- *
- * @param string $mp_id
- * @param string $open_id
- *
- * @return bool
- */
- public function sendOrder($mp_id, $open_id) {
- $_mem_id = MemCache::ins()->getMemIdByOpenId(OauthConst::OAUTH_MP, $open_id);
- if (empty($_mem_id)) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__
- ."&step=1&mp_id=$mp_id&open_id=$open_id&mem_id=$_mem_id&err_msg=玩家ID不存在",
- Log::ERROR
- );
- return false;
- }
- $_order_id = Cache::get(CacheConst::CACHE_CUSTOMER_ORDER_PAY_PREFIX.$_mem_id);
- /* 不存在最近订单,获取最后一个订单,若订单未支付弹出订单,已支付则不做任何处理 */
- if (empty($_order_id)) {
- $_order_id = (new OrderModel())->getMemLastOrderId($_mem_id);
- if (empty($_order_id)) {
- return false;
- }
- }
- return $this->sendOrderData($mp_id, $open_id, $_order_id);
- }
- /**
- * 发送订单信息
- *
- * @param $mp_id
- * @param $open_id
- * @param $order_id
- *
- * @return bool
- */
- public function sendOrderData($mp_id, $open_id, $order_id) {
- $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
- if (empty($_order_data)) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__
- ."&step=1&mp_id=$mp_id&open_id=$open_id&order_id=$order_id&err_msg=订单不存在",
- Log::ERROR
- );
- return false;
- }
- if (OrderConst::PAY_STATUS_SUC == $_order_data['status']) {
- /* 已支付的不做任何处理 */
- return true;
- }
- /* 获取小程序默认关联公众号 */
- $_wx_app_id = (new OaMpLogic())->getDefaultOaId($_order_data['app_id']);
- $_game_data = GameCache::ins()->getInfoByAppId($_order_data['app_id']);
- $_data = [
- "title" => '点我充值',
- "description" => '点我充值'.$_order_data['real_amount'].'元',
- "url" => MPBOXSITE.'/order/index?wx_app_id='.$_wx_app_id.'&order_id='.$order_id,
- "thumb_url" => cmf_get_image_url($_game_data['icon'])
- ];
- return $this->sendLinkMsg($mp_id, $open_id, $_data);
- }
- /**
- * 发送图文消息
- *
- * @param string $mp_id
- * @param string $open_id 玩家应用ID
- * @param string $data
- *
- * @return bool
- */
- public function sendLinkMsg($mp_id, $open_id, $data = '') {
- $_msg_type = OfficialAccountConst::MSG_TYPE_LINK;
- if (empty($data)) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__
- ."&step=1&mp_id=$mp_id&open_id=$open_id&err_msg=发送数据为空", Log::ERROR
- );
- return false;
- }
- $_mp_conf = (new MpConfModel())->getDataByMpId($mp_id);
- if (empty($_mp_conf)) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__
- ."&step=2&mp_id=$mp_id&open_id=".$open_id, Log::ERROR
- );
- return false;
- }
- $_wx_app_secret = $_mp_conf['app_secret'];
- (new OfficialAccountApi())->sendMsg($mp_id, $_wx_app_secret, $open_id, $_msg_type, $data);
- return true;
- }
- /**
- * 发送公众号二维码
- *
- * @param string $mp_id
- * @param string $open_id
- *
- * @return bool
- */
- public function sendOaQrCode($mp_id, $open_id) {
- $_media_id = $this->getQrCodeMediaId($mp_id);
- $_msg_type = 'image';
- $_data = [
- "media_id" => $_media_id
- ];
- $_mp_conf = (new MpConfModel())->getDataByMpId($mp_id);
- if (empty($_mp_conf) || empty($_media_id)) {
- Log::write("func=".__FUNCTION__."&class=".__CLASS__."&step=1&mp_id=$mp_id&open_id=$open_id", Log::ERROR);
- return false;
- }
- $_wx_app_secret = $_mp_conf['app_secret'];
- $_rs = (new OfficialAccountApi())->sendMsg($mp_id, $_wx_app_secret, $open_id, $_msg_type, $_data);
- if (OfficialAccountStatus::WX_SYSTEM_ERROR == $_rs['code']) {
- unset($_rs);
- unset($_data);
- $_media_id = $this->getQrCodeMediaId($mp_id, true);
- $_data = [
- "media_id" => $_media_id
- ];
- $_rs = (new OfficialAccountApi())->sendMsg($mp_id, $_wx_app_secret, $open_id, $_msg_type, $_data);
- }
- if (OfficialAccountStatus::NO_ERROR != $_rs['code']) {
- return false;
- }
- return true;
- }
- /**
- * 获取二维码的媒体ID
- *
- * @param string $mp_id 公众号ID
- *
- * @param bool $check_get 是否校验media_id 有效性
- *
- * @return bool|mixed
- */
- public function getQrCodeMediaId($mp_id, $check_get = false) {
- $_cache_key = CacheConst::CACHE_MP_QRCODE_PREFIX.$mp_id;
- $_data = Cache::get($_cache_key);
- if (!empty($_data) && false == $check_get) {
- return $_data;
- }
- $_mp_model = new MpConfModel();
- $_app_id = $_mp_model->getAppIdByMpId($mp_id);
- $_game_info = GameCache::ins()->getInfoByAppId($_app_id);
- $_qr_code = !empty($_game_info['ext_info']['auto_help']) ? $_game_info['ext_info']['auto_help'] : '';
- if (empty($_qr_code)) {
- $_oa_id = (new OaMpModel())->getOaByApp($_app_id);
- $_oa_data = $_mp_model->getDataByMpId($_oa_id);
- $_qr_code = !empty($_oa_data) ? $_oa_data['qr_code'] : '';
- }
- $_mp_conf = $_mp_model->getDataByMpId($mp_id);
- if (empty($_mp_conf)) {
- Log::write("func=".__FUNCTION__."&class=".__CLASS__."&step=1&mp_id=$mp_id", Log::ERROR);
- return false;
- }
- $_file_path = CMF_ROOT.'/public/upload/'.$_qr_code;
- if (empty($_qr_code)) {
- $_file_path = '/mini/public/upload/qrcode_for_gh_a30f9cefe8e9_258.jpg';
- }
- if (true != file_exists($_file_path)) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&step=2&mp_id=$mp_id&qr_code=$_qr_code&file_path="
- .$_file_path, Log::ERROR
- );
- return false;
- }
- $_wx_app_secret = $_mp_conf['app_secret'];
- $_rs = (new OfficialAccountApi())->imageUpload($mp_id, $_wx_app_secret, $_file_path);
- if (OfficialAccountStatus::NO_ERROR != $_rs['code'] || empty($_rs['data']['media_id'])) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&step=3&mp_id=$mp_id&file_path=".$_file_path."&rs="
- .json_encode($_rs), Log::ERROR
- );
- return false;
- }
- Cache::set($_cache_key, $_rs['data']['media_id'], CommonConst::CONST_DAY_SECONDS * 3);
- return $_rs['data']['media_id'];
- }
- }
|