{"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 if (res.confirm) {\r\n // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启\r\n updateManager.applyUpdate()\r\n }\r\n },\r\n })\r\n })\r\n updateManager.onUpdateFailed(function () {\r\n // 新的版本下载失败\r\n // uni.showModal({\r\n // title: '已经有新版本了哟~',\r\n // content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开~',\r\n // });\r\n })\r\n } else {\r\n if (!silence) {\r\n uni.showModal({\r\n title: '当前为最新版本',\r\n showCancel: false,\r\n })\r\n }\r\n }\r\n })\r\n }\r\n}\r\n\r\n// 获取订阅消息模板\r\nasync function getSubscribeTemplate() {\r\n const { code, data } = await SocialApi.getSubscribeTemplateList()\r\n if (code === 0) {\r\n subscribeEventList = data\r\n }\r\n}\r\n\r\n// 订阅消息\r\nfunction subscribeMessage(event, callback = undefined) {\r\n const tmplIds = []\r\n if (typeof event === 'string') {\r\n const temp = subscribeEventList.find((item) => item.title.includes(event))\r\n if (temp) {\r\n tmplIds.push(temp.id)\r\n }\r\n }\r\n if (typeof event === 'object') {\r\n event.forEach((e) => {\r\n const temp = subscribeEventList.find((item) => item.title.includes(e))\r\n if (temp) {\r\n tmplIds.push(temp.id)\r\n }\r\n })\r\n }\r\n if (tmplIds.length === 0) return\r\n\r\n uni.requestSubscribeMessage({\r\n tmplIds,\r\n success: () => {\r\n // 不管是拒绝还是同意都触发\r\n callback && callback()\r\n },\r\n fail: (err) => {\r\n console.log(err)\r\n },\r\n })\r\n}\r\n\r\nexport default {\r\n load,\r\n login,\r\n bind,\r\n unbind,\r\n bindUserPhoneNumber,\r\n mobileLogin,\r\n getInfo,\r\n getOpenid,\r\n subscribeMessage,\r\n checkUpdate,\r\n}\r\n"],"names":["uni","AuthUtil","SocialApi","UserApi","res"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAIA,MAAM,aAAa;AAEnB,IAAI,qBAAqB,CAAE;AAG3B,SAAS,OAAO;AACd,cAAa;AACb,uBAAsB;AACxB;AAGA,MAAM,QAAQ,MAAY;AACxB,SAAO,IAAI,QAAQ,CAAO,SAAS,WAAW;AAE5C,UAAM,aAAa,MAAMA,cAAG,MAAC,MAAO;AACpC,QAAI,WAAW,WAAW,YAAY;AACpC,aAAO,QAAQ,KAAK;AAAA,IACrB;AAGD,UAAM,cAAc,MAAMC,+BAAS,YAAY,YAAY,WAAW,MAA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