index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view>
  3. <view class="logo_box">
  4. <image src="https://kailin-mp.oss-cn-shenzhen.aliyuncs.com/static/logo2.jpg" class="logo_image"></image>
  5. </view>
  6. <view class="login_content">
  7. <!-- #ifdef MP-WEIXIN -->
  8. <button open-type="getPhoneNumber" @getphonenumber="getPhonenumber" class="get_phone">授权登录</button>
  9. <!-- #endif -->
  10. <!-- #ifdef H5 -->
  11. <button @click="toPhoneLogin" class="get_phone">授权登录</button>
  12. <!-- #endif -->
  13. <!-- #ifdef MP-TOUTIAO -->
  14. <view>
  15. <button open-type="getPhoneNumber" @getphonenumber="getPhonenumber" class="get_phone">抖音用户手机号授权登录</button>
  16. <!-- <button class="get_phone" style="margin-top: 40rpx; background-color: #fff; color: #333">手机号验证登录</button> -->
  17. <view class="policy_content">
  18. <image
  19. class="checkbox"
  20. @click="changeAgreePolicy"
  21. :src="agreePolicy ? 'https://kailin-mp.oss-cn-shenzhen.aliyuncs.com/static/icon/checked.png' : 'https://kailin-mp.oss-cn-shenzhen.aliyuncs.com/static/icon/checkbox.png'"
  22. ></image>
  23. 我已阅读并同意
  24. <navigator url="/pages/policy/index" style="color: #e03519">《服务协议》</navigator>
  25. </view>
  26. </view>
  27. <!-- #endif -->
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. agreePolicy: false,
  36. };
  37. },
  38. onLoad(options) {
  39. // 获取页面参数
  40. if (options.redirect) {
  41. this.redirect = options.redirect;
  42. this.activity_id = options.activity_id;
  43. this.id = options.id;
  44. }
  45. },
  46. methods: {
  47. changeAgreePolicy() {
  48. this.agreePolicy = !this.agreePolicy;
  49. },
  50. methods: {
  51. getPhonenumber(re) {
  52. // // 如果授权失败的话
  53. if (re.detail.errMsg != "getPhoneNumber:ok") {
  54. uni.showToast({
  55. icon: "error",
  56. title: "授权失败",
  57. });
  58. return;
  59. }
  60. let share_uid = "";
  61. share_uid = uni.getStorageSync("share_uid");
  62. // 授权成功以后,调用登录
  63. this.$http.request("api/wechat/phone_number", { code: re.detail.code, share_uid: share_uid }, "post").then((re) => {
  64. // 成功的话
  65. if (re.code == "success") {
  66. // 存储登录标识
  67. uni.setStorageSync("userLogin", re.data);
  68. // 跳转到页面
  69. if (this.redirect) {
  70. let url = this.redirect;
  71. if (this.activity_id) url = url + "?id=" + this.activity_id;
  72. if (this.id) url = url + "?id=" + this.id;
  73. uni.redirectTo({ url: url });
  74. } else uni.switchTab({ url: "/pages/user/index" });
  75. } else {
  76. uni.showToast({
  77. title: re.msg,
  78. icon: "none",
  79. });
  80. }
  81. });
  82. },
  83. toPhoneLogin(re) {
  84. // 登录效果
  85. this.$http.request("api/wechat/phone_number", this.requestParam).then((re) => {
  86. // 成功的话
  87. if (re.code == "success") {
  88. // 存储登录标识
  89. uni.setStorageSync("userLogin", re.data);
  90. // 跳转到页面
  91. uni.switchTab({ url: "/pages/user/index" });
  92. } else {
  93. uni.showToast({
  94. title: re.msg,
  95. icon: "none",
  96. });
  97. }
  98. });
  99. return;
  100. },
  101. },
  102. getPhonenumber(re) {
  103. // // 如果授权失败的话
  104. if (re.detail.errMsg != "getPhoneNumber:ok") {
  105. uni.showToast({
  106. icon: "error",
  107. title: "授权失败",
  108. });
  109. return;
  110. }
  111. // 授权成功以后,调用登录
  112. this.$http.request("api/wechat/phone_number", { code: re.detail.code }, "post").then((re) => {
  113. // 成功的话
  114. if (re.code == "success") {
  115. // 存储登录标识
  116. uni.setStorageSync("userLogin", re.data);
  117. // 跳转到页面
  118. if (this.redirect) {
  119. let url = this.redirect;
  120. if (this.activity_id) url = url + "?id=" + this.activity_id;
  121. if (this.id) url = url + "?id=" + this.id;
  122. uni.redirectTo({ url: url });
  123. } else uni.switchTab({ url: "/pages/user/index" });
  124. } else {
  125. uni.showToast({
  126. title: re.msg,
  127. icon: "none",
  128. });
  129. }
  130. });
  131. },
  132. toPhoneLogin(re) {
  133. // 登录效果
  134. this.$http.request("api/wechat/phone_number", this.requestParam).then((re) => {
  135. // 成功的话
  136. if (re.code == "success") {
  137. // 存储登录标识
  138. uni.setStorageSync("userLogin", re.data);
  139. // 跳转到页面
  140. uni.switchTab({ url: "/pages/user/index" });
  141. } else {
  142. uni.showToast({
  143. title: re.msg,
  144. icon: "none",
  145. });
  146. }
  147. });
  148. },
  149. },
  150. };
  151. </script>
  152. <style lang="less">
  153. .logo_box {
  154. display: block;
  155. margin: 0rpx auto;
  156. margin-top: 80rpx;
  157. .logo_image {
  158. width: 320rpx;
  159. height: 320rpx;
  160. display: block;
  161. margin: 0rpx auto;
  162. }
  163. }
  164. .login_content {
  165. padding: 0 40px;
  166. .policy_content {
  167. margin-top: 40rpx;
  168. font-size: 26rpx;
  169. display: flex;
  170. align-items: center;
  171. .checkbox {
  172. width: 40rpx;
  173. height: 40rpx;
  174. }
  175. }
  176. }
  177. .get_phone {
  178. display: block;
  179. width: 100%;
  180. height: 80rpx;
  181. color: #ffffff;
  182. font-size: 28rpx;
  183. background: green;
  184. margin: 0rpx auto;
  185. line-height: 80rpx;
  186. margin-top: 160rpx;
  187. border-radius: 30rpx;
  188. border: 0rpx solid #dddddd;
  189. }
  190. .get_phone::after {
  191. border: 0rpx solid #dddddd;
  192. }
  193. </style>