作者 anyv
1 个管道 的构建 通过 耗费 0 秒

14

... ... @@ -9,6 +9,7 @@
namespace app\portal\controller;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
class sendAPI {
... ... @@ -169,10 +170,82 @@ class PersonalcenterController extends WeChatBaseController{
*/
public function add_information(){
$options=config('wechat_config');
$app = new Application($options);
$js = $app->js;
$jssdk=$js->config(array('chooseImage','uploadImage'), false,false,true);
$this->assign('jssdk',$jssdk);
return $this -> fetch();
}
/**
* 上传图片
*/
public function savePicture(){
$access_token=$this->getAccessToken();
$filepath = $_POST['mediaId'];
$str = 'uploads/'.date('YmdHis').time().rand('1000000','99999999').'.png';
$targetName = $str;
$access_token = $access_token['accessToken'];
$file = file_get_contents("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$filepath");
file_put_contents($targetName,$file);
$msg["filename"] = '/'.$str;
return json_encode($msg);
}
public function getAccessToken() {
// access_token 应该全局存储与更新
// 获取数据库中的access_token
$token = Db::name('token') -> where('id',1) -> find();
// accessToken过期或不存在时
if($token['time'] + $token['expiresIn'] < time() || $token['accessToken'] == NULL){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="."wxdf34ec2179e19b9a"."&secret="."133ced06db2a45843ab52abc691ee82d";
// 微信返回的信息
$returnData = json_decode($this->httpGet($url));
// 组装数据
$resData['accessToken'] = $returnData->access_token;
$resData['expiresIn'] = $returnData->expires_in;
$resData['time'] = time();
// 把数据存进数据库
Db::name('token') -> where('id',1) -> update($resData);
$res = $resData;
}else{
$res = $token;
}
return $res;
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_TIMEOUT, 500 );
curl_setopt($curl, CURLOPT_URL, $url );
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
/**
* 浏览记录
*/
... ...