<view>
<form bindsubmit="formAdd">
<l-input label="标题" name="title" placeholder="请输入标题" />
<l-input label="文章" name="text" placeholder="说点什么"/>
请选择上传文件
<l-image-picker count="9" name="file" bind:linchange="onChangeTap" bind:linremove="remove" />
<l-input label="位置" value="{{location}}" name="address" bindtap="map"
disabled="{{true}}" placeholder="请选择位置"/>
<checkbox-group bindchange="checkboxChange">
<label class="weui-cell weui-check__label" wx:for="{{items}}" wx:key="{{item.value}}">
<view class="weui-cell__hd">
<checkbox value="{{item.value}}" name="states" checked="{{item.checked}}"/>
{{item.name}}
</view>
</label>
</checkbox-group>
<button type="default" form-type="submit">发布信息</button>
</form>
</view>
data: {
items: [
{value: '小记', name: '小记'},
{value: '爱情', name: '爱情'},
{value: '友情', name: '友情'},
{value: '亲情', name: '亲情'},
{value: '生活', name: '生活'}
],
arr :[],
path : [],
},
checkboxChange(e) {
const items = this.data.items
const values = e.detail.value
for (let i = 0, lenI = items.length; i < lenI; ++i) {
items[i].checked = false
for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
if (items[i].value === values[j]) {
items[i].checked = true
break
}
}
}
this.setData({
items
})
},
map(e){
var that = this
wx.chooseLocation({
success:res=>{
var location = res.address+res.name
that.setData({
location:location,
latitude:res.latitude,
longitude:res.longitude
})
}
})
},
formAdd:function(e){
let add = e.detail.value
let _token =wx.getStorageSync('token');
let arr = this.data.arr;
console.log(add);
wx.request({
url: 'http://www.transfer.com/api/diaryAdd',
method:'POST',
header:{Authorization:`Bearer ${_token}`},
data:{add,arr},
success:res=>{
if(res.data.code == 202){
wx.showToast({
title: '添加失败',
})
}else{
wx.showToast({
title: '添加成功',
duration: 1000,
success: function () {
wx.switchTab({
url: '/pages/index/index'
})
}
})
}
}
})
},
//文件上传
onChangeTap(ret){
//最新上传的图片
let imgUrl = ret.detail.current
//循环添加图片
imgUrl.map(function(v,k){
wx.uploadFile({
filePath: v,
name: 'file',
url: 'http://www.transfer.com/api/fileapi',
success:res=>{
console.log(res)
}
})
})
},
checkboxChange(e)
{
console.log(e.detail.value);
let arr = e.detail.value;
this.setData({
arr
})
},
添加接口
public function diaryAdd(Request $request)
{
$res = Validator::make($request['add'],[
'title'=>'required',
'text'=>'required',
'address'=>'required',
'states'=>'required',
],[
'title.required'=>'标题不能为空',
'text.required'=>'内容不能为空',
'address.required'=>'地址不能为空',
'states.required'=>'标签不能为空',
]);
if ($res->fails()) {
return ['code'=>202,'data'=>'','msg'=>$res->errors()];
}
$data = $request['add'];
$arr = $request['arr'];
$data['file'] = implode("#",$data['file']);
$data['states'] = implode(',',$arr);
//添加用户入库
$userModel = Diary::create($data);
if($userModel){
return ['code'=>200,'msg'=>'发布成功','data'=>''];
}else{
return ['code'=>400,'msg'=>'发布失败','data'=>''];
}
}
文件云存储接口
public function fileapi(Request $request)
{
$pathName = $request->file('file')->getPathname();
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
$accessKeyId = "LTAI5tR7SrAkrCBVV1NjUV9m";
$accessKeySecret = "7QoMZhXuGQAi4l2lFydaBez76ES9Ko";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-shanghai.aliyuncs.com";
// 存储空间名称
$bucket= "lizichen";
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
$fileName = md5(time() . $pathName . rand(1111,999999));
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
$object = date('Y-m-d',time()) . '/' .$fileName . '.png';
$content = file_get_contents($pathName);
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
print $e->getMessage();
}
}
|