HTML页面
<view class="container">
<view class="page-body">
<form catchsubmit="formSubmit" catchreset="formReset">
<view class="page-section">
<view class="page-section-title">姓名</view>
<view class="weui-cells weui-cells_after-title">
<view class="weui-cell weui-cell_input">
<view class="weui-cell__bd" style="margin: 30rpx 0" >
<input class="weui-input" name="name" placeholder="这是一个输入框" />
</view>
</view>
</view>
</view>
<view class="page-section">
<view class="page-section-title">年龄</view>
<view class="weui-cells weui-cells_after-title">
<view class="weui-cell weui-cell_input">
<view class="weui-cell__bd" style="margin: 30rpx 0" >
<input class="weui-input" name="age" placeholder="这是一个输入框" />
</view>
</view>
</view>
</view>
<view class="page-section page-section-gap">
<view class="page-section-title">性别</view>
<radio-group name="sex">
<label><radio value="男"/>男</label>
<label><radio value="女"/>女</label>
</radio-group>
</view>
<button size="mini" bindtap="bindUploadTap">
选择图片
</button>
<image src="{{imgsrc}}"></image>
<input hidden="true" name="image" value="{{images}}"/>
<view class="btn-area">
<button style="margin: 30rpx 0" type="primary" formType="submit">Submit</button>
</view>
</form>
</view>
</view>
js代码块
formSubmit(e) {
var name = e.detail.value.name;
var sex = e.detail.value.sex;
var age = e.detail.value.age;
var image = e.detail.value.image;
wx.request({
url: 'http://day518.homework.com/api/insert',
data: {
name:name,
sex:sex,
age:age,
image:image
},
success (res) {
console.log(res.data)
}
})
},
bindUploadTap:function(){
let _this = this
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
var tempFilePaths = res.tempFilePaths
_this.setData({
imgsrc: tempFilePaths[0]
})
wx.uploadFile({
header: {
'content-type': 'application/x-www-form-urlencoded'
},
url: 'http://day518.homework.com/api/upload_img',
method:"post",
filePath: tempFilePaths[0],
name: 'image',
formData: {
'user': 'test'
},
success: function (res) {
var data = res.data
var images=res.data;
_this.setData({images})
}
})
}
})
},
下载阿里云SDK
composer require aliyuncs/oss-sdk-php
下载image类库文件
composer require intervention/image
引入库
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Model\RefererConfig;
use Intervention\Image\Facades\Image;
public function insert(Request $request)
{
$param=$request->all();
$validator = Validator::make($request->all(), [
'name' => 'required',
'sex' => 'required',
'age' => 'required',
]);
if ($validator->fails()) {
return ['code'=>500,'data'=>'','msg'=>'所填信息不能为空'];
}
$res=Students::create($param);
if ($res){
return ['code'=>200,'data'=>$res,'msg'=>'添加成功'];
}else{
return ['code'=>500,'data'=>'','msg'=>'添加失败'];
}
}
public function upload_img(Request $request)
{
$file=$request->file('image')->store('','image');
$fontPath=public_path('STKAITI.TTF');
Image::make("./upload/".$file)
->fit(300,300)
->text('抽奖',135,135,function ($font)use ($fontPath){
$font->file($fontPath)
->size(20)
->color('#000')
->valign('center');
})->save("./upload/".$file);
$accessKeyId = "LTAI5t5s5FJEdqgbEtVikhw1";
$accessKeySecret = "5hRfEbliZN6mIFQjctJlSj8A82YsW5";
$endpoint = "http://oss-cn-shanghai.aliyuncs.com";
$bucket= "aliqiang";
$object = time().'.jpg';
$refererConfig = new RefererConfig();
$refererConfig->setAllowEmptyReferer(true);
$refererConfig->addReferer("*.console.aliyun.com");
$refererConfig->addReferer("www.aliiyuncs.com");
try{
$ossClient = new OssClient($accessKeyId,$accessKeySecret, $endpoint);
$ossClient->uploadFile($bucket, $object, public_path("/upload/".$file));
$ossClient->putBucketReferer($bucket, $refererConfig);
$urlPath="http://day518.homework.com/upload/".$file;
return $urlPath;
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
}