init.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // 导入配置
  2. import config from '@/uni_modules/uni-id-pages/config.js'
  3. // uni-id的云对象
  4. const uniIdCo = uniCloud.importObject('uni-id-co', {
  5. customUI: true
  6. })
  7. // 用户配置的登录方式、是否打开调试模式
  8. const {
  9. loginTypes,
  10. debug
  11. } = config
  12. export default async function () {
  13. // 有打开调试模式的情况下
  14. if (debug) {
  15. // 1. 检查本地uni-id-pages中配置的登录方式,服务器端是否已经配置正确。否则提醒并引导去配置
  16. // 调用云对象,获取服务端已正确配置的登录方式
  17. const {
  18. supportedLoginType
  19. } = await uniIdCo.getSupportedLoginType()
  20. // console.log('supportedLoginType: ' + JSON.stringify(supportedLoginType))
  21. // 登录方式,服务端和客户端的映射关系
  22. const data = {
  23. smsCode: 'mobile-code',
  24. univerify: 'univerify',
  25. username: 'username-password',
  26. weixin: 'weixin',
  27. qq: 'qq',
  28. xiaomi: 'xiaomi',
  29. sinaweibo: 'sinaweibo',
  30. taobao: 'taobao',
  31. facebook: 'facebook',
  32. google: 'google',
  33. alipay: 'alipay',
  34. apple: 'apple',
  35. weixinMobile: 'weixin'
  36. }
  37. // 遍历客户端配置的登录方式,与服务端比对。并在错误时抛出错误提示
  38. const list = loginTypes.filter(type => !supportedLoginType.includes(data[type]))
  39. if (list.length) {
  40. console.error(
  41. `错误:前端启用的登录方式:${list.join(',')};没有在服务端完成配置。配置文件路径:"/uni_modules/uni-config-center/uniCloud/cloudfunctions/common/uni-config-center/uni-id/config.json"`
  42. )
  43. }
  44. }
  45. // #ifdef APP-PLUS
  46. // 如果uni-id-pages配置的登录功能有一键登录,有则执行预登录(异步)
  47. if (loginTypes.includes('univerify')) {
  48. uni.preLogin({
  49. provider: 'univerify',
  50. complete: e => {
  51. // console.log(e);
  52. }
  53. })
  54. }
  55. // #endif
  56. // 3. 绑定clientDB错误事件
  57. // clientDB对象
  58. const db = uniCloud.database()
  59. db.on('error', onDBError)
  60. // clientDB的错误提示
  61. function onDBError ({
  62. code, // 错误码详见https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=returnvalue
  63. message
  64. }) {
  65. // console.error('onDBError', {code,message});
  66. }
  67. // 解绑clientDB错误事件
  68. // db.off('error', onDBError)
  69. // 4. 同步客户端push_clientid至device表
  70. if (uniCloud.onRefreshToken) {
  71. uniCloud.onRefreshToken(() => {
  72. // console.log('onRefreshToken');
  73. if (uni.getPushClientId) {
  74. uni.getPushClientId({
  75. success: async function (e) {
  76. // console.log(e)
  77. const pushClientId = e.cid
  78. // console.log(pushClientId);
  79. const res = await uniIdCo.setPushCid({
  80. pushClientId
  81. })
  82. // console.log('getPushClientId', res);
  83. },
  84. fail (e) {
  85. // console.log(e)
  86. }
  87. })
  88. }
  89. })
  90. }
  91. }