123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- /**
- * ShareController.php UTF-8
- *
- *
- * @date : 2018/5/26 15:48
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : luowei <lw@huosdk.com>
- * @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'];
- }
- }
|