公众号前端
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

334 satır
13 KiB

  1. <template>
  2. <div class="app-container">
  3. <!-- <van-tabs :swipeable="true" style="margin-top:0.5rem;padding:0 10px;">-->
  4. <van-form style="margin:50px 0;">
  5. <!-- <van-field-->
  6. <!-- v-model="formData.memberName"-->
  7. <!-- name="请输入姓名"-->
  8. <!-- placeholder="请输入姓名"-->
  9. <!-- :rules="[{ required: true, message: '' }]"-->
  10. <!-- />-->
  11. <!-- <van-field-->
  12. <!-- v-model="formData.idcard"-->
  13. <!-- name="请输入身份证号"-->
  14. <!-- style="margin-top: 20px"-->
  15. <!-- placeholder="请输入身份证号"-->
  16. <!-- :rules="[{ required: true, message: '' }]"-->
  17. <!-- />-->
  18. <van-field
  19. v-model="formData.mobile"
  20. name="请输入手机号"
  21. style="margin-top: 20px"
  22. placeholder="请输入手机号"
  23. :rules="[{ required: true, message: '' }]"
  24. />
  25. <!-- <van-field-->
  26. <!-- v-model="formData.code"-->
  27. <!-- center-->
  28. <!-- clearable-->
  29. <!-- label="验证码"-->
  30. <!-- placeholder="图形验证码"-->
  31. <!-- >-->
  32. <!-- <template #button>-->
  33. <!-- <img style="width: 100px" :src="codeUrl" @click="getCode"/>-->
  34. <!-- </template>-->
  35. <!-- </van-field>-->
  36. <van-field
  37. v-model="formData.smsCode"
  38. style="margin-top: 20px"
  39. placeholder="请输入验证码"
  40. :rules="[{ required: true, message: '' }]"
  41. >
  42. <template #button>
  43. <!-- <van-button size="mini" type="info" @click="getRegisterSmsCode" >获取验证码</van-button> -->
  44. <div class="registerSmsBtn" @click="getRegisterSmsCode">{{
  45. computeTime > 0 ? `(${computeTime}s)已发送` : "获取短信码"
  46. }}
  47. </div>
  48. </template>
  49. </van-field>
  50. <div style="margin: 50px 16px 16px;">
  51. <van-button block type="info" native-type="submit" @click="registerSubmit">绑定</van-button>
  52. </div>
  53. </van-form>
  54. <!-- </van-tabs>-->
  55. </div>
  56. </template>
  57. <style scoped>
  58. .app-container {
  59. background: #fff;
  60. height: 100vh;
  61. }
  62. .title {
  63. padding-top: 20%;
  64. width: 88%;
  65. margin: 0 auto;
  66. }
  67. .van-tab--active {
  68. font-size: .6rem;
  69. font-weight: bold;
  70. }
  71. .van-tabs__line {
  72. background: #1D6FE9;
  73. width: 0.15rem;
  74. height: 0.15rem;
  75. border-radius: 0.07rem;
  76. bottom: 0.3rem;
  77. }
  78. .van-tabs__nav {
  79. padding: 0
  80. }
  81. .van-tab {
  82. display: inline-block;
  83. flex: inherit;
  84. margin-left: 30px;
  85. line-height: .8rem;
  86. }
  87. .van-tab__text--ellipsis {
  88. overflow: auto;
  89. }
  90. .van-password-input {
  91. width: 50%;
  92. margin: 0 auto;
  93. }
  94. [class*=van-hairline]::after {
  95. border: none;
  96. }
  97. .van-password-input__security li {
  98. margin: 0 10px;
  99. border-bottom: 3px solid black;
  100. }
  101. .registerSmsBtn {
  102. color: rgb(29, 111, 233);
  103. font-size: 1rem;
  104. }
  105. </style>
  106. <script>
  107. import {getUUID} from '@/utils'
  108. const APPID = "wx7135dd84735bc392";
  109. // 静默授权:scope=snsapi_base
  110. // const WX_AUTH_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID
  111. // + '&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=0#wechat_redirect';
  112. // 非静默授权:scope=snsapi_userinfo
  113. const WX_AUTH_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID
  114. + '&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect';
  115. export default {
  116. data() {
  117. return {
  118. showMessage: false,
  119. smsCodeValue: "",
  120. showKeyboard: false,
  121. formData: {
  122. // username: "", //账号
  123. // password: "", //密码
  124. code: null, //图片验证码
  125. uuid: null, //识别uuid
  126. mobile: null, //手机号
  127. smsCode: null, //短信验证码
  128. codeTime: null, //短信验证码发送时间
  129. openId: null, // openId
  130. // memberName: null, //身份信息
  131. // idcard: null, //身份号码
  132. // rememberMe: false
  133. },
  134. loading: false,
  135. codeUrl: "", //验证码
  136. isSmsLogin: false, //是否手机验证码
  137. computeTime: 0,
  138. headimgurl: "",
  139. nickname: "",
  140. };
  141. },
  142. created() {
  143. // this.getCode();
  144. this.wxAuth();
  145. },
  146. methods: {
  147. showPopup() {
  148. this.showKeyboard = !this.showKeyboard
  149. },
  150. showMessagePop() {
  151. this.showMessage = !this.showMessage
  152. },
  153. wxAuth() {
  154. // 非静默授权:snsapi_userinfo,有弹框弹出需要用户手动点击确认授权。可以获取openId,用户的头像、昵称等
  155. return new Promise((resolve, reject) => {
  156. let openid = this.$cookie.get('openid');
  157. if (openid) {
  158. resolve(openid);
  159. return;
  160. }
  161. let code = this.getUrlParam('code');
  162. if (!code) {//未经过微信授权
  163. /**
  164. * 获取网页授权登录code
  165. * 如果有直接返回,如果没有重定向到腾讯授权页面,腾讯会再次重定向回来并带有code参数
  166. **/
  167. let code = this.getUrlParam('code');//获取URL中的code参数
  168. if (code) return code;
  169. let currentUrl = encodeURIComponent(window.location.href);
  170. window.location.replace(WX_AUTH_URL.replace('REDIRECT_URI', currentUrl));
  171. } else {
  172. this.$cookie.set('appid', APPID); //接口服务端从cookie中读取openid
  173. this.$http({
  174. url: this.$http.adornUrl('/wxAuth/codeToUserInfo'),
  175. method: 'post',
  176. data: this.$http.adornParams({
  177. 'code': code
  178. })
  179. }).then(res => {
  180. if (res.data.code == 200) {
  181. console.log("微信授权完成");
  182. this.headimgurl = res.data.headimgurl;
  183. this.nickname = res.data.nickname;
  184. console.log(this.headimgurl);
  185. console.log(this.nickname);
  186. } else {
  187. console.log("换取openid失败");
  188. }
  189. });
  190. }
  191. });
  192. // 静默授权:snsapi_base,没有弹窗,只能获取用户的openId。
  193. // return new Promise((resolve, reject) => {
  194. // let openid = this.$cookie.get('openid');
  195. // if (openid) {
  196. // resolve(openid);
  197. // return;
  198. // }
  199. // let code = this.getUrlParam('code');
  200. // if (!code) {//未经过微信授权
  201. // let currentUrl = encodeURIComponent(window.location.href);
  202. // window.location.replace(WX_AUTH_URL.replace('REDIRECT_URI', currentUrl));
  203. // } else {
  204. // this.$cookie.set('appid', APPID); //接口服务端从cookie中读取openid
  205. // this.$http({
  206. // url: this.$http.adornUrl('/wxAuth/codeToOpenid'),
  207. // method: 'post',
  208. // data: this.$http.adornParams({
  209. // 'code': code
  210. // })
  211. // }).then(res => {
  212. // if (res.code == 200) {
  213. // console.log("微信授权完成" + res.data);
  214. // resolve(res.data);
  215. // } else {
  216. // console.log("换取openid失败");
  217. // }
  218. // });
  219. // }
  220. // });
  221. },
  222. getUrlParam(key) {//获取当前页面url中的参数
  223. var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
  224. var result = window.location.search.substr(1).match(reg);
  225. return result ? decodeURIComponent(result[2]) : '';
  226. },
  227. getCode() {
  228. this.formData.uuid = getUUID()
  229. this.codeUrl = this.$http.adornUrl(`/captcha?uuid=${this.formData.uuid}`)
  230. },
  231. getRegisterSmsCode() {
  232. if (!this.computeTime) {
  233. let myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  234. if (!myreg.test(this.formData.mobile)) {
  235. this.$dialog.alert({
  236. message: '手机号格式不正确',
  237. });
  238. return false;
  239. }
  240. // else if (this.formData.code == "") {
  241. // this.$dialog.alert({
  242. // message: '图片验证码不能为空',
  243. // });
  244. // return false;
  245. // }
  246. this.$http({
  247. url: this.$http.adornUrl('/sms/code'),
  248. method: 'post',
  249. data: this.$http.adornParams({
  250. 'mobile': this.formData.mobile
  251. })
  252. }).then((res) => {
  253. if (res.data.code == 200) {
  254. this.$dialog.alert({
  255. message: '验证码已发送',
  256. });
  257. this.formData.codeTime = res.data.smsCode;
  258. this.computeTime = 60;
  259. this.timer = setInterval(() => {
  260. this.computeTime--;
  261. if (this.computeTime <= 0) {
  262. clearInterval(this.timer);
  263. }
  264. }, 1000);
  265. } else {
  266. this.$dialog.alert({
  267. message: res.data.msg,
  268. });
  269. }
  270. }).catch((res) => {
  271. this.$dialog.alert({
  272. message: '验证码发送失败' + res.data.msg,
  273. });
  274. });
  275. }
  276. },
  277. registerSubmit() {
  278. //绑定
  279. if (this.formData.mobile == "") {
  280. this.$dialog.alert({
  281. message: '手机号码不能为空',
  282. });
  283. return false;
  284. } else if (this.formData.smsCode == "") {
  285. this.$dialog.alert({
  286. message: '短信验证码不能为空',
  287. });
  288. return false;
  289. }
  290. this.$http({
  291. url: this.$http.adornUrl('/sms/verifyCode'),
  292. method: 'post',
  293. data: this.$http.adornParams({
  294. 'openId': this.$cookie.get('openid'),
  295. 'headimgurl': this.headimgurl,
  296. 'nickname': this.nickname,
  297. 'mobile': this.formData.mobile,
  298. 'code': this.formData.smsCode,
  299. 'codeTime': this.formData.codeTime
  300. })
  301. }).then((res) => {
  302. if (res.data.code == 200) {
  303. this.$dialog.alert({
  304. message: '手机号绑定成功',
  305. });
  306. } else {
  307. this.$dialog.alert({
  308. message: res.data.msg,
  309. });
  310. }
  311. }).catch((res) => {
  312. this.$dialog.alert({
  313. message: '手机号绑定失败' + res.data.msg,
  314. });
  315. });
  316. }
  317. },
  318. };
  319. </script>