123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- //app.js
- import { systemInfo, routers, isFn, formatUrl, getGlobalVal, getWxUserInfoSetting, getUpdateManager } from './utils/util.js'
- import { WxLogin, FirstWxLogin, CheckStatus } from './utils/api.js'
- const { reLaunch, redirectTo } = routers()
- const sys = wx.getSystemInfoSync()
- App({
- onLaunch: function (options) {
- this.globalData.options = options
- getUpdateManager()
- wx.getSystemInfo({
- success: e => {
- this.globalData.statusBar = e.statusBarHeight; //状态栏高度
- let custom = wx.getMenuButtonBoundingClientRect();//菜单按钮
- this.globalData.custom = custom;
- this.globalData.customBar = custom.bottom + custom.top - e.statusBarHeight;
- //计算得到定义的状态栏高度
- }
- })
- },
- onShow: function (options) {
- this.globalData.options = options
- },
- onHide: function (e) {
- },
- onError: function (e) {
- wx.showToast({
- title: '程序执行失败',
- icon: 'none'
- })
- },
- onPageNotFound: function (e) {
- wx.showToast({
- title: '没有找到页面',
- icon: 'none'
- })
- setTimeout(() => {
- redirectTo({ url: 'pages/index/index' })
- }, 2000)
- },
- /**
- * 初始化
- */
- init: function () {
- // this.reLaunchCtl({ url: 'onetoone_feedback' }, true)
- // return
- // this.login()
- const userInfo = getGlobalVal('userInfo')
- getWxUserInfoSetting((type, res) => {
- if (type != 1 || !userInfo) {
- redirectTo({ url: 'pages/main/main' })
- return
- }
- this.globalData.userInfo = userInfo
- wx.checkSession({
- success: res => {
- this.globalData.accessToken = getGlobalVal('accessToken')
- this.globalData.identityList = getGlobalVal('identityList')
- this.permissionsJumpFn()
- },
- fail: res => {
- this.login()
- }
- })
- })
- },
- /**
- * 获取微信code
- */
- getWxloginCode: function (cb) {
- wx.login({
- success: res => {
- if (isFn(cb)) cb(res.code)
- },
- fail: res => {
- wx.showToast({
- title: '获取微信code失败',
- icon: 'none'
- })
- }
- })
- },
- /**
- * 普通登录
- */
- login: function () {
- const { appType } = this.globalData
- this.getWxloginCode(code => {
- WxLogin({ data: { code, appType } }).then(this.handleLogin).catch(e => {
- if (e.data.code == '888') {
- redirectTo({ url: 'pages/wx_authority/wx_authority' })
- }
- })
- })
- },
-
- /**
- * 接收登录返回信息
- */
- handleLogin: function (res) {
- const { postData, continuousFn } = this.globalData
-
- const { accessToken, list = [] } = res.data
- // 判断并缓存token
- if (accessToken) {
- this.globalData.accessToken = accessToken
- wx.setStorageSync('accessToken', accessToken)
- }
- this.permissionsJumpFn()
- // 判断并缓存userId
- // if (userId) {
- // this.globalData.userId = userId
- // wx.setStorageSync('userId', userId)
- // }
- // 判断该用户下用户身份和对应身份下的权限列表
- // if (list.length > 0) {
- // const identityList = wx.getStorageSync('identityList')
- // if (identityList.length != list.length || JSON.stringify(identityList) !== JSON.stringify(list)) {
- // wx.setStorageSync('currentIdentity', 0)
- // // wx.setStorageSync('hasBubbleCache', 0)
- // wx.setStorageSync('hasLectureNotesCache', 0)
- // }
- // this.globalData.identityList = list
- // wx.setStorageSync('identityList', list)
- // // 判断是否报登录失效后,接着记录事件继续做用户交互
- // if (continuousFn) {
- // const { fn, param } = continuousFn
- // fn(...param)
- // this.globalData.continuousFn = null
- // return
- // }
- // if (postData) {
- // const { fn, param } = postData
- // fn(...param)
- // this.globalData.postData = null
- // return
- // }
-
- // } else {
- // wx.showToast({
- // title: '无任何身份',
- // icon: 'none'
- // })
- // }
- },
- /**
- * 跳转判断权限控制层
- */
- permissionsJumpFn: function () {
- const res = this.getGlobalAttributeValue('identityList');
- console.log(res)
- const currentIdentity = this.getGlobalAttributeValue('currentIdentity') || -1
- console.log(currentIdentity);
- // if (res.length > 1) {
- // if (currentIdentity < 0) {
- // this.redirectCtl && this.redirectCtl({ url: 'selectpermissions', method: { identityList: JSON.stringify(res) } }, true)
- // } else {
- // this.selectPermissions(res[currentIdentity - 1])
- // }
- // } else {
- this.selectPermissions(res[0])
- // }
- },
- /**
- * 选择权限
- */
- selectPermissions: function (obj) {
- const { tempOptions, options } = this.globalData
- const { query = {}, scene, path } = options || tempOptions
- let url = ''
- this.checkStatus()
- // if (query && query.type > 1) {
- // url = path ? path.split('/')[1] : 'pages/task/task'
- // } else {
- // // if (permissions['isMaster']) {
- // // url = 'pages/course/course'
- // // } else {
- // url = 'pages/task/task'
- // // }
- // }
- // const paras = { ...query,url, scene }
- // console.log(this.globalData)
- // console.log(paras)
- // if (isFn(reLaunch)) reLaunch({ url, paras})
- this.globalData.options = tempOptions
- },
- /**
- * 保存微信用户数据
- * */
- saveUserInfo: function (res, code) {
- const { appType } = this.globalData
- const { encryptedData, iv, signature, userInfo } = res
- const user = Object.assign(userInfo, { encryptedData, iv, signature })
- FirstWxLogin({ data: { code, appType, ...user } }).then(res => {
- this.globalData.userInfo = user
- wx.setStorageSync('userInfo', user)
- this.handleLogin(res)
- })
- },
- /**
- * 审核结果
- * */
- checkStatus:function(){
- CheckStatus({ data: {} }).then(res => {
- const { curatorName, status, auditFailureReason,phone} = res.data
- if (status == 1) {
- this.redirectCtl({ url: 'register', method: { curatorName,phone} }, true)
- } else if (status == 2) {
- this.redirectCtl({ url: 'check_status', method: { isCheck: 1, curatorName,phone} }, true)
- } else if (status == 3) {
- this.redirectCtl({ url: 'check_status', method: { isCheck: 2, auditFailureReason: JSON.stringify(auditFailureReason), curatorName,phone} }, true)
- } else if (status == 4) {
- // this.redirectCtl({ url: 'register', method: { curatorName } }, true)
- if (!phone) {
- this.redirectCtl({ url: 'register', method: { isPhone:1,phone} }, true)
- } else{
- this.redirectCtl({ url: 'main', method: {} }, true)
- }
- }
- })
- },
- /**
- * 获取微信定位授权
- */
- getWxLocationSetting: function (cb) {
- this.wxSetting({ scope: 'scope.userLocation', text: '微信定位授权' }).then((res) => {
- wx.setStorageSync('hasPosition', 1)
- if (typeof cb === 'function') {
- cb(res)
- }
- }).catch((res) => {
- wx.setStorageSync('hasPosition', 0)
- wx.showToast({
- title: res.text,
- icon: 'none'
- })
- setTimeout(() => {
- wx.navigateBack({
- delta: 1
- })
- }, 1200)
- })
- },
- /**
- * 获取微信相机授权
- */
- getWxCameraSetting: function (cb) {
- this.wxSetting({ scope: 'scope.camera', text: '微信相机授权' }).then((res) => {
- wx.setStorageSync('hasCamera', 1)
- if (typeof cb === 'function') {
- cb(res)
- }
- }).catch((res) => {
- wx.setStorageSync('hasCamera', 0)
- wx.showToast({
- title: res.text,
- icon: 'none'
- })
- setTimeout(() => {
- wx.navigateBack({
- delta: 1
- })
- }, 1200)
- })
- },
- /**
- * 微信授权接口封装
- */
- wxSetting: function ({ scope, text }) {
- const isEmptyObject = function (e) {
- var temp;
- for (temp in e)
- return !1;
- return !0
- }
- return new Promise((resolve, reject) => {
- wx.getSetting({
- success: res => {
- if (isEmptyObject(res.authSetting)) {
- resolve(Object.assign(res, { type: 1, text: `请手动点击${text}` }))
- } else if (res.authSetting[scope] === true) {
- resolve(Object.assign(res, { type: 2, text: `默认${text}成功` }))
- } else if (res.authSetting[scope] === false) {
- wx.showModal({
- content: `检测到您没打开${text},是否去设置打开?`,
- confirmText: "确认",
- cancelText: "取消",
- success: res => {
- if (res.confirm) {
- wx.openSetting({
- success: res => {
- if (res.authSetting[scope]) {
- resolve(Object.assign(res, { type: 3, text: `手动设置${text}成功` }))
- } else {
- reject(Object.assign(res, { type: 5, text: `没有手动设置${text}` }))
- }
- },
- fail: res => {
- reject(Object.assign(res, { type: 4, text: `打开手动设置${text}失败` }))
- }
- })
- } else {
- reject(Object.assign(res, { type: 3, text: `拒绝打开手动设置${text}` }))
- }
- },
- fail: res => {
- reject(Object.assign(res, { type: 2, text: `弹出${text}提示框失败` }))
- }
- })
- } else {
- wx.authorize({
- scope,
- success: res => {
- resolve(Object.assign(res, { type: 4, text: `自动获取${text}成功` }))
- },
- fail: res => {
- // console.log(res)
- reject(Object.assign(res, { type: 6, text: `自动获取${text}失败` }))
- }
- })
- }
- },
- fail: res => {
- reject(res, { type: 1, text: `获取${text}失败` })
- }
- })
- })
- },
- /**
- * 获取全局属性值
- */
- getGlobalAttributeValue: function (e1, e2 = e1) {
- return typeof this.globalData[e1] !== 'undefined' && this.globalData[e1] !== null ? this.globalData[e1] : typeof wx.getStorageSync(e2) !== 'undefined' && wx.getStorageSync(e2) !== null ? wx.getStorageSync(e2) : null
- },
- /**
- * POST 封装
- * @param obj{Object} {src【请求后台路径】, method【拼接在路径的参数对象】, type【判断是否需要把默认token和参数一起拼接在路径后面】} = obj
- * @param data{Object} data【自定义传的参数数据】
- * @param head{Boolean} head[true【data参数类型为json对象】,false【data参数类型为json字符串】]
- * @param postData{Object} postData: { fn: Function, param: Object } 当接口报登录失效401的时候 记录失效的当前状态,等待自动登录成功后接着运行
- */
- post: function (obj, data = {}, head, postData) {
- const { globalData } = this
- const { host, loginTime, wechatsys, phoneimei, phonesys, phonetype, version, platform, build } = globalData
- const accessToken = this.globalData.accessToken || wx.getStorageSync('accessToken')
- const header = { wechatsys, phoneimei, phonesys, phonetype, version, platform, build, "Content-Type": head ? "application/x-www-form-urlencoded" : "application/json"}
- let url
- if (typeof obj === 'object') {
- const { src, method, type } = obj
- url = formatUrl(host + src, type ? method : Object.assign(method || {}, { accessToken }))
- } else if (typeof obj === 'string') {
- url = formatUrl(host + obj, { accessToken })
- } else {
- throw new Error('请正确填写访问后台接口路径')
- return
- }
- return new Promise((resolve, reject) => {
- wx.request({
- url,
- data,
- method: 'POST',
- header,
- success: e => {
- if (e.statusCode == 200) {
- if (e.data.code == '999') {
- resolve(e.data)
- } else {
- if (url.indexOf('/user/checkToken') < 0) {
- wx.showToast({
- title: e.data.msg,
- icon: 'none'
- })
- }
- reject(e.data)
- }
- if (loginTime > 0) {
- this.globalData.loginTime = 0
- }
- } else if (e.statusCode == 401) {
- this.globalData.loginTime = loginTime + 1
- if (loginTime < 2) {
- this.globalData.postData = postData || null
- this.login()
- } else {
- reject(e)
- wx.showToast({
- title: '登录失效,稍后再试',
- icon: 'none'
- })
- }
- } else {
- reject(e)
- wx.showToast({
- title: '加载失败',
- icon: 'none'
- })
- }
- console.log({ url, request: data, response: e.data, globalData })
- },
- fail: e => {
- reject(e)
- wx.showToast({
- title: '加载失败',
- icon: 'none'
- })
- console.log({ url, request: data, response: e })
- }
- })
- })
- },
- /**
- * uploadFile 封装
- * @param obj{Object} {src【请求后台路径】, method【拼接在路径的参数对象】, type【判断是否需要把token和参数一起拼接在路径后面】} = obj
- * @param file{String} file【微信小程序生成的临时文件资源路径】
- * @param data{Object} data【自定义传的参数数据】
- * @param head{Boolean} head[true【data参数类型为json对象】,false【data参数类型为json字符串】]
- * @param progress{Function} progress【上传文件进度条回调函数方法】
- * @param postData{Object} postData: { fn: Function, param: Object } 当接口报登录失效401的时候 记录失效的当前状态,等待自动登录成功后接着运行
- */
- uploadFile: function (obj, file, data = {}, head, progress, postData) {
- const { globalData } = this
- const { host, loginTime, wechatsys, phoneimei, phonesys, phonetype, version, platform, build } = globalData
- const accessToken = this.globalData.accessToken || wx.getStorageSync('accessToken')
- const header = { wechatsys, phoneimei, phonesys, phonetype, version, platform, build, "Content-Type": head ? "application/x-www-form-urlencoded" : "application/json" }
- let url
- if (typeof obj === 'object') {
- const { src, method, type } = obj
- url = formatUrl(host + src, type ? method : Object.assign(method || {}, { accessToken }))
- } else if (typeof obj === 'string') {
- url = formatUrl(host + obj, { accessToken })
- } else {
- throw new Error('请正确填写访问后台接口路径')
- return
- }
- wx.showLoading({
- title: '上传中...',
- })
- return new Promise((resolve, reject) => {
- const uploadTask = wx.uploadFile({
- url,
- filePath: file,
- name: 'files',
- formData: data,
- header,
- success: e => {
- if (e.statusCode == 200) {
- resolve(e.data)
- if (loginTime > 0) {
- this.globalData.loginTime = 0
- }
- } else if (e.statusCode == 401) {
- this.globalData.loginTime = loginTime + 1
- if (loginTime < 2) {
- this.globalData.postData = postData || null
- this.login()
- } else {
- reject(e)
- wx.showToast({
- title: '登录失效,稍后再试',
- icon: 'none'
- })
- }
- } else {
- reject(e)
- wx.showToast({
- title: '加载失败',
- icon: 'none'
- })
- }
- console.log({ url, request: data, response: e.data, globalData })
- },
- fail (e) {
- reject(e)
- wx.showToast({
- title: '加载失败',
- icon: 'none'
- })
- console.log({ url, request: data, response: e })
- },
- complete (e) {
- wx.hideLoading()
- }
- })
- uploadTask.onProgressUpdate((res) => {
- progress && progress(res)
- wx.showLoading({
- title: ` 已上传(${res.progress}%)`
- })
- if (res.progress == 100) {
- wx.hideLoading()
- }
- })
- })
- },
- /**
- * reLaunch路由跳转
- */
- reLaunchCtl: function (e, type) {
- const { method, url, msg } = type ? e : e.currentTarget.dataset
- if (!url) {
- wx.showToast({
- title: msg || '暂未开放',
- icon: 'none'
- })
- return
- }
- let array = []
- if (method) {
- for (let i in method) {
- if (typeof data === 'object') {
- array.push(`${i}=${JSON.stringify(method[i])}`)
- } else {
- array.push(`${i}=${method[i]}`)
- }
- }
- }
- wx.reLaunch({
- url: `${getCurrentPages().length == 0 ? 'pages' : '..'}/${url}/${url}${array.length > 0 ? '?' + array.join('&') : ''}`,
- success: res => { },
- fail: res => {
- wx.showToast({
- title: '跳转失败',
- icon: 'none'
- })
- }
- })
- },
- /**
- * navigate路由跳转
- */
- navigateCtl: function (e, type) {
- const { method, url, msg } = type ? e : e.currentTarget.dataset
- if (!url) {
- wx.showToast({
- title: msg || '暂未开放',
- icon: 'none'
- })
- return
- }
- let array = []
- if (method) {
- for (let i in method) {
- if (typeof data === 'object') {
- array.push(`${i}=${JSON.stringify(method[i])}`)
- } else {
- array.push(`${i}=${JSON.stringify(method[i])}`)
- }
- }
- }
- wx.navigateTo({
- url: `${getCurrentPages().length == 0 ? 'pages' : '..'}/${url}/${url}${array.length > 0 ? '?' + array.join('&') : ''}`,
- success: res => { },
- fail: res => {
- wx.showToast({
- title: '跳转失败',
- icon: 'none'
- })
- }
- })
- },
- /**
- * redirect路由跳转
- */
- redirectCtl: function (e, type) {
- const { method, url, msg } = type ? e : e.currentTarget.dataset
- if (!url) {
- wx.showToast({
- title: msg || '暂未开放',
- icon: 'none'
- })
- return
- }
- let array = []
- if (method) {
- for (let i in method) {
- if (typeof data === 'object') {
- array.push(`${i}=${JSON.stringify(method[i])}`)
- } else {
- array.push(`${i}=${method[i]}`)
- }
- }
- }
- wx.redirectTo({
- url: `${getCurrentPages().length == 0 ? 'pages' : '..'}/${url}/${url}${array.length > 0 ? '?' + array.join('&') : ''}`,
- success: res => { },
- fail: res => {
- wx.showToast({
- title: '跳转失败',
- icon: 'none'
- })
- }
- })
- },
- /**
- * 查看图片
- */
- viewImageCtl: function (e, type) {
- const { images, index, baseurl, url = 'img' } = type ? e : e.currentTarget.dataset
- const arr = []
- if (images !== null && typeof images === 'object') {
- for (const v of images) {
- arr.push(`${baseurl}${v[url] ? v[url] : v }`)
- }
- }
- wx.previewImage({
- current: arr[index],
- urls: arr
- })
- },
- /**
- * 分享控制层
- */
- sharePageDefaultCtl: function () {
- return {
- title: '私塾家校长版',
- path: 'pages/index/index',
- success: res => {
- wx.showToast({
- title: '转发成功',
- icon: 'success'
- })
- }
- }
- },
- /**
- * 全局变量
- */
- globalData: {
- sys,
- wechatsys: sys.version + ' ' + sys.SDKVersion,
- phoneimei: '',
- phonesys: sys.system,
- phonetype: sys.brand + ' ' + sys.model,
- version: '1.2.4',
- platform: '3',
- build: '1.2.4',
- updateInfo: '私塾家校长版',
- appid: 'wx5a97ed416d40ac39',
- userInfo: null,
- appType: 14,
- loginTime: 0,
- // host: 'https://sx.sharingschool.com/sz',
- // host: 'https://api.sharingschool.com/sz',
- // baseImgUrl: 'https://img.sharingschool.com',
- thumbnail: '?x-oss-process=image/resize,m_fill,h_300,w_200',
- host: 'https://xt.sharingschool.com/sz',
- baseImgUrl: 'https://xtimg.sharingschool.com',
- accessToken: null,
- identityList: null,
- permissions: null,
- libId: null,
- tempOptions: { query: {}, path: 'pages/index/index', scene: 1001 },
- options: null,
- postData: null,
- continuousFn: null,
- errorText: '出了点小问题',
- toastText: '您点太快了',
- }
- })
|