index.vue 5.2 KB

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