code_login.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <view class="content">
  3. <view class="title">验证码登录</view>
  4. <view class="form vflex">
  5. <u-input v-model="userInfo.name" placeholder="用户名/手机号"></u-input>
  6. <u-input v-model="code" placeholder="请输入验证码">
  7. <template slot="suffix">
  8. <u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
  9. <view class="code" @tap="getCode" >{{tips}}</view>
  10. </template>
  11. </u-input>
  12. </view>
  13. <view class="hflex acenter">
  14. <u-checkbox-group @change="checkboxChange" iconSize="20">
  15. <u-checkbox v-model="agree" shape="circle" iconSize="20"></u-checkbox>
  16. </u-checkbox-group>
  17. <view class="text">阅读并同意<span class="read" @click="open(0)">《用户服务协议》</span>、<span class="read" @click="open(1)">《个人信息保护政策》</span></view>
  18. </view>
  19. <view class="button hflex acenter jcenter" @click="login">
  20. <view>登录</view>
  21. </view>
  22. <view class="hflex acenter jcenter bottom">
  23. <view class="text_style1" @click="pwdLogin">账号密码登录</view>
  24. <view style="margin: 0 10rpx;">|</view>
  25. <view class="text_style2" @click="register">注册</view>
  26. </view>
  27. <!-- 弹出层 -->
  28. <u-modal :show="showProtocol" :title="protocolTitle" confirmColor="#2988FE" @confirm="isRead" confirmText="我已阅读">
  29. <view class="slot-content">
  30. <u-parse :content="protocolContent"></u-parse>
  31. <!-- <rich-text :nodes="protocolContent"></rich-text> -->
  32. </view>
  33. </u-modal>
  34. </view>
  35. </template>
  36. <script>
  37. import $api from '@/static/js/api.js'
  38. var that = ''
  39. export default {
  40. data() {
  41. return {
  42. userInfo: {
  43. name: '',
  44. },
  45. code: '',
  46. tips: '',
  47. agree: false,
  48. showProtocol: false,
  49. protocolTitle: '',
  50. protocolContent: '',
  51. }
  52. },
  53. onLoad() {
  54. that = this
  55. },
  56. methods: {
  57. // 验证码文字
  58. codeChange(text) {
  59. that.tips = text
  60. },
  61. // 获取验证码
  62. getCode() {
  63. if(that.$refs.uCode.canGetCode) {
  64. uni.showLoading({
  65. title: '正在获取验证码'
  66. })
  67. $api.req({
  68. url: '/data/api.Login/sendsms',
  69. method: 'POST',
  70. data: {
  71. phone: that.userInfo.name,
  72. type: 1
  73. }
  74. }, function(res) {
  75. uni.hideLoading();
  76. if(res.code == 1) {
  77. uni.$u.toast('验证码已发送')
  78. that.$refs.uCode.start()
  79. } else {
  80. uni.$u.toast('验证码发送失败')
  81. }
  82. })
  83. } else {
  84. uni.$u.toast('倒计时结束后再发送')
  85. }
  86. },
  87. // 阅读并同意
  88. checkboxChange(n) {
  89. this.agree = !this.agree
  90. },
  91. // 打开弹出层
  92. open(index) {
  93. const that = this
  94. $api.req({
  95. url: '/data/api.Login/getloginset'
  96. }, function(res) {
  97. if (res.code == 1) {
  98. if(index == 0) {
  99. uni.setStorageSync('xieyi',res.data.service_agreement)
  100. $api.jump('/pages/user/agreement?title=用户服务协议')
  101. } else {
  102. uni.setStorageSync('xieyi',res.data.protection_policy)
  103. $api.jump('/pages/user/agreement?title=个人隐私保护政策')
  104. }
  105. }
  106. })
  107. },
  108. isRead() {
  109. that.showProtocol = false
  110. },
  111. // 登录
  112. login() {
  113. if ($api.formCheck(that.userInfo.name,"phone") && $api.formCheck(that.code, "code6")) {
  114. if (!that.agree) {
  115. $api.info('请先阅读并同意用户协议和隐私协议')
  116. } else {
  117. wx.getSetting({
  118. success(res) {
  119. if (res.authSetting['scope.userInfo']) {
  120. wx.login({
  121. success: function(res) {
  122. uni.request({
  123. url: $api.config.baseUrl + '/data/api.Login/getOpenid',
  124. method: 'POST',
  125. data: {
  126. code: res.code
  127. },
  128. header: {
  129. 'content-type': 'application/json',
  130. 'api-name': 'wxapp'
  131. },
  132. success:(res)=> {
  133. console.log(res);
  134. if(res.data.code == 1) {
  135. $api.req({
  136. url: '/data/api.Login/sms',
  137. method: 'POST',
  138. data: {
  139. phone: that.userInfo.name,
  140. verify: that.code,
  141. openid: res.data.data.openid
  142. }
  143. }, function(res) {
  144. console.log(res)
  145. if(res.code == 1) {
  146. uni.setStorageSync("token",res.data.token)
  147. uni.setStorageSync("id",res.data.id)
  148. // var options = {
  149. // user: res.data.id.toString(),
  150. // pwd: '123456',
  151. // appKey: uni.WebIM.config.appkey,
  152. // success: function (res2) {
  153. // wx.setStorageSync("HXtoken",res2.access_token)
  154. // $api.jump('/pages/tabbar/mine/mine',3)
  155. // },
  156. // error: function(error){
  157. // console.log(error);
  158. // }
  159. // };
  160. // uni.WebIM.conn.open(options);
  161. uni.WebIM.conn.open({
  162. user: res.data.huanxinID,
  163. pwd: "12345678",
  164. appKey: uni.WebIM.config.appkey
  165. }).then((res2) => {
  166. console.log("login success",res2);
  167. uni.setStorageSync("myUsername",res.data.huanxinID)
  168. }).catch((reason) => {
  169. console.log("login fail", reason);
  170. });
  171. $api.jump('/pages/tabbar/mine/mine',3)
  172. }
  173. })
  174. }
  175. }
  176. })
  177. }
  178. })
  179. }
  180. }
  181. })
  182. }
  183. }
  184. },
  185. // 账号密码登录
  186. pwdLogin() {
  187. $api.jump('/pages/login/password_login')
  188. },
  189. // 注册
  190. register() {
  191. $api.jump('/pages/login/register')
  192. }
  193. },
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .content::v-deep {
  198. padding: 0 60rpx;
  199. .title {
  200. margin: 64rpx 0;
  201. font-size: 52rpx;
  202. color: #222;
  203. }
  204. .form {
  205. margin-bottom: 100rpx;
  206. .u-input {
  207. width: 630rpx !important;
  208. height: 104rpx !important;
  209. background-color: #f4f4f4;
  210. border-radius: 52rpx;
  211. font-size: 30rpx !important;
  212. box-sizing: border-box;
  213. padding: 30rpx 48rpx !important;
  214. margin: 26rpx 0;
  215. }
  216. .pwd_icon {
  217. width: 40rpx;
  218. height: 40rpx;
  219. }
  220. .code {
  221. font-size: 30rpx;
  222. color: #506dff;
  223. }
  224. }
  225. .text {
  226. font-size: 24rpx;
  227. color: #9c9c9c;
  228. }
  229. .read {
  230. color: #2a63f3;
  231. }
  232. .button {
  233. width: 100%;
  234. height: 96rpx;
  235. background-color: #506dff;
  236. border-radius: 50rpx;
  237. box-shadow: 0 4rpx 28rpx 0 rgba(132,123,255,0.4);
  238. font-size: 40rpx;
  239. color: #fff;
  240. margin: 40rpx 0 36rpx;
  241. }
  242. .bottom {
  243. width: 100%;
  244. .text_style1 {
  245. font-size: 24rpx;
  246. color: #555;
  247. }
  248. .text_style2 {
  249. font-size: 24rpx;
  250. color: #506dff;
  251. }
  252. }
  253. }
  254. </style>