214 lines
5.7 KiB
TypeScript
214 lines
5.7 KiB
TypeScript
import Request_ from '../../api/request';
|
||
|
||
|
||
class _init {
|
||
|
||
// 登录完成回调(包含处理完成用户携带数据)
|
||
UserLoginInitCollback = undefined;
|
||
userInfor = {};
|
||
// 微信登录之后获取到 参数
|
||
openid : String = "";
|
||
session_key : string = "";
|
||
// Wechat = {};
|
||
// 支付宝登录之后获取到的参数
|
||
Apple = {
|
||
access_token: "0",
|
||
alipay_user_id: "0",
|
||
auth_start: "0",
|
||
expires_in: 0,
|
||
re_expires_in: 0,
|
||
refresh_token: "",
|
||
user_id: "",
|
||
};
|
||
code = '';
|
||
pay_type : number = 0;
|
||
provider : "weixin" | "qq" | "sinaweibo" | "xiaomi" | "apple" | "univerify" = 'weixin';
|
||
// 进入来源 Scan | Half
|
||
referrerInfo:'Scan' | 'Half';
|
||
// 初始参数
|
||
__programParameter__ = null;
|
||
/** 请求参数 */
|
||
APIoptions = {};
|
||
constructor(parameters) {
|
||
// #ifdef MP-ALIPAY
|
||
this['pay_type'] = 2;
|
||
// #endif
|
||
// #ifdef MP-WEIXIN
|
||
this['pay_type'] = 1;
|
||
// #endif
|
||
/**
|
||
* 初始化来源
|
||
*/
|
||
// parameters.referrerInfo = {
|
||
// path:"pages/index/index",
|
||
// quer:{},
|
||
// scene:1037,
|
||
// referrerInfo:{"appId":"wxc8e2e1d28d15d2e3"},
|
||
// extraData:{
|
||
// "thrust_code":"4871020984",
|
||
// code:"49750683"
|
||
// },
|
||
// mode:"embedded",
|
||
// apiCategory:"embedded"
|
||
// };
|
||
if(parameters?.referrerInfo && Object.keys(parameters['referrerInfo'])['length']){
|
||
this.referrerInfo = 'Half';
|
||
/**
|
||
* 初始化参数
|
||
*/
|
||
this.__programParameter__ = parameters['referrerInfo']['extraData'];
|
||
this.APIoptions = this['__programParameter__'];
|
||
}else{
|
||
this.referrerInfo = 'Scan';
|
||
/**
|
||
* 初始化参数
|
||
*/
|
||
this.__programParameter__ = parameters;
|
||
/**处理拿到的初始化参数 */
|
||
this.InitParam();
|
||
|
||
};
|
||
|
||
|
||
}
|
||
|
||
/** 获取来源 */
|
||
get source(){
|
||
return this['referrerInfo'];
|
||
}
|
||
|
||
|
||
// 推送播报
|
||
// Pushbroadcast(data){
|
||
// if(getApp()['globalData']['Pay_Status'] == 2) return;
|
||
// Request_.GET({ url:"/MiniProgram/Order/cancelPaymentSound", data });
|
||
// }
|
||
|
||
Login() {
|
||
let url : string = null;
|
||
// #ifdef MP-ALIPAY
|
||
url = '/MiniProgram/Api/alipayCodeLogin';
|
||
this['pay_type'] = 2;
|
||
// #endif
|
||
// #ifdef MP-WEIXIN
|
||
url = '/MiniProgram/Api/wechatCodeLogin';
|
||
this['pay_type'] = 1;
|
||
console.log(uni.login,this['provider'],'执行登录');
|
||
// #endif
|
||
uni.login({
|
||
provider: this['provider'],
|
||
success: loginRes => {
|
||
console.log(loginRes['code'], 'code---');
|
||
if (loginRes.code) {
|
||
let data = null;
|
||
// #ifdef MP-ALIPAY
|
||
console.log(loginRes['code'], 'apple:code');
|
||
data = { code: loginRes['code'] };
|
||
// #endif
|
||
|
||
// #ifdef MP-WEIXIN
|
||
console.log(loginRes['code'], 'WeChat:code');
|
||
data = { code: loginRes['code'], type: 1 };
|
||
// #endif
|
||
data['thrust_code'] = this['options']['thrust_code'] || '';
|
||
data['invite_code'] = this['options']['invite_code'] || '';
|
||
// return
|
||
|
||
console.log(data,'data')
|
||
this.userInfor = data;
|
||
Request_.GET({ url, data }).then(res => {
|
||
this.userInfor = {...this['userInfor'],...res['data']}
|
||
uni.setStorageSync('token', res['data']['token']);
|
||
console.log(res, '登录信息');
|
||
// #ifdef MP-ALIPAY
|
||
// let { openid, session_key } = res['data'];
|
||
this['Apple'] = res['data'];
|
||
// #endif
|
||
// #ifdef MP-WEIXIN
|
||
let { scan_openid, session_key } = res['data'];
|
||
this['openid'] = scan_openid;
|
||
this['session_key'] = session_key;
|
||
// #endif
|
||
console.log(this['UserLoginInitCollback'], 'UserLoginInitCollback');
|
||
if (this['UserLoginInitCollback'] != null && typeof this['UserLoginInitCollback'] == 'function') {
|
||
this.UserLoginInitCollback();
|
||
}
|
||
})
|
||
} else {
|
||
uni.showToast({
|
||
title: '登录失败!未获取到code',
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
}
|
||
},
|
||
fail:error=>{
|
||
console.log(error,'error')
|
||
}
|
||
});
|
||
}
|
||
|
||
// 支付时获取指定平台字段
|
||
get Getopenid() {
|
||
// #ifdef MP-ALIPAY
|
||
return this['Apple']['user_id'];
|
||
// #endif
|
||
// #ifdef MP-WEIXIN
|
||
return this['openid'];
|
||
// #endif
|
||
}
|
||
|
||
get options(){
|
||
return this['APIoptions']
|
||
}
|
||
|
||
// 解析小程序初始参数
|
||
InitParam() {
|
||
console.log(this['__programParameter__'],this['source'],'__programParameter__');
|
||
// 扫码进入
|
||
if (this['__programParameter__']?.query?.q || this['__programParameter__']?.query?.scene || this['__programParameter__']?.query?.qrCode) {
|
||
let searchParamToObj = '';
|
||
searchParamToObj = this['__programParameter__']['query']?.q || this['__programParameter__']['query']?.scene || this['__programParameter__']['query']?.qrCode;
|
||
console.log(searchParamToObj, 'searchParamToObj');
|
||
const obj = {};
|
||
const Para = this.escape2Html(decodeURIComponent(searchParamToObj));
|
||
const _i = Para.indexOf('?');
|
||
console.log(Para, _i, 'Para');
|
||
if (~_i) {
|
||
const _U = Para.slice(_i + 1, Para['length']);
|
||
const _M = _U.split('&');
|
||
|
||
for (let i = 0; i < _M['length']; i++) {
|
||
const index = _M[i].indexOf("=");
|
||
console.log(index, 'index');
|
||
const KEY = _M[i].substring(0, index);
|
||
const VAL = _M[i].substring(index + 1, _M[i].length);
|
||
obj[KEY] = VAL;
|
||
};
|
||
this.APIoptions = obj;
|
||
|
||
} else {
|
||
this.APIoptions = {};
|
||
}
|
||
} else {
|
||
// 商户端扫码进入
|
||
try{
|
||
this.APIoptions = this['__programParameter__']['query'];
|
||
}catch(e){
|
||
this.APIoptions = {};
|
||
//TODO handle the exception
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
// 字符转义
|
||
escape2Html(str) {
|
||
var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
|
||
return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; });
|
||
};
|
||
|
||
}
|
||
|
||
|
||
export default _init; |