init.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. let {
  18. supportedLoginType
  19. } = await uniIdCo.getSupportedLoginType()
  20. console.log("supportedLoginType: " + JSON.stringify(supportedLoginType));
  21. //登录方式,服务端和客户端的映射关系
  22. let 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. }
  36. //遍历客户端配置的登录方式,与服务端比对。并在错误时抛出错误提示
  37. let list = loginTypes.filter(type => !supportedLoginType.includes(data[type]))
  38. if (list.length) {
  39. console.error(
  40. `错误:前端启用的登录方式:${list.join(',')};没有在服务端完成配置。配置文件路径:"/uni_modules/uni-config-center/uniCloud/cloudfunctions/common/uni-config-center/uni-id/config.json"`
  41. );
  42. }
  43. }
  44. // #ifdef APP-PLUS
  45. //如果uni-id-pages配置的登录功能有一键登录,有则执行预登录(异步)
  46. if (loginTypes.includes('univerify')) {
  47. uni.preLogin({
  48. provider: 'univerify',
  49. complete: e => {
  50. console.log(e);
  51. }
  52. })
  53. }
  54. // #endif
  55. //3. 绑定clientDB错误事件
  56. // clientDB对象
  57. const db = uniCloud.database()
  58. db.on('error', onDBError)
  59. //clientDB的错误提示
  60. function onDBError({
  61. code, // 错误码详见https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=returnvalue
  62. message
  63. }) {
  64. console.error('onDBError', {
  65. code,
  66. message
  67. });
  68. }
  69. // 解绑clientDB错误事件
  70. //db.off('error', onDBError)
  71. //4. 同步客户端push_clientid至device表
  72. if (uniCloud.onRefreshToken) {
  73. uniCloud.onRefreshToken(() => {
  74. console.log('onRefreshToken');
  75. if (uni.getPushClientId) {
  76. uni.getPushClientId({
  77. success: async function(e) {
  78. console.log(e)
  79. let pushClientId = e.cid
  80. console.log(pushClientId);
  81. let res = await uniIdCo.setPushCid({
  82. pushClientId
  83. })
  84. console.log('getPushClientId', res);
  85. },
  86. fail(e) {
  87. console.log(e)
  88. }
  89. })
  90. }
  91. })
  92. }
  93. }