* @version : HUOSDK 8.0 */ namespace h5wap\wap\controller; use h5wap\common\controller\V2BaseController; use huo\controller\integral\MemIa; use huo\controller\posts\ShareAct; use huo\model\integral\MemItgLogModel; use huolib\constant\IaConst; use huolib\constant\ShareConst; use huolib\status\CommonStatus; use huolib\status\MemberStatus; use huolib\tool\StrUtils; use huolib\tool\Time; use huoMpMsg\status\OfficialAccountStatus; class ShareController extends V2BaseController { public function _initialize() { parent::_initialize(); } /** * 分享成功后回调接口 * http://doc.1tsdk.com/138?page_id=3359 * 【域名】/wap/share/add */ public function add() { $_mem_id = $this->mem_id; if (empty($_mem_id)) { $_code = MemberStatus::NO_ERROR; $this->success(lang(MemberStatus::getMsg($_code)), [], $_code); } $_game_rq = $this->setGameData(); $_channel_rq = $this->setChannelData(); $_device_rq = $this->setDeviceData(); $_mem_rq = $this->setMemData(); $_share_type = $this->request->param('share_type/s', ''); if (empty($_share_type)) { $_code = CommonStatus::INVALID_PARAMS; $this->error(lang(CommonStatus::getMsg($_code)), [], $_code); } if (ShareConst::SHARE_TYPE_POSTS == $_share_type) { $_param = $this->request->param(); $_act_id = $this->request->param('share_id/d', 0); if (empty($_act_id)) { $_code = CommonStatus::INVALID_PARAMS; $this->error(lang(CommonStatus::getMsg($_code)), [], $_code); } //分享成功回调处理 $_rs = (new ShareAct())->callback($_game_rq, $_channel_rq, $_device_rq, $_mem_rq, $_param); return $this->returnData($_rs); } else { // TODO: liuhongliang 2018/5/28 其他分享类型 $_code = MemberStatus::NO_ERROR; $this->success(lang(MemberStatus::getMsg($_code)), [], $_code); } } /** * 获取微信分享配置 */ public function config() { $this->checkLogin(); $_share_url = $this->request->param('share_url/s', ''); if (empty($_share_url)) { $_code = CommonStatus::INVALID_PARAMS; $this->error(CommonStatus::getMsg($_code), [], $_code); } $_wx_app_id = $this->getDefaultWxAppId(); if (empty($_wx_app_id)) { $_code = CommonStatus::INNER_ERROR; $this->error(CommonStatus::getMsg($_code), [], $_code); } $_rs = $this->getJsapiTicket(); $_share_url = urldecode($_share_url); $_rdata = [ 'noncestr' => StrUtils::getRandChars(16), 'jsapi_ticket' => $_rs['data'], 'timestamp' => time(), 'url' => $_share_url ]; ksort($_rdata); $_sig_str = 'jsapi_ticket='.$_rdata['jsapi_ticket'].'&noncestr='.$_rdata['noncestr'].'×tamp=' .$_rdata['timestamp'].'&url='.$_rdata['url']; $_rdata['signature'] = sha1($_sig_str); $_rdata['wx_app_id'] = $_wx_app_id; /* 读取配置 */ $_share_config = [ 'share_title' => '分享好游戏', 'share_content' => '多款精品游戏一起玩!', 'share_icon' => '/upload/icon6.png', ]; $_rdata['title'] = get_val($_share_config, 'share_title', '分享标题~'); $_rdata['content'] = get_val($_share_config, 'share_content', '分享内容~'); $_icon = get_val($_share_config, 'share_icon', ''); $_rdata['icon'] = cmf_get_image_url($_icon); $rdata['code'] = CommonStatus::NO_ERROR; $rdata['msg'] = CommonStatus::getMsg(CommonStatus::NO_ERROR); $rdata['data'] = $_rdata; $this->returnData($rdata); } /** * 分享通知接口 * * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function shareNotify() { $_today_is_share = $this->getTodayIsShare($this->mem_id); if (!$_today_is_share) { (new MemIa($this->mem_id))->doItgAct(IaConst::IA_WX_SHARE); } } /** * 获取今日是否分享 * * @param $mem_id * * @return bool * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ private function getTodayIsShare($mem_id) { $_map['mem_id'] = $mem_id; $_map['ia_id'] = IaConst::IA_WX_SHARE; list($_start_time, $_end_time) = Time::today(); $_map['create_time'] = ['BETWEEN', [$_start_time, $_end_time]]; $_detail = (new MemItgLogModel())->getDetail($_map); if (!empty($_detail)) { return true; } return false; } private function getDefaultWxAppId() { if (file_exists(GLOBAL_CONF_PATH."extra/oauth/weixin.php")) { $_config = include GLOBAL_CONF_PATH."extra/oauth/weixin.php"; } $_wx_app_id = !empty($_config['APP_KEY']) ? $_config['APP_KEY'] : ''; return $_wx_app_id; } public function getJsapiTicket() { $_config = []; if (file_exists(GLOBAL_CONF_PATH."extra/oauth/weixin.php")) { $_config = include GLOBAL_CONF_PATH."extra/oauth/weixin.php"; } $_mp_data = $_config; $_mp_data['mp_id'] = $_config['APP_KEY']; $_mp_data['app_secret'] = $_config['APP_SECRET']; $_access_token = $this->getAccessToken($_mp_data['mp_id'], $_mp_data['app_secret']); if (empty($_access_token)) { return false; } $_ticket = $this->getJsapiTicket1($_access_token); if (empty($_ticket)) { return false; } $_rdata = [ 'code' => OfficialAccountStatus::NO_ERROR, 'msg' => OfficialAccountStatus::getMsg(OfficialAccountStatus::NO_ERROR), 'data' => $_ticket ]; return $_rdata; } /** * 获取 access_token * https://developers.weixin.qq.com/miniprogram/dev/api/token.html * * @param string $appid 第三方用户唯一凭证 * @param string $app_secret 第三方用户唯一凭证密钥,即appsecret * * @return string|bool */ public static function getAccessToken($appid, $app_secret) { $_url = 'https://api.weixin.qq.com/cgi-bin/token'; $_param['grant_type'] = 'client_credential'; $_param['appid'] = $appid; $_param['secret'] = $app_secret; $_rdata = self::curl($_url, http_build_query($_param)); $_rdata = json_decode($_rdata, true); if (!isset($_rdata['access_token'])) { return false; } $_data['access_token'] = $_rdata['access_token']; $_data['expires_in'] = $_rdata['expires_in']; $_data['expires_time'] = time() + $_rdata['expires_in']; return $_data['access_token']; } /** * CURL请求 * * @param string $url 请求的URL * @param string $params 请求的参数 * @param string $is_get 是否是get请求 * @param array $header 头 * * @return int|mixed */ public static function curl($url, $params, $is_get = "GET", $header = []) { $_header = $header; $ch = curl_init(); if ("GET" == $is_get) { if (!empty($params)) { $url = $url.'?'.$params; } curl_setopt($ch, CURLOPT_HEADER, 0); // 过滤HTTP头 } else { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上 /* https 请求 */ if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if (!empty($_header)) { array_push($_header, 'Content-Length: '.strlen($params)); curl_setopt($ch, CURLOPT_HTTPHEADER, $_header); } $_rs = curl_exec($ch); if (false === $_rs) { $_rs = curl_errno($ch); } curl_close($ch); return $_rs; } /** * 获取 jsapi_ticket * https://developers.weixin.qq.com/miniprogram/dev/api/token.html * * @param $access_token $appid 第三方用户唯一凭证 * * @return string|bool */ public static function getJsapiTicket1($access_token) { $_url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; $_param['type'] = 'jsapi'; $_param['access_token'] = $access_token; $_rdata = self::curl($_url, http_build_query($_param)); $_rdata = json_decode($_rdata, true); if (!isset($_rdata['ticket'])) { return false; } $_data['ticket'] = $_rdata['ticket']; $_data['expires_in'] = $_rdata['expires_in']; $_data['expires_time'] = time() + $_rdata['expires_in']; return $_data['ticket']; } }