一,在controll重写add方法
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException(true)->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
Db::commit();
//添加方法
if($result){
//通过save后获取自增的ID,传进获取二维码方法,生成跳转指定小程序页面的二维码,保存到数据表中
$this->getEwm($this->model->getData('id'));
}
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
2、自定义getEwm方法
public function getEwm($id){
$access_token = $this->getWxAccessToken();
// p($access_token);die;
//构建请求二维码参数
//path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
//width是二维码宽度
$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
$data = array();
$data['scene'] = 'type=qrcode';
$data['path'] = "pages/actDetails/actDetails?id=".$id;
$data = json_encode($data);
// $data = json_encode(array("path"=>"pages/index/index?aOpenid=".$openid,"width"=> 150));
$jpg = $this->get_http_array($qcode,$data);
$imgDir = 'uploads/zshd/';
// $filename="ewm.jpg";///要生成的图片名字
$filename = 'zshdact'.$id.'.jpg';
$file = fopen("./".$imgDir.$filename,"w");//打开文件准备写入
fwrite($file,$jpg);//写入
fclose($file);//关闭
$imgUrl = constant("URL").'/uploads/zshd/'.$filename;
//更新二维码图片到活动表中
ZshdActivityModel::where('id',$id)->update(['ewmImage'=>$imgUrl]);
// return json($imgUrl);
}
public function get_http_array($url,$post_data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //没有这个会自动输出,不用print_r();也会在后面多个1
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
return $output;
}
/**
* 获取小程序access_token
*/
public function getWxAccessToken(){
$appId = constant('appidZshd');
$appSecret = constant('secretZshd');
$access_token = Cache::get('wx_access_token:'.$appId);
if($access_token){
return $access_token;
}else{
//1.请求url地址
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
$res = $this->curl($url);
if(isset($res['errcode']) && $res['errcode']!=0){
return ('获取access_token出错');
}
$access_token = $res['access_token'];
Cache::set('wx_access_token:'.$appId,$access_token,5400);
return $access_token;
}
}
public function curl($url,$data = null){
// p($data);die;
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
// curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$res = curl_exec($curl);
curl_close($curl);
$json_obj = json_decode($res,true);
// p($json_obj);die;
return $json_obj;
}










