index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. <button open-type="getPhoneNumber" @getphonenumber="getPhonenumber" class="get_phone">抖音用户手机号授权登录</button>
  15. <!-- <button class="get_phone" style="margin-top: 40rpx; background-color: #fff; color: #333">手机号验证登录</button> -->
  16. <!-- #endif -->
  17. <button class="cancel_login" @click="cancelLogin()">取消登录</button>
  18. <view class="policy_content">
  19. <image
  20. class="checkbox"
  21. @click="changeAgreePolicy"
  22. :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'"
  23. ></image>
  24. 我已阅读并同意
  25. <navigator url="/pages/policy/index" style="color: #e03519">《服务协议》</navigator>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. agreePolicy: false,
  35. };
  36. },
  37. onLoad(options) {
  38. // 获取页面参数
  39. if (options.redirect) {
  40. this.redirect = options.redirect;
  41. this.activity_id = options.activity_id;
  42. this.id = options.id;
  43. }
  44. },
  45. methods: {
  46. changeAgreePolicy() {
  47. this.agreePolicy = !this.agreePolicy;
  48. },
  49. cancelLogin(){
  50. // 返回上一页
  51. uni.navigateBack();
  52. },
  53. getPhonenumber(re) {
  54. console.log(re.detail.errMsg);
  55. // // 如果授权失败的话
  56. if (re.detail.errMsg != "getPhoneNumber:ok") {
  57. uni.showToast({
  58. icon: "error",
  59. title: "授权失败",
  60. });
  61. return;
  62. }
  63. let share_uid = "";
  64. share_uid = uni.getStorageSync("share_uid");
  65. uni.login({
  66. provider: 'weixin',
  67. success: (loginRes) => {
  68. console.log(loginRes.code);
  69. // 登录成功,获取用户code
  70. let open_code = loginRes.code;
  71. // 授权成功以后,调用登录
  72. this.$http.request("api/wechat/phone_number", { code: re.detail.code, share_uid: share_uid, open_code: open_code }, "post").then((re) => {
  73. // 成功的话
  74. if (re.code == "success") {
  75. // 存储登录标识
  76. uni.setStorageSync("userLogin", re.data);
  77. if (!uni.getStorageSync("shopId")) {
  78. uni.setStorageSync("shopId", re.data.shop_id);
  79. }
  80. // 跳转到页面
  81. uni.switchTab({ url: "/pages/user/index" });
  82. } else {
  83. uni.showToast({
  84. title: re.msg,
  85. icon: "none",
  86. });
  87. }
  88. });
  89. },
  90. fail: (err) => {
  91. console.log('uni.login 接口调用失败,无法获取openid', err);
  92. uni.showToast({
  93. title: '支付失败',
  94. icon: 'none'
  95. });
  96. }
  97. });
  98. },
  99. toPhoneLogin(re) {
  100. // 登录效果
  101. this.$http.request("api/wechat/phone_number", this.requestParam).then((re) => {
  102. // 成功的话
  103. if (re.code == "success") {
  104. // 存储登录标识
  105. uni.setStorageSync("userLogin", re.data);
  106. // 跳转到页面
  107. uni.switchTab({ url: "/pages/user/index" });
  108. } else {
  109. uni.showToast({
  110. title: re.msg,
  111. icon: "none",
  112. });
  113. }
  114. });
  115. },
  116. },
  117. };
  118. </script>
  119. <style lang="less">
  120. .logo_box {
  121. display: block;
  122. margin: 0rpx auto;
  123. margin-top: 80rpx;
  124. .logo_image {
  125. width: 320rpx;
  126. height: 320rpx;
  127. display: block;
  128. margin: 0rpx auto;
  129. }
  130. }
  131. .login_content {
  132. padding: 0 40px;
  133. .policy_content {
  134. margin-top: 40rpx;
  135. font-size: 26rpx;
  136. display: flex;
  137. align-items: center;
  138. .checkbox {
  139. width: 40rpx;
  140. height: 40rpx;
  141. }
  142. }
  143. }
  144. .get_phone {
  145. display: block;
  146. width: 100%;
  147. height: 80rpx;
  148. color: #ffffff;
  149. font-size: 28rpx;
  150. background: green;
  151. margin: 0rpx auto;
  152. line-height: 80rpx;
  153. margin-top: 160rpx;
  154. border-radius: 30rpx;
  155. border: 0rpx solid #dddddd;
  156. }
  157. .get_phone::after {
  158. border: 0rpx solid #dddddd;
  159. }
  160. .cancel_login{
  161. display: block;
  162. width: 100%;
  163. height: 80rpx;
  164. color: #666666;
  165. font-size: 28rpx;
  166. background: #dddddd;
  167. margin: 0rpx auto;
  168. line-height: 80rpx;
  169. margin-top: 40rpx;
  170. border-radius: 30rpx;
  171. border: 0rpx solid #dddddd;
  172. }
  173. .cancel_login::after {
  174. border: 0rpx solid #dddddd;
  175. }
  176. </style>