1. 程式人生 > >小程式中圖片上傳

小程式中圖片上傳

前端小程式:wxml

<!--huitianxia/view/attend/attend/attend.wxml-->

<import src="/huitianxia/view/common/foot.wxml"/>

<view class='wrapper'>

<view class='bg'>

<image src="{{bg}}" mode="widthFix"></image>

</view>

<view class='content'>

<view class='attend'>

<view class='con'>

<form bindsubmit="bindSaveTap">

<view class='info info1 clearfix'>

<view class='info-left'>

<image src="{{content.icon1}}"></image>

</view>

<view class='info-right'>

<input type='text' placeholder='單位名稱' bindchange="bindworknameChange" value="{{u_workname}}"/>

</view>

</view>

<view class='info info2 clearfix'>

<view class='info-left'>

<image src="{{content.icon2}}"></image>

</view>

<view class='info-right'>

<input type='text' bindchange="bindCorporateChange" value='{{u_corporate}}' placeholder='法人代表'/>

</view>

</view>

<view class='info info3 clearfix'>

<view class='info-left'>

<image src="{{content.icon3}}"></image>

</view>

<view class='info-right'>

<input type='text' bindchange="bindRegisternumberChange" value='{{u_registernumber}}' placeholder='工商登記號'/>

</view>

</view>

<view class='info info4 clearfix'>

<view class='info-left'>

<image src="{{content.icon4}}"></image>

</view>

<view class='info-right'>

<input type='text' bindchange="bindRegistercapitalChange" value='{{u_registercapital}}' placeholder='註冊資金'/>

</view>

</view>

<view class='info info5 clearfix'>

<view class='info-left'>

<image src="{{content.icon5}}"></image>

</view>

<view class='info-right'>

<input type='text' bindchange="bindStaffnumberChange" value='{{u_staffnumber}}' placeholder='員工數'/>

</view>

</view>

<view class='info info6 clearfix'>

<view class='info-left'>

<image src="{{content.icon6}}"></image>

</view>

<view class='info-right'>

<input type='text' bindchange="bindCapitalsumChange" value='{{u_capitalsum}}' placeholder='資產總數'/>

</view>

</view>

<view class='info info7 clearfix'>

<view class='info-left'>

<image src="{{content.icon7}}"></image>

</view>

<view class='info-right'>

<input type='text' bindchange="bindMarketsumChange" value='{{u_marketsum}}' placeholder='銷售總額'/>

</view>

</view>

<view class='info info8 clearfix'>

<view class='info-left'>

<image src="{{content.icon8}}"></image>

</view>

<view class='info-right'>

<input type='text' bindchange="bindCompanydescChange" value='{{u_companydesc}}' placeholder='企業簡介'/>

</view>

</view>

<view class='info info9 clearfix'>

<view class='info-left'>

<image mode='widthFix' src="{{content.icon9}}"></image>

</view>

<view class='info-right'>

<input type='text' bindchange="bindTypeChange" value='{{u_type}}' placeholder='申請選項'/>

</view>

</view>

<view class='photo clearfix'>

<view class='info-left'>

<image src='{{content.icon10}}'></image>

<text>上傳營業執照</text>

</view>

<view class='info-right'>

<view class='white'>

<image src='{{photo}}' bindtap='upload'></image>

</view>

</view>

</view>

<view class='infos clearfix'>

<view class='info-left'>

<image src='{{content.icon11}}'></image>

<text>姓名</text>

</view>

<view class='info-right'>

<input type='text' bindchange="bindUsernameChange" value='{{m_username}}' placeholder='請填寫聯絡人姓名'></input>

</view>

</view>

<view class='infos clearfix'>

<view class='info-left'>

<image src='{{content.icon12}}'></image>

<text>電話</text>

</view>

<view class='info-right'>

<input type='text' bindchange="bindtelphoneChange" type='number' placeholder='請填寫聯絡人電話' value="{{u_telphone}}"/>

</view>

</view>

<view class='btn'>

<button formType="submit" type="primary" size="default" bindtap='bindChaneInfo'>提交</button>

</view>



 

</form>

 

</view>

 

</view>

</view>




 

<template is="foot" data="{{footList}}"/>

</view>

js裡面:

//huitianxia/view/attend/attend/attend.js

var app = getApp();

Page({

/**

*頁面的初始資料

*/

data: {

img_arr: [],

formdata: '',

bg: "../../../image/attend/[email protected]",

photo: "../../../image/attend/[email protected]",

content: {

icon1: "../../../image/attend/[email protected]",

icon2: "../../../image/attend/[email protected]",

icon3: "../../../image/attend/[email protected]",

icon4: "../../../image/attend/[email protected]",

icon5: "../../../image/attend/[email protected]",

icon6: "../../../image/attend/[email protected]",

icon7: "../../../image/attend/[email protected]",

icon8: "../../../image/attend/[email protected]",

icon9: "../../../image/attend/[email protected]",

icon10: "../../../image/attend/[email protected]",

icon11: "../../../image/attend/[email protected]",

icon12: "../../../image/attend/[email protected]",

},

footList: [

{

text: "查詢證書",

pressImg: "../../../image/common/[email protected]",

seleImg: "../../../image/common/[email protected]",

url: "/huitianxia/view/index/index/index",

active: false

},

{

text: "參會申請",

pressImg: "../../../image/common/[email protected]",

seleImg: "../../../image/common/[email protected]",

url: "/huitianxia/view/attend/attend/attend",

active: true

},

{

text: "聯絡我們",

pressImg: "../../../image/common/[email protected]",

seleImg: "../../../image/common/[email protected]",

url: "/huitianxia/view/contact/contact/contact",

active: false,

}

]

},

/**

*生命週期函式--監聽頁面載入

*/

onLoad: function (options) {

},

/**

*生命週期函式--監聽頁面初次渲染完成

*/

onReady: function () {

},

/**

*生命週期函式--監聽頁面顯示

*/

onShow: function () {

},

/**

*生命週期函式--監聽頁面隱藏

*/

onHide: function () {

},

/**

*生命週期函式--監聽頁面解除安裝

*/

onUnload: function () {

},

/**

*頁面相關事件處理函式--監聽使用者下拉動作

*/

onPullDownRefresh: function () {

},

/**

*頁面上拉觸底事件的處理函式

*/

onReachBottom: function () {

},

 

// 呼叫圖片上傳介面

 

upload: function () {

var that = this;

wx.chooseImage({

success: function (res) {

var tempFilePaths = res.tempFilePaths;

// console.log(tempFilePaths[0])

that.setData({

img:tempFilePaths[0],

photo: tempFilePaths[0]

})

 

// var uid = app.globalData.uid;

// var web_url = app.globalData.web_url;

// var web_url1 = web_url + "/Tjg/uploadsImg";

// wx.uploadFile({

// url: web_url1, //僅為示例,非真實的介面地址

// filePath: tempFilePaths[0],

// name: 'file',

// formData: {

// 'user': uid

// },

// success: function (res) {

// var res1 = JSON.parse(res.data);

// console.log(res1.data)

// that.setData({

// u_image: res1.data

// })

// }

// })

}

})

},



 

/**

*使用者點選右上角分享

*/

onShareAppMessage: function () {

},

formSubmit: function (e) {

varid = e.target.id

adds = e.detail.value;

adds.program_id = app.jtappid

adds.openid = app._openid

adds.zx_info_id = this.data.zx_info_id

this.upload()

},

//單位名稱start

bindworknameChange: function (e) {

console.log('u_workname傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_workname: e.detail.value

})

},

//法人代表start

bindCorporateChange: function (e) {

console.log('u_corporate傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_corporate: e.detail.value

})

},

//工商登記號start

bindRegisternumberChange: function (e) {

console.log('u_registernumber傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_registernumber: e.detail.value

})

},

//註冊資金start

bindRegistercapitalChange: function (e) {

console.log('u_registercapital傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_registercapital: e.detail.value

})

},

//員工數start

bindStaffnumberChange: function (e) {

console.log('u_staffnumber傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_staffnumber: e.detail.value

})

},

//資產總數start

bindCapitalsumChange: function (e) {

console.log('u_capitalsum傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_capitalsum: e.detail.value

})

},

//銷售總額start

bindMarketsumChange: function (e) {

console.log('u_marketsum傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_marketsum: e.detail.value

})

},

//企業簡介start

bindCompanydescChange: function (e) {

console.log('u_companydesc傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_companydesc: e.detail.value

})

},

//申請選項start

bindTypeChange: function (e) {

console.log('u_type傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_type: e.detail.value

})

},

//姓名start

bindUsernameChange: function (e) {

console.log('m_username傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

m_username: e.detail.value

})

},

//電話start

bindtelphoneChange: function (e) {

console.log('u_telphone傳送選擇改變,攜帶值為', e.detail.value)

this.setData({

u_telphone: e.detail.value

})

},

//提交資料,重新整理頁面

bindChaneInfo: function () {

var check_tel = /^[1][3,4,5,7,8][0-9]{9}$/;

var that = this;

//varu_id=app.globalData.uid;

var u_workname = this.data.u_workname;

var u_corporate = this.data.u_corporate;

var u_registernumber = this.data.u_registernumber;

var u_registercapital = this.data.u_registercapital;

var u_staffnumber = this.data.u_staffnumber;

var u_capitalsum = this.data.u_capitalsum;

var u_marketsum = this.data.u_marketsum;

var u_companydesc = this.data.u_companydesc;

var u_type = this.data.u_type;

var m_username = this.data.m_username;

var u_telphone = this.data.u_telphone;

var u_image = this.data.img;

//console.log(u_image);

if (!u_workname) {

wx.showToast({

title: '請填寫單位名稱',

icon: 'none',

duration: 2000

})

return;

}

if (!u_corporate) {

wx.showToast({

title: '請填寫法人代表',

icon: 'none',

duration: 2000

})

return;

}

if (!u_registernumber) {

wx.showToast({

title: '請填寫工商登記號',

icon: 'none',

duration: 2000

})

return;

}

if (!u_registercapital) {

wx.showToast({

title: '請填寫註冊資金',

icon: 'none',

duration: 2000

})

return;

}

if (!u_staffnumber) {

wx.showToast({

title: '請填寫員工數',

icon: 'none',

duration: 2000

})

return;

}

if (!u_capitalsum) {

wx.showToast({

title: '請填寫資產總數',

icon: 'none',

duration: 2000

})

return;

}

if (!u_marketsum) {

wx.showToast({

title: '請填寫銷售總額',

icon: 'none',

duration: 2000

})

return;

}

if (!u_companydesc) {

wx.showToast({

title: '請填寫企業簡介',

icon: 'none',

duration: 2000

})

return;

}

if (!u_type) {

wx.showToast({

title: '請填寫申請選項',

icon: 'none',

duration: 2000

})

return;

}

if (!u_image) {

wx.showToast({

title: '請上傳圖片',

icon: 'none',

duration: 2000

})

return;

}

if (!m_username) {

wx.showToast({

title: '請填寫聯絡人姓名',

icon: 'none',

duration: 2000

})

return;

}

if (!check_tel.test(u_telphone)) {

wx.showToast({

title: '請填寫正確手機號',

icon: 'none',

duration: 2000

})

return;

}

var web_url = app.globalData.web_url;

var web_url2 = web_url + "/Tjg/applyMember";

 

wx.uploadFile({

url: web_url2, //僅為示例,非真實的介面地址

filePath: that.data.img,

name: 'file',

formData: {

u_workname: u_workname,

u_corporate: u_corporate,

u_registernumber: u_registernumber,

u_registercapital: u_registercapital,

u_staffnumber: u_staffnumber,

u_capitalsum: u_capitalsum,

u_marketsum: u_marketsum,

u_companydesc: u_companydesc,

u_type: u_type,

m_username: m_username,

m_telphone: u_telphone,

},

success: function (res) {

console.log(res);

wx.showToast({

title: "申請成功",

icon: 'none',

duration: 2000

})

setTimeout(function(){

wx.redirectTo({

url: '/huitianxia/view/attend/attend/attend'

})

},1500)

}

})



 

// wx.request({

// url: web_url2,//介面地址

// data: {

// u_workname: u_workname,

// u_corporate: u_corporate,

// u_registernumber: u_registernumber,

// u_registercapital: u_registercapital,

// u_staffnumber: u_staffnumber,

// u_capitalsum: u_capitalsum,

// u_marketsum: u_marketsum,

// u_companydesc: u_companydesc,

// u_type: u_type,

// m_username: m_username,

// m_telphone: u_telphone,

// },

// header: {

// 'content-type': 'application/json'//預設值

// },

// success: function (res) {

// wx.showToast({

// title: res.data.message,

// icon: 'none',

// duration: 2000

// })

// }

// })

}

})

後臺介面:

    //申請成為會員
    public function applyMember(){
        $data['u_workname']=input("u_workname");
        $data['u_corporate']=input("u_corporate");
        $data['u_registernumber']=input("u_registernumber");
        $data['u_registercapital']=input("u_registercapital");
        $data['u_staffnumber']=input("u_staffnumber");
        $data['u_capitalsum']=input("u_capitalsum");
        $data['u_marketsum']=input("u_marketsum");
        $data['u_companydesc']=input("u_companydesc");
        $data['u_type']=input("u_type");
        $data['m_username']=input("m_username");
        $data['m_telphone']=input("m_telphone");
        $data['u_create']=time();
        //接受營業執照business
        $file = request()->file('u_image');
        if($file){
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'business');
                    if($info){
                        // 成功上傳後 獲取上傳資訊
                        $data['u_image'] = $info->getSaveName();
                        //$servername="https://".$_SERVER['SERVER_NAME']."/beijinghuitianxia/public/business/";
                        $data['u_image'] = str_replace("\\","/", $data['u_image']);
                        //$data['u_image'] = $servername.$data['u_image'];
                        $res=Db::table('htx_member')->insertGetId($data);
                        if($res){
                        $result = array(
                            'code' => 1,
                            'message' => '申請成功!',
                        );
                        }else{
                             $result = array(
                                    'code' => 0,
                                    'message' => '申請錯誤!'
                                );
                        }
                    }else{
                        $error=$file->getError();
                        $result = array(
                                    'code' => 0,
                                    'message' => $error
                                );
                    }
                
                }else{
                    $result = array(
                                'code' => 0,
                                'message' => '請上傳圖片!'
                            );
                }


                return json($result);

    }