univerify-custom-page.uvue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <view class="container">
  3. <view class="safe_content">
  4. <text class="close_icon" @click="closePage">{{closeIcon}}</text>
  5. <text class="title">这是一个普通uvue的dialogPage页面</text>
  6. <view class="number">
  7. <text id="number-text" class="number-text" ref="number-text">{{phone}}</text>
  8. <text id="slogan-text" class="slogan-text">{{slogan}}</text>
  9. </view>
  10. <button id="login-button" class="login-button" @click="loginIn">本机号码一键登录</button>
  11. <view class="privacy">
  12. <checkbox id="privacy-checkbox" class="privacy-checkbox" ref="privacy-checkbox" :checked="false"></checkbox>
  13. <text class="privacy-normal-text">登录即同意</text>
  14. <text id="privacy-text" class="privacy-text" @click="openLink">{{privacyName}}</text>
  15. </view>
  16. <text class="other" @click="otherLogin">其他登录方式</text>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. uniVerifyManager: null as UniVerifyManager | null,
  25. phone: '',
  26. slogan: '',
  27. privacyName: '',
  28. privacyUrl: '',
  29. closeIcon: '\uE650',
  30. isLoading: false //是否正在登录中
  31. }
  32. },
  33. onLoad(options : OnLoadOptions) {
  34. this.uniVerifyManager = uni.getUniVerifyManager();
  35. this.phone = options['phone'] as string;
  36. this.slogan = options['slogan'] as string;
  37. this.privacyName = options['name'] as string;
  38. this.privacyUrl = options['link'] as string;
  39. },
  40. methods: {
  41. closePage() {
  42. uni.closeDialogPage({
  43. dialogPage: this.$page,
  44. animationType: 'slide-out-bottom',
  45. success(res) {
  46. console.log('closeThisDialog success', res)
  47. },
  48. fail(err) {
  49. console.log('closeThisDialog fail', err)
  50. }
  51. })
  52. },
  53. openLink() {
  54. let url = '/pages/API/get-univerify-manager/full-webview-page?url=' + this.privacyUrl + '&title=' + this.privacyName + '&animationType=slide-out-bottom';
  55. uni.openDialogPage({
  56. url: url,
  57. animationType: 'slide-in-bottom',
  58. success(res) {
  59. console.log("打开隐私协议");
  60. },
  61. fail(err) {
  62. console.log(err);
  63. }
  64. })
  65. },
  66. loginIn() {
  67. if (this.isLoading) {
  68. uni.showToast({
  69. title: "正在登录中,请稍后",
  70. position: "bottom",
  71. duration: 2000
  72. })
  73. return;
  74. }else{
  75. this.isLoading = true;
  76. }
  77. const numberTextElement = this.$page.getElementById('number-text');
  78. const sloganTextElement = this.$page.getElementById('slogan-text');
  79. const loginButtonElement = this.$page.getElementById('login-button');
  80. const privacyCheckBoxElement = this.$page.getElementById('privacy-checkbox');
  81. const privacyTextElement = this.$page.getElementById('privacy-text');
  82. if (numberTextElement != null && sloganTextElement != null && loginButtonElement != null && privacyCheckBoxElement != null && privacyTextElement != null) {
  83. this.uniVerifyManager?.customLogin({
  84. numberTextElement: numberTextElement,
  85. sloganTextElement: sloganTextElement,
  86. loginButtonElement: loginButtonElement,
  87. privacyCheckBoxElement: privacyCheckBoxElement,
  88. privacyTextElement: privacyTextElement,
  89. success: (res) => {
  90. console.log(res);
  91. this.takePhoneNumber(res.accessToken, res.openId);
  92. },
  93. fail: (error) => {
  94. if (error.errCode == 40001) {
  95. uni.showToast({
  96. title: "请同意服务条款",
  97. position: "bottom",
  98. duration: 2000
  99. })
  100. } else if (error.errCode == 40002) {
  101. uni.showToast({
  102. title: "授权页不符合规范",
  103. position: "bottom",
  104. duration: 2000
  105. })
  106. } else {
  107. const errorMsg = JSON.parseObject(error.cause?.cause?.message ?? "")?.getString("errorDesc") ?? error.errMsg;
  108. uni.showToast({
  109. title: errorMsg,
  110. position: "bottom",
  111. duration: 2000
  112. })
  113. }
  114. },
  115. complete: () =>{
  116. this.isLoading = false;
  117. }
  118. })
  119. } else {
  120. uni.showToast({
  121. title: "未获取到页面元素",
  122. position: "bottom",
  123. duration: 2000
  124. })
  125. this.isLoading = false;
  126. }
  127. },
  128. takePhoneNumber(token : string, openId : string) {
  129. //云函数取号
  130. uniCloud.callFunction({
  131. name: 'univerify',
  132. data: {
  133. access_token: token, // 客户端一键登录接口返回的access_token
  134. openid: openId // 客户端一键登录接口返回的openid
  135. }
  136. }).then(res => {
  137. // 关闭登录页
  138. this.closePage();
  139. setTimeout(() => {
  140. uni.showModal({
  141. title: '取号成功',
  142. content: res.result.getJSON("res")?.getString("phoneNumber"),
  143. showCancel: false
  144. });
  145. }, 100);
  146. }).catch(err => {
  147. console.error(JSON.stringify(err));
  148. // 关闭登录页
  149. this.closePage();
  150. setTimeout(() => {
  151. uni.showModal({
  152. title: '取号失败',
  153. content: (err as Error).message,
  154. showCancel: false
  155. });
  156. }, 100);
  157. });
  158. },
  159. otherLogin() {
  160. //此处可实现其他登录方式
  161. uni.showToast({
  162. title: "使用其他方式登录",
  163. position: "bottom"
  164. })
  165. }
  166. }
  167. }
  168. </script>
  169. <style>
  170. .container {
  171. background-color: white;
  172. flex: 1;
  173. width: 100%;
  174. }
  175. .safe_content {
  176. padding-top: var(--status-bar-height);
  177. width: 100%;
  178. height: 100%;
  179. }
  180. .close_icon {
  181. left: 90%;
  182. top: 15px;
  183. font-family: uni-icon;
  184. font-size: 24px;
  185. /* font-weight: bold; */
  186. }
  187. .title {
  188. align-self: center;
  189. top: 20px;
  190. }
  191. .number {
  192. width: 100%;
  193. top: 25%;
  194. position: absolute;
  195. height: 120px;
  196. }
  197. .number-text {
  198. width: 100%;
  199. text-align: center;
  200. font-size: 28px;
  201. font-weight: bold;
  202. align-self: center;
  203. height: 30px;
  204. }
  205. .slogan-text {
  206. margin-top: 10px;
  207. width: 100%;
  208. font-size: 13px;
  209. text-align: center;
  210. align-self: center;
  211. color: gray;
  212. height: 20px;
  213. }
  214. .login-button {
  215. width: 80%;
  216. top: 40%;
  217. position: absolute;
  218. align-self: center;
  219. background-color: orangered;
  220. font-size: 16px;
  221. color: white;
  222. }
  223. .privacy {
  224. margin-top: 10px;
  225. flex-direction: row;
  226. flex-wrap: wrap;
  227. top: 45%;
  228. width: 100%;
  229. justify-content: center;
  230. align-self: center;
  231. position: absolute;
  232. }
  233. .privacy-checkbox {
  234. transform: scale(0.65);
  235. }
  236. .privacy-text {
  237. margin-top: 4px;
  238. color: #007aff;
  239. font-size: 14px;
  240. }
  241. .privacy-normal-text {
  242. margin-top: 4px;
  243. color: gray;
  244. font-size: 14px;
  245. }
  246. .other {
  247. bottom: 7%;
  248. transform: translateY(50%);
  249. position: absolute;
  250. align-self: center;
  251. font-size: 14px;
  252. }
  253. </style>