WeChat--API


獲取用戶信息

fetchinfo: function(){
	var that = this
	wx.getUserInfo({
	  withCredentials: true,
	  lang: '',
	  success: function(res) {
		that.setData({
		  name:res.userInfo.nickName,
		  path:res.userInfo.avatarUrl
		})
	  },
	  fail: function(res) {},
	  complete: function(res) {},
	})
  },

獲取地理位置

getLocalPath:function(){
	var that = this
	wx.chooseLocation({
	  success: function(res) {
		that.setData({
		  localPath:res.address
		})
	  },
	  fail: function(res) {},
	  complete: function(res) {},
	})
  },

上傳圖片,儲存在緩沖中

uploadImage:function(){
	var that = this
	wx.chooseImage({
	  count: 9,
	  sizeType: ['original', 'compressed'],
	  sourceType: ['album', 'camera'],
	  success: function(res) {
		that.setData({
		  imageList:that.data.imageList.concat(res.tempFilePaths)
		})
	  },
	  fail: function(res) {},
	  complete: function(res) {},
	})
  },

頁面跳轉

'''

# 路由5種跳轉方式
(1)wx.switchTab :只能跳轉到導航頁,並關閉其他的導航頁
(1)wx.reLaunch :關閉所有頁面,打開到應用內的某個頁面
(1)wx.redirectTo :關閉當前頁面,跳轉到應用內的某個頁面。但是不允許跳轉到 導航 頁面
(1)wx.navigateTo :只保留當前頁面,跳轉到應用內的某個頁面。但是不能跳到 tabbar 頁面(最多10層)
(1)wx.navigateBack :返回上一頁,可以返回多級頁面

'''

click_acution:function(e){
	var nid = e.currentTarget.dataset.nid;
	wx.navigateTo({
	  url: '/pages/redirect/redirect?id=' + nid,
	  success: function(res) {},
	  fail: function(res) {},
	  complete: function(res) {},
	})
  },
## 跳轉到指定頁面后自動觸發指定頁面的js文件中的的onLoad函數,option接收傳過來的參數

```
Page({
/**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {
	console.log(options);
  }
})
```

彈窗接口:wx.showToast

wx.showToast({
  title: '',
  icon: '',
  image: '',
  duration: 0,
  mask: true,
  success: function(res) {},
  fail: function(res) {},
  complete: function(res) {},
})

wxml頁面

<view>手機號:</view>
<input value="{{phone}}" placeholder="請輸入手機號" bindinput="getPhone"></input>

<view>驗證碼:<view bindtap="messageCode">點擊獲取驗證碼 </view></view>
<input value="{{code}}" placeholder="請輸入驗證碼" bindinput="getCode"></input>

<button bindtap="login">登錄</button>

手機格式驗證:

  * 發送短信驗證碼
  messageCode: function () {
	// 手機長度限制
	if (this.data.phone.length != 11) {
	  // 彈窗
	  wx.showToast({
		title: '手機號長度錯誤',
		icon: "none", // loading/success/none
	  })
	  return;
	}


	// 正則匹配手機格式
	var reg = /^(1[3|4|5|6|7|8|9])\d{9}$/;
	if (!reg.test(this.data.phone)) {
	  wx.showToast({
		title: '手機號格式錯誤',
		icon: "none", // loading/success/none
	  })
	  return;
	}

	wx.request({
	  url: 'http://127.0.0.1:8000/api/message/',
	  data: { phone: this.data.phone },
	  method: 'GET',
	  success: function (res) {
		console.log(res);
	  }
	})
  },

API接口發送請求:wx.request

wx.request({
  url: '',
  data: '',
  header: {},
  method: 'GET',
  dataType: 'json',
  responseType: 'text',
  success: function(res) {},
  fail: function(res) {},
  complete: function(res) {},
})

示例:手機號驗證碼登陸

	* 用戶登錄

  login: function () {
	console.log(this.data.phone, this.data.code);
	// 將手機號和驗證碼發送到后端,后端進行登錄。
	wx.request({
	  url: 'http://127.0.0.1:8000/api/login/',
	  data: { phone: this.data.phone, code: this.data.code },
	  method: 'POST',
	  success: function (res) {
		console.log(res);
	  }
	})
  },

選擇框

wx.showModal({

  title: '提示',
  content: '這是一個模態彈窗',
  success (res) {
    if (res.confirm) {
      console.log('用戶點擊確定')
    } else if (res.cancel) {
      console.log('用戶點擊取消')
    }
  }
})

加載中

wx.showLoading({

  title: '加載中',
})

# 設置超時時間
setTimeout(function () {
  wx.hideLoading()
}, 2000)

分享

onLoad: function (options) {
  this.getDate(options.id)
  wx.showShareMenu({
	// 要求小程序返回分享目標信息
	withShareTicket: true
  })
},
/* 轉發*/
onShareAppMessage: function (ops) {
  if (ops.from === 'button') {
	// 來自頁面內轉發按鈕
	console.log(ops.target)
  }
  return {
	title: '轉發dom',
	path: `pages/index/index`,
	success: function (res) {
	  // 轉發成功
	  console.log("轉發成功:" + JSON.stringify(res));
	  var shareTickets = res.shareTickets;
	},
	fail: function (res) {
	  // 轉發失敗
	  console.log("轉發失敗:" + JSON.stringify(res));
	}
  }
}

本地緩沖

function wx.setStorageSync(key: string): void

將 data 存儲在本地緩存中指定的 key 中,會覆蓋掉原來該 key 對應的內容,這是一個同步接口


function wx.getStorageSync(key: string): void

從本地緩存中同步獲取指定 key 對應的內容


function wx.getStorageSync(key: string): void

從本地緩存中同步獲取指定 key 對應的內容

本地緩沖示例:將userinfo緩沖到本地---app.js

//app.js

App({
  onLaunch: function() {
	var userInfo = wx.getStorageSync('userInfo');
	if (userInfo) {
	  this.globalData.userInfo = userInfo;
	}
  },
  globalData: {
	userInfo: null
  },
  initUserInfo: function(tokenInfo, userInfo) {
	var info = {
	  nickName: userInfo.nickName,
	  avatarUrl: userInfo.avatarUrl,
	  token: tokenInfo.token,
	  phone: tokenInfo.phone
	};
	this.globalData.userInfo = info
	wx.setStorageSync('userInfo', info);
  },
  logoutUserInfo:function(){
	wx.removeStorageSync('userInfo');
	this.globalData.userInfo=null;
  }
})

打電話

<button bindtap="onClickCall">
  <image src="/static/images/icon/phone_contact_icon_show.png"></image>
</button>

onClickCall:function(){
wx.makePhoneCall({
  phoneNumber: '15000000000'
})
},


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM