import * as config from '@/config' import WXBizDataCrypt from './WXBizDataCrypt.js' let baseUrl = config.def().baseUrlNew // 定义基础请求路径(后端服务器地址) const baseRequest = (_gp, _mt, data = {}, failCallback) => { //异步请求数据 return new Promise(resolve => { // if (!userInfo || !userInfo.accessToken) { // userInfo = uni.getStorageSync('userInfo') // } // let accessToken = userInfo ? userInfo.accessToken : '' let baseUrl = config.def().baseUrlNew uni.request({ url: baseUrl + '/m.api', data: { ...data, _gp, _mt }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', // 'ACCESSTOKEN': accessToken }, success: (res) => { if (res.statusCode === 200) { if (res.data.errno === 200) { resolve(res.data); } else { if (failCallback) { failCallback(res.data) } else { uni.showToast({ title: res.data.errmsg, icon: 'none' }) } } } } }) }) } //带Token请求 const TokenRequest = (method, url, data, header) => { var contentheader = 'application/json' if (header) { contentheader = header } let ac_token = ""; uni.getStorage({ key: 'userInfo', success: function(res) { ac_token = res.data.accessToken } }); //此token是登录成功后后台返回保存在storage中的 let DefaultOpts = { url: baseUrl + url, data: data, method: method, header: { 'content-type': contentheader, 'Token': ac_token, } } let promise = new Promise(function(resolve, reject) { uni.request(DefaultOpts).then( (res) => { console.log(JSON.stringify(res[1].data)) if (res[1].data.code == '200' || res[1].data.code == 200) { // 后端返回的状态码100为成功状态,成功则返回请求结果,在app调试时可以通过console.log(JSON.stringify(res[1].data))来查看返回值(以项目实际情况为准) resolve(res[1].data) } if (res[1].data.code == '105' || res[1].data.code == 105) { // 后端返回状态码为105则为未登录状态(以项目实际情况为准) uni.showToast({ icon: 'none', title: '尚未登录', duration: 2000 }); // 尚未登录的逻辑处理 return false } } ).catch( (response) => { reject(response) } ) }) return promise } //微信登录 const wxlogin = () => { let promise = new Promise(function(resolve, reject) { uni.login({ "provider": "weixin", success: function(wxres) { console.log("wxlogin",wxres) baseRequest('commonUserApp', 'commonUserLogin', { loginType: 1, raw: JSON.stringify(wxres) }, failres => { uni.showToast({ icon: "none", title: failres.errmsg, duration: 3000 }); uni.hideLoading() }).then(res => { console.log(res.data) resolve(res.data) }) }, fail: function(err) { reject(err.code) } }) }) return promise } //获取手机号 const getPhone = (e, userInfo) => { let promise = new Promise(function(resolve, reject) { if (e.mp.detail.errMsg === "getPhoneNumber:ok") { let appId = 'wx5d8906c2208c899f' let sessionKey = userInfo.sessionKey let encryptedData = e.mp.detail.encryptedData let iv = e.mp.detail.iv let data ='' try { console.log('解密前 encryptedData: ', encryptedData) console.log('解密前 iv: ', iv) console.log('解密前 appId: ', appId) console.log('解密前 sessionKey: ', sessionKey) data= WXBizDataCrypt.prototype.decryptData(encryptedData, iv, appId, sessionKey) console.log('解密后 data: ', data) resolve(data.phoneNumber) } catch (e) { uni.showToast({ icon: "none", title: e, duration: 3000 }); } } }) return promise } //同步信息 const syncInfo = (userInfo) => { let promise = new Promise(function(resolve, reject) { debugger if (!userInfo.head) userInfo.head = 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/card_head.png' if (!userInfo.nickname) { let code = "" for (var i = 0; i < 6; i++) { code += parseInt(Math.random() * 10); } userInfo.nickname = 'user' + code } baseRequest('commonUserApp', 'edit', { commonUserInfo: JSON.stringify(userInfo) }, failres => { uni.showToast({ icon: "none", title: failres.errmsg, duration: 3000 }); reject(err.code) }).then(res => { resolve(res.data) }) }) return promise } // 处理海报数据 const makeCanvasData = (check_idx,height,posterObj1,theight) => { console.log(height,theight) var dth = (theight - 22) * 2 var h=((height+40)*2) console.log(posterObj1) var posterObj= {} switch (check_idx) { case 0: posterObj={ width: '670rpx', height: h+'rpx', background: '#fff', borderRadius: '16rpx', views:[ { type: 'text', text: posterObj1.title, use:'title', css: { fontSize: '32rpx', color: '#1A2033', fontWeight: 'bold', lineHeight: '45rpx', left: '32rpx', top: '410rpx', width: '606rpx' } }, { type: 'text', text: posterObj1.title1, use:'title1', css: { fontSize: '28rpx', fontWeight: 'bold', color: '#1A2033', lineHeight: '28rpx', left: '32rpx', top: 535+dth+'rpx' } }, { type: 'text', text: '长按或扫描识别二维码', use:'title2', css: { fontSize: '24rpx', color: '#4070FF', lineHeight: '24rpx', left: '32rpx', top: 579+dth+'rpx' } }, { type: 'image', src: posterObj1.image, use:'dt', css: { left: '32rpx', top: '44rpx', width: '606rpx', height: '341rpx', borderRadius: '16rpx' } }, { type: 'image', src: posterObj1.qrcode, use:'qrcode', css: { left: '455rpx', top: 520+dth+'rpx', // transform: 'translate(-50%,0)', width: '240rpx', height: '240rpx', } }, // { // type: 'text', // text: '———— 由云现场提供技术支持 ————', // css: { // left: '0', // top: '863rpx', // width: '100%', // textAlign: 'center', // fontSize: '24rpx', // color: '#989FB3', // lineHeight: '33rpx' // } // } ], } break; case 1: posterObj={ width: '670rpx', height: h+'rpx', background: '#fff', borderRadius: '16rpx', views: [{ type: 'image', use:'bg', src: 'https://s.yun-live.com/images/20210201/39ae4d9d8ad0b1acac7c224e845c641f.png', css: { left: '0', top: '0', width: '100%', height: '100%' } }, { type: 'image', src: posterObj1.image, css: { left: '32rpx', top: '44rpx', width: '606rpx', height: '341rpx', borderRadius: '16rpx' } }, { type: 'text', text: posterObj1.title, use:'title', css: { fontSize: '32rpx', color: '#fff', fontWeight: 'bold', lineHeight: '45rpx', left: '32rpx', top: '420rpx', width: '590rpx' } }, { type: 'text', text:posterObj1.title1, use:'title1', css: { fontSize: '28rpx', fontWeight: 'bold', color: '#D8AB87', lineHeight: '28rpx', left: '32rpx', top: 535+dth+'rpx' } }, { type: 'text', text: '长按或扫描识别二维码', use:'title2', css: { fontSize: '24rpx', color: '#FFFFFF', lineHeight: '24rpx', left: '32rpx', top: 579+dth+'rpx' } }, { type: 'image', src: 'https://s.yun-live.com/images/20210201/d88d56843d43b917e2a28550b2a62723.png', css: { left: '0rpx', top: '90%', width: '103rpx', height: '103rpx', } }, { type: 'image', src: 'https://s.yun-live.com/images/20210201/247736ffd279276b891ec14db8ed0fd0.png', css: { left: '600rpx', top: '50%', width: '56.4rpx', height: '56.4rpx', } }, { type: 'view', use:'qrcode-view', css: { left: '445rpx', top: 520+dth+'rpx', width: '240rpx', height: '240rpx', background: '#fff', } }, { type: 'image', src: posterObj1.qrcode, use:'qrcode', css: { left: '455rpx', top: 530+dth+'rpx', // transform: 'translate(-50%,0)', width: '220rpx', height: '220rpx', } }, ] } break; case 2: posterObj={ width: '670rpx', height: h+'rpx', background: '#fff', borderRadius: '16rpx', views: [{ type: 'image', use:'bg', src: 'https://s.yun-live.com/images/20210201/78f227bd701da20676c9da9166ce3144.png', css: { left: '0', top: '0', width: '100%', height: '100%' } }, { type: 'image', src: posterObj1.image, css: { left: '62rpx', top: '84rpx', width: '540rpx', height: '304rpx', borderRadius: '16rpx' } }, { type: 'text', text: posterObj1.title, use:'title', css: { fontSize: '32rpx', color: '#1D1D25', fontWeight: 'bold', lineHeight: '45rpx', left: '62rpx', top: '420rpx', width: '530rpx' } }, { type: 'text', text:posterObj1.title1, use:'title1', css: { fontSize: '28rpx', fontWeight: 'bold', color: '#1D1D25', lineHeight: '28rpx', left: '62rpx', top: 515+dth+'rpx' } }, { type: 'text', text: '长按或扫描识别二维码', use:'title2', css: { fontSize: '24rpx', color: '#6CB37A', lineHeight: '24rpx', left: '62rpx', top: 559+dth+'rpx' } }, { type: 'view', use:'qrcode-view', css: { left: '405rpx', top: 500+dth+'rpx', width: '240rpx', height: '240rpx', background: '#fff', } }, { type: 'image', src: posterObj1.qrcode, use:'qrcode', css: { left: '415rpx', top: 510+dth+'rpx', // transform: 'translate(-50%,0)', width: '220rpx', height: '220rpx', } },] } break; case 3: posterObj={ width: '670rpx', height: h+'rpx', background: '#fff', borderRadius: '16rpx', views: [{ type: 'image', use:'bg', src: 'https://s.yun-live.com/images/20210201/524ab6a41fe8c7eb57b35df9a547d388.png', css: { left: '0', top: '0', width: '100%', height: '100%' } }, { type: 'image', src: posterObj1.image, css: { left: '32rpx', top: '44rpx', width: '606rpx', height: '341rpx', borderRadius: '16rpx' } }, { type: 'text', text: posterObj1.title, use:'title', css: { fontSize: '32rpx', color: '#1D1D25', fontWeight: 'bold', lineHeight: '45rpx', left: '32rpx', top: '410rpx', width: '530rpx' } }, { type: 'text', text:posterObj1.title1, use:'title1', css: { fontSize: '28rpx', fontWeight: 'bold', color: '#1D1D25', lineHeight: '28rpx', left: '32rpx', top: 525+dth+'rpx' } }, { type: 'text', text: '长按或扫描识别二维码', use:'title2', css: { fontSize: '24rpx', color: '#6CB37A', lineHeight: '24rpx', left: '32rpx', top: 569+dth+'rpx' } }, { type: 'view', use:'qrcode-view', css: { left: '440rpx', top: 500+dth+'rpx', width: '240rpx', height: '240rpx', background: '#fff', } }, { type: 'image', src: posterObj1.qrcode, use:'qrcode', css: { left: '450rpx', top: 510+dth+'rpx', // transform: 'translate(-50%,0)', width: '220rpx', height: '220rpx', } },] } // this.posterObj1.backgroundType='image' // this.posterObj1.background='https://s.yun-live.com/images/20210201/524ab6a41fe8c7eb57b35df9a547d388.png'; // this.posterObj1.width='606rpx', // this.posterObj1.height='341rpx', // this.posterObj1.titleCssColor='#1D1D25' // this.posterObj1.title1CssColor='#1D1D25' // this.posterObj1.title2CssColor='#6CB37A' // break; default: break; } return posterObj } export default { baseUrl, baseRequest, TokenRequest, wxlogin, getPhone, syncInfo, makeCanvasData }