123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- const {
- DEBUG,
- CODE_NOT_LOGIN
- } = require('./c.js');
- const {
- api_get_openid,
- api_get_unionid,
- api_wx_login
- } = require('./api.js');
- const md5 = require('./md5.js');
- const util = require('./util.js');
- /**
- * 网络请求对象
- */
- const net = {
- commonParams: () => {
- let systemInfo = util.getStorageSync('systemInfo');
- if (!systemInfo) {
- systemInfo = wx.getSystemInfoSync();
- if (!systemInfo) {
- wx.setStorageSync('systemInfo', systemInfo);
- }
- }
- let deviceId = 'weapp';
- let systemVersion = 'weapp';
- if (systemInfo) {
- let {
- brand,
- model,
- system
- } = systemInfo;
- if (brand || model) {
- deviceId = brand + model;
- }
- if (system && 'string' === typeof (system)) {
- systemVersion = system;
- }
- }
- let params = {
- language: 'cn', //语言参数
- deviceType: 'weapp', //接口调用来源参数
- deviceId: deviceId, //设备唯一标识码
- apiVersion: '1.0.3', //api版本号
- systemVersion: systemVersion //操作系统及版本号
- };
- let loginUserInfo = util.getStorageSync('userInfo');
- if (loginUserInfo) {
- let userToken = loginUserInfo.token;
- if (userToken) {
- params.token = userToken; //用户登录凭证token
- }
- }
- let citySelectObj = util.getStorageSync('citySelectObj');
- if (citySelectObj) {
- citySelectObj.cityId && (params.cityId = citySelectObj.cityId) //用户选择的城市id
- citySelectObj.schoolId && JSON.stringify(citySelectObj.schoolId).length && (params.schoolId = citySelectObj.schoolId) //用户选择的学校id
- }
- return params;
- },
- paramsSign: function (params, url) {
- /*参数签名*/
- let key = 'd9ce9fe030776727e85442d033d1be84';
- if (url && url.indexOf('/common/') !== -1) {
- key = 'b15681f74461af9642ab5356fba7962b';
- }
- let allKeys = [],
- paramsLower = {},
- sArr = [],
- res = '';
- for (let k in params) {
- let kLower = k;
- allKeys.push(kLower);
- paramsLower[kLower] = params[k];
- }
- allKeys.sort();
- allKeys.forEach((v, i, a) => {
- sArr.push(`${v}=${paramsLower[v]}`);
- });
- sArr.push(`key=${key}`);
- res = sArr.join('&');
- res = md5(res);
- return res.toLowerCase();
- },
- //isDealFail是否手动处理异常,true or false
- req: function (options, isDealFail) { //get或post请求
- const params = {
- ...options
- }
- if (!params.url) {
- wx.showToast({
- title: '请设置请求url',
- icon: 'none',
- duration: 2000
- });
- return Promise.reject();
- }
- // 过滤空值
- for (const key in params.data) {
- if (util.isUndef(params.data[key])) {
- delete params.data[key]
- }
- }
- return new global.Promise((resolve, reject) => {
- params.data = Object.assign({}, net.commonParams(), params.data);
- params.data.signToken = net.paramsSign(params.data, params.url);
- wx.request({
- url: params.url,
- data: params.data || null,
- header: {
- 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
- },
- method: params.method || 'POST',
- dataType: params.dataType || 'json',
- success: function (res) {
- const {
- code,
- msg,
- data
- } = res.data;
- if (0 === code) { //ok
- return resolve(data);
- } else if (CODE_NOT_LOGIN === code) { //未登录
- DEBUG && console.log(`Request for ${params.url} exception.--->${JSON.stringify(msg)}`);
- wx.clearStorage();
- wx.showToast({
- title: '您的用户信息已失效,请重新登录操作',
- icon: 'none',
- duration: 3000,
- success: () => {
- wx.clearStorageSync();
- wx.reLaunch({
- url: '/pages/index/index'
- });
- },
- });
- } else {
- DEBUG && console.log(`Request for ${params.url} exception.--->${JSON.stringify(msg)}`);
- if (isDealFail !== true) {
- wx.showToast({
- title: msg || 'error',
- icon: 'none',
- duration: 2000
- });
- }
- return reject(res.data);
- }
- },
- fail: function (e) {
- DEBUG && console.log(`Request for ${params.url} fail.--->${JSON.stringify(e)}`);
- if (isDealFail !== true) {
- wx.showToast({
- title: (e && e.errMsg) || '',
- icon: 'none',
- duration: 2000
- });
- }
- reject(e);
- },
- complete: function (res) {
- DEBUG && console.log(`Request for ${params.url} complete.--->${JSON.stringify(res)}`);
- }
- });
- })
- },
- getOpenid: function (code, next) { //获取openid
- let params = {};
- params.url = api_get_openid;
- params.data = {
- code: code,
- program: "hooliHome"
- };
- net.req(params).then((res) => {
- let {
- openid
- } = res;
- wx.setStorageSync('openid', openid);
- next && next(openid);
- });
- },
- getUnionid: function (openid, encryptedData, iv, rawData, signature, next) { //获取unionid
- let params = {};
- params.url = api_get_unionid;
- params.data = {
- openid,
- encryptedData,
- iv,
- rawData,
- signature,
- program: 'hooliHome'
- };
- net.req(params, true).then((res) => {
- let {
- unionid
- } = res;
- if (unionid) {
- wx.setStorageSync('unionid', unionid);
- next && next(res);
- } else {
- wx.showToast({
- title: '网络不稳定,请重新操作',
- icon: 'none',
- duration: 2000
- });
- }
- }, (e) => {
- wx.showToast({
- title: '网络不稳定,请重新操作',
- icon: 'none',
- duration: 2000
- });
- });
- },
- wxLogin: function (next) { //点击注册登录操作
- wx.checkSession({
- success: () => { //session_key 未过期,并且在本生命周期一直有效
- let openid = util.getStorageSync('openid');
- if (openid) {
- next && next(openid);
- } else {
- wx.login({
- success: (res) => {
- let {
- code
- } = res;
- net.getOpenid(code, (openid) => {
- next && next(openid);
- });
- }
- });
- }
- },
- fail: () => { //session_key 已经失效,需要重新执行登录流程,让服务端获取新的session_key
- wx.login({
- success: (res) => {
- let {
- code
- } = res;
- net.getOpenid(code, (openid) => {
- next && next(openid);
- });
- }
- });
- }
- });
- },
- login: function (unionid, countryId, mobile, smsCode, next) { //微信绑定手机号注册登录
- let params = {};
- params.url = api_wx_login;
- params.data = {
- unionid,
- countryId,
- mobile,
- smsCode
- };
- net.req(params).then((res) => {
- wx.setStorageSync('userInfo', res.userInfo);
- next && next(res);
- });
- }
- }
- module.exports = net;
|