小程序开发踩坑记录

  • Post author:
  • Post category:小程序


官方文档:

https://developers.weixin.qq.com/miniprogram/dev/framework/view/two-way-bindings.html

1.微信授权相关

1.appid的比对失败(”missing code, hints: [ req_id: wEKAPiqNe-9QVxxa ]”)



前端比对appid,后端比对appsecret和appid。且注意js_code一次性使用的,贴一下授权属性

<button wx:if="{{!userInfo}}" open-type='getUserInfo' bindgetuserinfo="getUserInfo" >获取授权</button>
<van-button wx:if="{{userInfo}}" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" color="#4BC4B2" type="primary">授权手机号</van-button>

2.在做手机授权先确保小程序已经审核完成。

3.授权手机号步骤:用户端调用wx.login(获取js_code)=>  发给服务端(使用js_code获取open_id)=>用户点击授权按钮(得到加密字段)=> 一起发给服务端解密得到json数据包

2.封装接口请求函数

因为这次做的项目不是大型项目,就没有使用promiss包装,采用传入回调函数的方法处理结果。

export function request( url, data = {}, method = 'GET', callBack, header = {}) {
  console.log()
  wx.request({
    url: baseUrl + url,
    method: method,
    data:data,
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
      ...header,
		  "Authorization": `Bearer ${token}`,
    },
    success: function (res) {
      checkStatus(res,callBack);
    },
    fail: function (res) {

    }
  })
}

function checkStatus(res,callBack) {
	if (res.statusCode === 200) {
    if(res.data.rc !== 0 && res.data.msg) {
      console.log(res.data,'res.data')
      Toast.fail(res.data.msg)
    }else{
      callBack(res.data.data)
    }
	} else if (res.statusCode >= 400 && res.statusCode < 500) {
    if(res.statusCode === 401) {
      Toast.fail('token失效')
    }
	} else if (res.statusCode >= 500 && res.statusCode < 600) {
    Toast.fail('服务器故障!')
	}
}

3.三种跳转页面的方式

1.wx.navigateTo({ url: ‘../aaa/aaaa’})    //常规跳转

2.wx.switchTab({ url: ‘../aaa/aaaa’})      //跳转到tab页面

3.wx.redirectTo({ url: ‘../aaa/aaaa’})     //同一,但是没有返回按钮

4.bindtap优先级极高

在已经使用了disabled的button组件里用bindtap做点击事件还是会生效,改为bind:click



版权声明:本文为weixin_41557291原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。