微信分享
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | <?php /** * Description of WxShare *微信分享 * @author xinjun */ namespace Controller\Wx; use Controller\Home\HomeBase; use Model\Wx\UserModel; use Model\Wx\TokenModel; use Framework\Net\Request; class WxShare extends HomeBase { public function __construct() { parent::__construct(); } /** * 分享配置信息 * @return type */ public function getConfig() { $url = $_POST [ 'url' ]; $conf = array (); $conf [ 'debug' ] = true; $conf [ 'appId' ] = $this ->getAppid(); $conf [ 'timestamp' ] = time(); $conf [ 'nonceStr' ] = "Wm3WZYTPz0wzccnW" ; $conf [ 'signature' ] = $this ->getSignature( $conf [ 'nonceStr' ], $conf [ 'timestamp' ], $url ); $conf [ 'jsApiList' ] = [ 'onMenuShareTimeline' , 'onMenuShareAppMessage' ]; // print_r($conf);exit; return $this ->getData( $conf ); //json_encode($conf,JSON_UNESCAPED_SLASHES) ; } /** * 获取appid * @return type */ public function getAppid() { $tokenModel = new TokenModel(); $appidData = $tokenModel ->getAccessToken( array ( 'nf_type' =>1)); return $appidData [ 'nf_appid' ]; } /** * 获取签名signature * @param type $UNSTR * @param type $TIMESTAMP * @return type */ public function getSignature( $UNSTR , $TIMESTAMP , $url ) { // 获取 jsapi_ticket 值 $jsapi_ticket = $this ->getTicket(); $signature = 'jsapi_ticket=' . $jsapi_ticket . '&noncestr=' . $UNSTR . '×tamp=' . $TIMESTAMP . '&url=' . $url ; return sha1( $signature ); } /** * 获取 ticket * @return boolean */ public function getTicket() { // 判断数据库jsapi_ticket更新时间 // 超过 1000 秒则重新获取 并更新数据库 ( 每日调用次数为 100万次 2016年12月15日 ) //取基础access_token $tokenModel = new TokenModel(); $tokenData = $tokenModel ->getAccessToken( array ( 'id' => 2)); $accesstokenData = $tokenModel ->getAccessToken( array ( 'id' => 1)); if (( $tokenData [ 'nf_val' ] != '' )&& (( $tokenData [ 'nf_time' ] + 6000) > time())) { return $tokenData [ 'nf_val' ]; } else { // 更新 jsapi_ticket $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $accesstokenData [ 'nf_val' ]. "&type=jsapi" ; $https = true; $method = 'get' ; $data = null; $jsonJsapi_ticket = $this ->request( $url , $https , $method , $data ); $jsapi_ticket = json_decode( $jsonJsapi_ticket , true); if (!isset( $jsapi_ticket [ 'ticket' ])) { return false; } $upTicket = $tokenModel ->edit( array ( 'nf_val' => $jsapi_ticket [ 'ticket' ], 'nf_time' =>time()), array ( 'id' =>2)); if ( $upTicket ) { $data = $tokenModel ->getAccessToken( array ( 'id' => 2)); if (! empty ( $data )){ $jsapi_ticket [ 'ticket' ] = $data [ 'nf_val' ]; return $jsapi_ticket [ 'ticket' ]; } else { return false; } } else { return false; } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步