1 line
9.3 KiB
Plaintext
1 line
9.3 KiB
Plaintext
|
{"version":3,"file":"miniProgram.js","sources":["../../../../../../../src/sheep/platform/provider/wechat/miniProgram.js"],"sourcesContent":["import AuthUtil from '@/sheep/api/member/auth'\r\nimport SocialApi from '@/sheep/api/member/social'\r\nimport UserApi from '@/sheep/api/member/user'\r\n\r\nconst socialType = 34 // 社交类型 - 微信小程序\r\n\r\nlet subscribeEventList = []\r\n\r\n// 加载微信小程序\r\nfunction load() {\r\n checkUpdate()\r\n getSubscribeTemplate()\r\n}\r\n\r\n// 微信小程序静默授权登陆\r\nconst login = async () => {\r\n return new Promise(async (resolve, reject) => {\r\n // 1. 获得微信 code\r\n const codeResult = await uni.login()\r\n if (codeResult.errMsg !== 'login:ok') {\r\n return resolve(false)\r\n }\r\n\r\n // 2. 社交登录\r\n const loginResult = await AuthUtil.socialLogin(socialType, codeResult.code, 'default')\r\n if (loginResult.code === 0) {\r\n setOpenid(loginResult.data.openid)\r\n return resolve(true)\r\n } else {\r\n return resolve(false)\r\n }\r\n })\r\n}\r\n\r\n// 微信小程序手机号授权登陆\r\nconst mobileLogin = async (e) => {\r\n return new Promise(async (resolve, reject) => {\r\n if (e.errMsg !== 'getPhoneNumber:ok') {\r\n return resolve(false)\r\n }\r\n\r\n // 1. 获得微信 code\r\n const codeResult = await uni.login()\r\n if (codeResult.errMsg !== 'login:ok') {\r\n return resolve(false)\r\n }\r\n\r\n // 2. 一键登录\r\n const loginResult = await AuthUtil.weixinMiniAppLogin(e.code, codeResult.code, 'default')\r\n if (loginResult.code === 0) {\r\n setOpenid(loginResult.data.openid)\r\n return resolve(true)\r\n } else {\r\n return resolve(false)\r\n }\r\n })\r\n}\r\n\r\n// 微信小程序绑定\r\nconst bind = () => {\r\n return new Promise(async (resolve, reject) => {\r\n // 1. 获得微信 code\r\n const codeResult = await uni.login()\r\n if (codeResult.errMsg !== 'login:ok') {\r\n return resolve(false)\r\n }\r\n\r\n // 2. 绑定账号\r\n const bindResult = await SocialApi.socialBind(socialType, codeResult.code, 'default')\r\n if (bindResult.code === 0) {\r\n setOpenid(bindResult.data)\r\n return resolve(true)\r\n } else {\r\n return resolve(false)\r\n }\r\n })\r\n}\r\n\r\n// 微信小程序解除绑定\r\nconst unbind = async (openid) => {\r\n const { code } = await SocialApi.socialUnbind(socialType, openid)\r\n return code === 0\r\n}\r\n\r\n// 绑定用户手机号\r\nconst bindUserPhoneNumber = (e) => {\r\n return new Promise(async (resolve, reject) => {\r\n const { code } = await UserApi.updateUserMobileByWeixin(e.code)\r\n if (code === 0) {\r\n resolve(true)\r\n }\r\n resolve(false)\r\n })\r\n}\r\n\r\n// 设置 openid 到本地存储,目前只有 pay 支付时会使用\r\nfunction setOpenid(openid) {\r\n uni.setStorageSync('openid', openid)\r\n}\r\n\r\n// 获得 openid\r\nasync function getOpenid(force = false) {\r\n let openid = uni.getStorageSync('openid')\r\n if (!openid && force) {\r\n const info = await getInfo()\r\n if (info && info.openid) {\r\n openid = info.openid\r\n setOpenid(openid)\r\n }\r\n }\r\n return openid\r\n}\r\n\r\n// 获得社交信息\r\nasync function getInfo() {\r\n const { code, data } = await SocialApi.getSocialUser(socialType)\r\n if (code !== 0) {\r\n return undefined\r\n }\r\n return data\r\n}\r\n\r\n// ========== 非登录相关的逻辑 ==========\r\n\r\n// 小程序更新\r\nconst checkUpdate = async (silence = true) => {\r\n if (uni.canIUse('getUpdateManager')) {\r\n const updateManager = uni.getUpdateManager()\r\n updateManager.onCheckForUpdate(function (res) {\r\n // 请求完新版本信息的回调\r\n if (res.hasUpdate) {\r\n updateManager.onUpdateReady(function () {\r\n uni.showModal({\r\n title: '更新提示',\r\n content: '新版本已经准备好,是否重启应用?',\r\n success: function (res) {\r\n
|