//在utils里面寫公共方法 比如說這個頁面為path.js 頁面 const prefix = 'https://dev.cdsoft.work/ct'; const getDepartmentList = prefix + '/mobile/getDepartmentList'; //員工注冊 const employeeRegister=prefix + "/mobile/employeeRegister"; //伯爵訂單--登錄 1刷新 2.重新申請 審核通過 const getExamineStatusById = prefix +'/mobile/getExamineStatusById' //暴露接口,拋出 export { login, getDepartmentList, employeeRegister, getExamineStatusById // getUserInfo }
//需要請求的方式 比如這個寫在util.js 里面 /*get 請求 post 請求 */ /**發送GET請求**/ function getRequest(url, data, callback) { request(url, data, 'get', null, true, callback); } /**發送POST請求**/ function postRequest(url, data, callback) { request(url, data, 'post', null, true, callback); } /** url:請求地址 data:請求內容,對象格式 get /post 請求方式 header 自定義請求 * needAuth 是否帶上token 默認為true * callback 回調 **/ function request(url, data, type, header, needAuth, callback) { const app = getApp(); if (!header) { header = { 'content-type': 'application/json', }; } if (needAuth != false) { const token = app.globalData.token; header.token = token; } wx.request({ url: url, method: type, data: data, header: header, success: function (res) { if (!res.data.success && res.data.message == 'token error') { // wx.reLaunch({ // url: `/pages/login/login?isTokenError=true`, // }) } callback && callback(res) } }) } //暴露接口 module.exports = { formatTime: formatTime, postRequest: postRequest, getRequest: getRequest }
/*這個頁面所需要對接數據 **/ /**需要引入以上兩個頁面----> 后端***/ import {getDepartmentList,employeeRegister} from '../../utils/path.js' //大括號里面代表 這個頁面與后台對接參數 import{postRequest} form '../../utils/utils.js' //這個postRequest代表請求數據的方式 formsubmit:function(e){ console.log(e); const phone = this.data.phoneValue; const password = this.data.passValue; let bool = apiname.rePhone(phone); if(!bool){ wx.showToast({ title: '手機號輸入錯誤', icon: 'none' }) return false; } postRequest(login, { "phone": phone, "password": password },res => { if (res.data.success){ //登錄成功 -- 跳轉下個頁面 getApp().globalData.token = res.data.data.token }else{ //登錄失敗 wx.showToast({ title: res.data.message, icon: 'none' }) } }); },