//声明全局变量
var image_url_string = ''; 用户自定义上传商品图片 此处写的图片路径为字符串
var title ='';
var remark ='';
//ajax将本地图片上传到阿里云 oss
// image_path图片路径
function upload_img_function(image_path){
api.ajax({
url: Param_ApiDomain,
method: 'post',
data: {
values: {
service: 'User.testUpload'
},
files: {
file: image_path
}
}
},function(ret, err){
if (ret) {
upload_image_success_function(ret.data);
} else {
upload_image_fail_function();
}
});
}
//本地图片上传成功时的回调函数
function upload_image_success_function(upload_image_path){
image_url_string = image_url_string + upload_image_path + ',';//拼接图片路径字符串
check_img_html +=‘' //上传图片后的样式和内容
<span class="delete_img" data-img-url='+ upload_image_path +' onclick="delete_img_function(this)">//给图片上的删除按钮加上属性值 等下要随机删除的图片路径
show_img.innerHTML = check_img_html;
}
//清除图片
function delete_img_function(delete_img_info){
var remove_img = delete_img_info.parentNode;
$api.remove(remove_img);
check_img_html = $api.html(show_img);
show_img_number(); //判断显示上传几张图片的一个函数
var delete_img_url = $api.attr(delete_img_info, 'data-img-url');
// 替换字符串 将要删除的字符串替换成空的
image_url_string = image_url_string.replace(delete_img_url+',','');
console.log(image_url_string);
}
//当input框失去焦点时(onblur())获取到input的value值
function save(){
title = $api.val($api.byId('purchase_item_name'));
remark =$api.val($api.byId('purchase_item_detail'));
}
//点击保存按钮时将前台数据发送到后台
//注意函数内部的变量跟全局的变量名字虽然一样,但是值不一样(踩坑)
$api.dom('.send_msg').onclick= function(){
save();
var user_id = 1;
//有三个参数是必传项 在前台要作判断 为空时就不向后台传递数据
if(title!=''&&remark!=''&&image_url_string!=''){
add_purchase(user_id,title,remark,image_url_string);
}else{
alert('商品情報と写真は必須内容とされています!');
}
}
//写个函数将前台所要传给后台的数据写成变量当参数传给后台
function add_purchase(user_id,title,remark,image_url_string){
api.ajax({
url: Param_ApiDomain,
method: 'post',
data: {
values: {
service:'Purchase_Purchase.Add',
user_id:user_id,
title:title,
remark:remark,
image:image_url_string,
// token:token
}
}
},function(ret, err){
if (ret) {
alert( JSON.stringify( ret ) );
} else {
alert( JSON.stringify( err ) );
}
});
}