| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- // 新正式(未审批)
- const apiHost = 'https://fohowapi.hiwavo.com/'
- // 测试
- // const apiHost = 'https://tfohowapi.hiwavo.com/'
- function $get (url, params, success, fail) {
- wx.showNavigationBarLoading()
- var session = wx.getStorageSync('lbt_session_key')
- var token = wx.getStorageSync('lbt_token_key')
- // console.log('nowtoken',token);
- wx.request({
- url: apiHost + url,
- data: params,
- method: 'GET',
- header: {
- "content-type": "application/json",
- "terminal": 'mini-program',
- // "Cookie": 'fohow_sid=' + session
- "Authorization":"Bearer " + token
- },
- success: function (res) {
- callBack(res, success, fail)
- },
- fail: function (res) {
- callBack(res)
- },
- complete: function (res) {
- wx.hideNavigationBarLoading()
- }
- })
- }
- function $post (url, params, success, fail) {
- wx.showNavigationBarLoading()
- var session = wx.getStorageSync('lbt_session_key');
- var token = wx.getStorageSync('lbt_token_key');
- wx.request({
- url: apiHost + url,
- data: params,
- method: 'POST',
- header: {
- "content-type": "application/x-www-form-urlencoded",
- "terminal": 'mini-program',
- // "Cookie": 'fohow_sid=' + session
- "Authorization":"Bearer " + token
- },
- success: function (res) {
- callBack(res, success, fail)
- },
- fail: function (res) {
- },
- complete: function (res) {
- wx.hideNavigationBarLoading()
- }
- })
- }
- function $put (url, params, success, fail) {
- wx.showNavigationBarLoading()
- var session = wx.getStorageSync('lbt_session_key');
- var token = wx.getStorageSync('lbt_token_key');
- wx.request({
- url: apiHost + url,
- data: params,
- method: 'PUT',
- header: {
- "content-type": "application/x-www-form-urlencoded",
- "terminal": 'mini-program',
- // "Cookie": 'fohow_sid=' + session
- "Authorization":"Bearer " + token
- },
- success: function (res) {
- callBack(res, success, fail)
- },
- fail: function (res) {
- },
- complete: function (res) {
- wx.hideNavigationBarLoading()
- }
- })
- }
- function $del (url, params, success, fail) {
- wx.showNavigationBarLoading()
- var session = wx.getStorageSync('lbt_session_key');
- var token = wx.getStorageSync('lbt_token_key');
- wx.request({
- url: apiHost + url,
- data: params,
- method: 'DELETE',
- header: {
- "content-type": "application/x-www-form-urlencoded",
- "terminal": 'mini-program',
- // "Cookie": 'fohow_sid=' + session,
- "Authorization":"Bearer " + token
- },
- success: function (res) {
- callBack(res, success, fail)
- },
- fail: function (res) {
- },
- complete: function (res) {
- wx.hideNavigationBarLoading()
- }
- })
- }
- function callBack (res, success, fail) {
- if (res.statusCode === 200) {
- success(res)
- } else {
- if (fail) {
- fail()
- }
- switch (res.data.err_code) {
- case 'userNeedLogin':
- var pages = getCurrentPages()
- console.log(pages)
- var currentPage = pages[pages.length-1]
- var url = currentPage.route
- var options = currentPage.options
- var params = ''
- console.log(url)
- for (let a in options) {
- params += a + '=' +options[a] + '&'
- }
- if (params) {
- url = '/' + url + '?' + params
- } else {
- url = '/' + url
- }
- console.log(url)
-
- // wx.showToast({
- // title: 'getApp().globalData.try_login'+getApp().globalData.try_login,
- // icon: 'none',
- // duration: 2000
- // })
- // return false;
-
- if (getApp().globalData.try_login) {
- getApp().globalData.try_login = false
- // wx.showToast({
- // title: '尝试登陆失败,请到个人中心进行授权登陆',
- // icon: 'none',
- // duration: 2000
- // })
- // setTimeout(function(){
- // wx.switchTab({
- // url: '/pages/user/all/all'
- // })
- // },1000)
-
- //新token
- wx.login({
- success: res => {
- getApp().globalData.try_login = true;
- pageLogin(res, url)
- }
- })
- } else {
- wx.login({
- success: res => {
- getApp().globalData.try_login = true;
- pageLogin(res, url)
- }
- })
- }
- break
- default:
- wx.showToast({
- title: res.data.err_msg,
- icon: 'none',
- duration: 2000
- })
- }
- }
- }
- // 登陆接口
- function login (res) {
- var url = 'xcx/login'
- var invite = wx.getStorageSync('invite');
- var params = {
- code: res.code,
- invite_id: invite//新token模式
- }
- var success = function (res) {
- wx.setStorageSync('lbt_session_key', res.data.session_key)
- wx.setStorageSync('lbt_session_time', Date.now())
- wx.setStorageSync('lbt_token_key', res.data.token)
- // check()
- console.log('login:id=' + res.data.wx_user.id)
- getApp().globalData.wx_id = res.data.wx_user.id;
- }
- $get(url, params, success)
- }
- // 获取授权信息
- function check () {
- var url = 'v1/user/check'
- var params = {
- }
- var success = function (res) {
- console.log('login:id=' + res.data.wx_user_id)
- getApp().globalData.wx_id = res.data.wx_user_id;
- }
- $get(url, params, success)
- }
- function getAuthorize () {
- wx.getSetting({
- success: res => {
- var auth = res.authSetting
- if (!auth['scope.userInfo']) {
- setAuthorize()
- }
- }
- })
- }
- // 主动请求授权
- function setAuthorize () {
- wx.authorize({
- scope: 'scope.userInfo',
- success: res => {
- sendInfo()
- },
- fail: res => {
- getAuthorizePopup()
- }
- })
- }
- // 发送个人信息给后端
- function sendInfo (a, channel, invite, that) {
- // var userInfo = a.detail;
-
- var userInfo = a;//新授权方式
- var str = JSON.stringify(userInfo)
- updateWxUserInfo(str, channel, invite, that)
- }
- // 显示未授权提示弹框
- function getAuthorizePopup () {
- wx.showModal({
- title: '用户未授权',
- content: '如需正常使用凤凰菁选商城,请按确定并在授权管理中选中“用户信息”,然后点按确定。最后再重新进入小程序即可正常使用。',
- showCancel: false,
- success: function (res) {
- if (res.confirm) {
- openAuthorize()
- }
- }
- })
- }
- // 进入授权界面
- function openAuthorize () {
- wx.openSetting({
- success: function (res) {
- var auth = res.authSetting
- if (!auth['scope.userInfo']) {
- // getAuthorizePopup()
- } else {
- sendInfo()
- }
- }
- })
- }
- // 更新个人信息
- function updateWxUserInfo (val, channel, invite, that) {
- var url = 'xcx/authorize'
- var params = {
- userinfo: val,
- channel: channel,
- invite_id: invite
- }
- var success = function (res) {
- wx.hideLoading()
- that.setData({
- popup: false
- })
- that.getVersion(500)
- }
- $post(url, params, success)
- }
- function pageLogin (res, str) {
- var url = 'xcx/login'
- var params = {
- code: res.code
- }
- var success = function (res) {
- console.log('pageLogin',res);
- res = res.data;
- if(res.wx_user){
- // console.log('newtoken---',res.token);
- wx.setStorageSync('lbt_session_key', res.session_key)
- wx.setStorageSync('lbt_token_key', res.token)
- wx.setStorageSync('lbt_session_time', Date.now())
- wx.redirectTo({
- url: str
- })
- }else{
- wx.showToast({
- title: '尝试登陆失败,请到个人中心进行授权登陆',
- icon: 'none',
- duration: 2000
- })
- }
- }
- $get(url, params, success)
- }
- function share (val, timline) {
- var url = ''
- var image = ''
- var title = val.title || '欢迎来到凤凰菁选商城'
- if (val.path) {
- if (val.path.indexOf('?') > -1) {
- url = val.path + '&invite=' + getApp().globalData.wx_id
- } else {
- url = val.path + '?invite=' + getApp().globalData.wx_id
- }
- } else {
- url = '/pages/start/start?invite=' + getApp().globalData.wx_id
- }
- if (getApp().globalData.wx_id === 0) {
- title = '·' + title
- }
- if (val.imageUrl === undefined) {
- image = 'http://fohow.oss-cn-shenzhen.aliyuncs.com/xcx/share/sharemain.jpg'
- } else {
- image = val.imageUrl
- }
- console.log(title)
- if(timline){
- var query = url.split('?')[1];
- return {
- title: title,
- path: url,
- imageUrl: image,
- query: query,
- success: function (res) {
- shareInfo(val)
- }
- }
- }else{
- return {
- title: title,
- path: url,
- imageUrl: image,
- success: function (res) {
- shareInfo(val)
- }
- }
- }
-
- }
- function $uploadFile(url, name, file, params, success, fail){
- wx.showNavigationBarLoading()
- var session = wx.getStorageSync('lbt_session_key');
- var token = wx.getStorageSync('lbt_token_key');
- wx.uploadFile({
- url: apiHost + url, //仅为示例,非真实的接口地址
- filePath: file,
- name: name,
- formData: params,
- header: {
- "content-type": "application/x-www-form-urlencoded",
- "terminal": 'mini-program',
- // "Cookie": 'fohow_sid=' + session,
- "Authorization":"Bearer " + token
- },
- success(res) {
- callBack(res, success, fail)
- },
- fail(res) {
- console.log('err',res)
- },
- complete(res) {
- wx.hideNavigationBarLoading()
- }
- })
- }
- function shareInfo (val) {
- var url = 'v1/share/info'
- var params = {
- sc: val.sc || 'xcx',
- ri: val.ri || 0,
- rp: val.rp || 0,
- to: 'group'
- }
- var success = function (val) {
- }
- $post(url, params, success)
- }
- module.exports = {
- $post: $post,
- $put: $put,
- $get: $get,
- $del: $del,
- login: login,
- getAuthorize: getAuthorize,
- updateWxUserInfo: updateWxUserInfo,
- sendInfo: sendInfo,
- share: share,
- $uploadFile:$uploadFile
- }
|