verifyManager.uvue 7.5 KB

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