jVerificationLogin.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * 极光一键登录封装
  3. * 功能:1. 判断登录态 2. 未登录则弹出极光一键登录界面 3. 登录成功后执行自定义回调
  4. */
  5. import { isProduction } from '@/common/config'
  6. import HttpRequest from '@/common/httpRequest'
  7. // 1. 引入极光插件
  8. const jv = uni.requireNativePlugin('JG-JVerification');
  9. // 2. 全局状态(避免重复初始化、保存协议勾选状态)
  10. const jvState = {
  11. isInit: false, // SDK 是否已初始化
  12. };
  13. const isIOS = uni.getSystemInfoSync().platform == "ios"
  14. /**
  15. * 初始化极光 SDK(仅初始化一次)
  16. */
  17. function initJVerification() {
  18. return new Promise((resolve, reject) => {
  19. // 已初始化,直接返回成功
  20. if (jvState.isInit) {
  21. resolve(true);
  22. return;
  23. }
  24. // 初始化 SDK
  25. jv.init({
  26. timeout: 7000,
  27. isProduction: isProduction // 生产环境改为 true
  28. }, (result) => {
  29. if (result.code === 8000) { // 初始化成功(极光返回 code=0 为成功)
  30. jvState.isInit = true;
  31. console.log('极光 SDK 初始化成功');
  32. resolve(true);
  33. } else {
  34. reject(new Error(result.content || '初始化失败'));
  35. }
  36. });
  37. });
  38. }
  39. /**
  40. * 配置极光一键登录自定义 UI
  41. */
  42. function configJVerificationUI() {
  43. if (isIOS) {
  44. // iOS平台
  45. jv.setCustomUIWithConfigiOS({
  46. logoImg: 'static/invite.png',
  47. logoWidth: 200,
  48. logoHeight: 200,
  49. navColor: 0xffffff,
  50. navReturnImg: 'static/close-iOS.png',
  51. numberColor: 0xff333333,
  52. numberSize: 28,
  53. numFieldOffsetY: 340,
  54. sloganOffsetY: 380,
  55. logBtnText: '一键登录',
  56. logBtnOffsetY: 418,
  57. logBtnImgs: ['static/login-btn-iOS.png', 'static/login-btn-iOS.png', 'static/login-btn-iOS.png'],
  58. uncheckedImg: 'static/unchecked-iOS.png',
  59. checkedImg: 'static/checked-iOS.png',
  60. privacyCheckToastMessage: '请点击同意协议',
  61. privacyShowBookSymbol: true,
  62. privacyTextFontSize: 12,
  63. appPrivacyColor: [0xff605D67, 0xff016BF6],
  64. appPrivacys:[
  65. "已阅读并同意",
  66. ["、","《亿职赞用户协议》","https://h5.bosszan.com/my/setting/xieyi","亿职赞用户协议"],
  67. ["和","《隐私协议》","https://h5.bosszan.com/my/setting/mimi","隐私协议"],
  68. ",允许亿职赞统一管理本人账号信息"],
  69. agreementNavBackgroundColor: 0xffffff,
  70. agreementNavReturnImage: 'static/close-iOS.png'
  71. })
  72. } else {
  73. // Android平台
  74. jv.setCustomUIWithConfigAndroid({
  75. setLogoImgPath: 'static/invite.png',
  76. setLogoWidth: 200,
  77. setLogoHeight: 200,
  78. setNumFieldOffsetY: 340,
  79. setSloganOffsetY: 380,
  80. setLogBtnText: '一键登录',
  81. setLogBtnImgPath: 'static/login-btn.png',
  82. setLogBtnHeight: 40,
  83. setLogBtnOffsetY: 418,
  84. setNavReturnImgPath: 'static/close.png',
  85. setNavReturnBtnWidth: 20,
  86. setNavReturnBtnHeight: 20,
  87. setNavReturnBtnOffsetX: 20,
  88. setNavReturnBtnOffsetY: 18,
  89. setNavTransparent: true,
  90. setNumberColor: 0xff333333,
  91. setNumberSize: 28,
  92. setPrivacyOffsetX: 30,
  93. setPrivacyTextSize: 12,
  94. setCheckedImgPathFromJs: 'static/checked.png',
  95. setUncheckedImgPathFromJs: 'static/unchecked.png',
  96. setPrivacyCheckboxSize: 12,
  97. setPrivacyWithBookTitleMark: true,
  98. setAppPrivacyColor: [0xff605D67, 0xff016BF6],
  99. enableHintToast: true,
  100. enableHintToastText: '请点击同意协议',
  101. setPrivacyNameAndUrlBeanList: [{
  102. name: '亿职赞用户协议',
  103. url: 'https://h5.bosszan.com/my/setting/xieyi',
  104. beforeName: '、'
  105. },
  106. {
  107. name: '隐私协议',
  108. url: 'https://h5.bosszan.com/my/setting/mimi',
  109. beforeName: '和'
  110. },
  111. ],
  112. setPrivacyNavColor: 0xff016BF6,
  113. });
  114. }
  115. }
  116. /**
  117. * 执行极光一键登录核心逻辑
  118. */
  119. function doJVerificationLogin(callback) {
  120. return new Promise((resolve, reject) => {
  121. // 预登录(提升登录速度,极光推荐)
  122. jv.preLogin(7000, (preResult) => {
  123. if (preResult.code !== 7000) {
  124. // 预取号失败
  125. plus.nativeUI.toast('一键登录失败')
  126. reject(new Error(preResult.content || '预取号失败'));
  127. return;
  128. }
  129. // 配置自定义 UI
  130. configJVerificationUI();
  131. // 显示一键登录界面
  132. jv.loginAuth({
  133. autoFinish: isIOS ? true : false,
  134. timeout: 5000
  135. }, (loginRes) => {
  136. uni.showLoading({ title: '登录中' })
  137. if (loginRes.code == 6000) {
  138. // 成功获取运营商的loginToken
  139. // 请求后端接口进行登录
  140. const params = {
  141. ...loginRes,
  142. platform: 'app'
  143. }
  144. HttpRequest
  145. .postJson('/app/Login/jpushLogin', params)
  146. .then((response) => {
  147. if (response.code === 0) {
  148. if (typeof callback === 'function') {
  149. callback(response);
  150. }
  151. // 返回 accessToken,供调用方后续换取手机号
  152. resolve(true);
  153. return
  154. }
  155. plus.nativeUI.toast('登录失败:', response.msg)
  156. })
  157. .finally(() => {
  158. uni.hideLoading()
  159. })
  160. } else if (loginRes.code == 6002) {
  161. // 用户取消了一键登录
  162. uni.hideLoading()
  163. } else {
  164. // 其他情况则跳转到手机验证码登录
  165. uni.hideLoading()
  166. plus.nativeUI.toast('一键登录失败')
  167. }
  168. }, (event) => {
  169. // 监听协议勾选/取消事件
  170. });
  171. });
  172. });
  173. }
  174. /**
  175. * 关闭授权弹窗
  176. */
  177. export function closeVerifyView() {
  178. // #ifdef APP-PLUS
  179. if (!isIOS) {
  180. jv.dismissLoginAuth(true);
  181. }
  182. // #endif
  183. }
  184. /**
  185. * 对外暴露的核心方法:极光一键登录入口
  186. * @param {Function} callback // 成功后执行
  187. */
  188. export function jVerificationLogin(callback) {
  189. initJVerification()
  190. .then(() => doJVerificationLogin(callback))
  191. .catch((err) => {
  192. console.error('极光一键登录失败:', err.message);
  193. });
  194. }