123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- export let api_url = null;
- export function get(url,params, success = noop, fail = noop) {
- return http(url,'GET',params, success,fail)
- }
- export function post(url,params, success = noop, fail = noop) {
- return http(url,'POST',params, success,fail)
- }
- function noop() {}
- function http(url,method,params, success, fail) {
- const type = wx.getStorageSync('env') || 'dev'; // 测试环境
- // const type = wx.getStorageSync('env') || 'ijolijoli'; // 正式环境
- switch(type) {
- case 'dev':
- api_url = 'https://store.test-api.ijolijoli.com/'
- break;
- default:
- api_url = 'https://api.ijolijoli.com/'
- break;
- }
- return wx.request({
- url: `${api_url}${url}`,
- method: method,
- data: params,
- header: {
- token: wx.getStorageSync('token') || '',
- },
- success: ({
- data,
- statusCode,
- header
- }) => {
- console.log(`==============${url}`,data)
- if(data.code == 200) {
- success(data, header);
- } else if(data.code == 201) {
- wx.showToast({
- title: data.msg,
- icon: 'none'
- })
- fail(data, header)
- } else if(data.code == 400) {
- console.log('登录失败')
- wx.showToast({
- title: data.msg,
- icon: 'none'
- })
- wx.reLaunch({
- url: '/pages/login/login',
- })
- // login()
- }
- },
- fail() {
- wx.showToast({
- title: '服务器异常 请稍后再试',
- icon: 'none'
- })
- }
- })
- }
- export function login(opts) {
- wx.login({
- success: res => {
- post('api/login',{
- js_code: res.code
- },(res) => {
- if(res.code == 200) {
- wx.setStorageSync('token', res.data.token);
-
- if(res.data.status == 1) {
- // wx.setStorageSync('userInfo', {
- // avatar_url: res.data.avatar_url,
- // mobile: res.data.mobile,
- // nickname: res.data.nickname,
- // uid: res.data.uid,
- // });
- getUser()
- }
- if(res.data.status == 0) {
- wx.reLaunch({
- url: '/pages/login/login',
- })
- // if(opts && opts.path == 'pages/deviceLogin/deviceLogin') {
- // wx.navigateTo({
- // url: '/pages/login/login',
- // })
- // } else {
- // wx.reLaunch({
- // url: '/pages/login/login',
- // })
- // }
- return;
- }
- }
- })
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- }
- })
- }
- /**
- * 获取用户信息
- * api/user
- */
- function getUser() {
- get('api/user',{},(res) => {
- if(res.data) {
- wx.setStorageSync('userInfo',res.data);
- }
- console.log(res)
- })
- }
|