index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="container main">
  3. <view class="overlay">
  4. <view class="one">
  5. 欢迎登录!
  6. </view>
  7. <view class="two">
  8. <uni-forms ref="baseForm" :rules="rules" :modelValue="form">
  9. <uni-forms-item label="账号" required name="account">
  10. <uni-easyinput v-model="form.account" placeholder="请输入账号" />
  11. </uni-forms-item>
  12. <uni-forms-item label="密码" required name="password">
  13. <uni-easyinput type="password" v-model="form.password" placeholder="请输入密码" />
  14. </uni-forms-item>
  15. </uni-forms>
  16. <button class="button_1" type="primary" @click="submit('baseForm')">登录</button>
  17. <button class="button_2" type="primary" @click="toRegister">注册</button>
  18. </view>
  19. <view class="thr">
  20. <view class="thr_1" @tap="toLogin">
  21. <text class="t-icon t-icon-weixin"></text>
  22. <text>微信信任登录>></text>
  23. </view>
  24. </view>
  25. <view class="four">
  26. <checkbox-group @change="changeAgree">
  27. <label>
  28. <checkbox color="#2979ff" :checked="agree" />
  29. <text @tap.stop="toAgree()">我已阅读并同意<text style="color: #2979ff;"
  30. @click="toAgree">《用户服务协议》</text>及
  31. <text style="color: #2979ff;" @click="toAgree">《隐私策略》</text>
  32. </text>
  33. </label>
  34. </checkbox-group>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. openid: '',
  44. agree: false,
  45. form: {
  46. account: '',
  47. password: ''
  48. },
  49. // 校验规则
  50. rules: {
  51. account: {
  52. rules: [{
  53. required: true,
  54. errorMessage: '账号不能为空'
  55. }]
  56. },
  57. password: {
  58. rules: [{
  59. required: true,
  60. errorMessage: '密码不能为空'
  61. }]
  62. }
  63. },
  64. }
  65. },
  66. onShow: function() {
  67. const that = this;
  68. // 查询平台信息
  69. that.searchOpenids();
  70. },
  71. methods: {
  72. async searchOpenids() {
  73. const that = this;
  74. uni.getStorage({
  75. key: 'openid',
  76. success: function(res) {
  77. that.$set(that, `openid`, res.data);
  78. },
  79. fail: function(err) {
  80. uni.login({
  81. success: async function(res) {
  82. if (res.code) {
  83. const aee = await that.$app('/wechat/api/login/app',
  84. 'GET', {
  85. js_code: res.code,
  86. config: that.$config.wx_projectkey
  87. })
  88. if (aee.errcode == '0') {
  89. uni.setStorage({
  90. key: "openid",
  91. data: aee.data.openid
  92. })
  93. that.$set(that, `openid`, aee.data.openid);
  94. } else {
  95. uni.showToast({
  96. title: aee.errmsg,
  97. icon: 'none'
  98. })
  99. }
  100. } else {
  101. uni.showToast({
  102. title: res.errMsg,
  103. icon: 'none'
  104. })
  105. }
  106. }
  107. });
  108. }
  109. })
  110. },
  111. // 登录
  112. submit(ref) {
  113. const that = this;
  114. let agree = that.agree;
  115. let openid = that.openid;
  116. if (agree) {
  117. if (openid) {
  118. that.$refs[ref].validate().then(async res => {
  119. const arr = await that.$api(`/login/User`, 'POST', res);
  120. if (arr.errcode == '0') {
  121. const user = that.$jwt(arr.data);
  122. if (!user.openid) {
  123. const openidUser = await that.$api(`/user/${user.id}`, 'POST', {
  124. openid
  125. });
  126. }
  127. uni.setStorage({
  128. key: 'token',
  129. data: arr.data,
  130. success: function(res) {
  131. uni.navigateBack({
  132. delta: 1
  133. })
  134. },
  135. fail: function(err) {
  136. console.log(err);
  137. }
  138. })
  139. } else {
  140. uni.showToast({
  141. title: arr.errmsg,
  142. icon: 'none'
  143. })
  144. }
  145. }).catch(err => {
  146. console.log('err', err);
  147. })
  148. } else {
  149. uni.showToast({
  150. title: '系统更新中,请稍后再试!',
  151. icon: 'none'
  152. })
  153. }
  154. } else {
  155. uni.showToast({
  156. title: '请阅读并同意用户协议和隐私政策',
  157. icon: 'none'
  158. })
  159. }
  160. },
  161. // 微信登录
  162. async toLogin() {
  163. const that = this;
  164. let agree = that.agree;
  165. if (agree) {
  166. const aee = await that.$api(`/login/wxapp/User`, 'POST', {
  167. openid: that.openid
  168. })
  169. if (aee.errcode == '0') {
  170. uni.setStorage({
  171. key: 'token',
  172. data: aee.data,
  173. success: function(res) {
  174. uni.navigateBack({
  175. delta: 1
  176. })
  177. },
  178. fail: function(err) {
  179. console.log(err);
  180. }
  181. })
  182. } else {
  183. uni.showToast({
  184. title: aee.errmsg + ` 请注册账号!`,
  185. icon: 'none'
  186. })
  187. }
  188. } else {
  189. uni.showToast({
  190. title: '请阅读并同意用户协议和隐私政策',
  191. icon: 'none'
  192. })
  193. }
  194. },
  195. // 同意隐私协议
  196. changeAgree() {
  197. const that = this;
  198. that.$set(that, `agree`, !that.agree);
  199. },
  200. // 查看协议
  201. toAgree() {
  202. uni.navigateTo({
  203. url: '/pagesMy/agree/index'
  204. })
  205. },
  206. // 注册
  207. toRegister() {
  208. uni.navigateTo({
  209. url: '/pagesHome/register/index'
  210. })
  211. },
  212. }
  213. }
  214. </script>
  215. <style lang="scss">
  216. .main {
  217. position: relative;
  218. // background-image: url('../../static/loginbg.jpg');
  219. // background-repeat: no-repeat;
  220. // background-size: cover;
  221. // background-position: center;
  222. .overlay {
  223. position: absolute;
  224. top: 0;
  225. left: 0;
  226. right: 0;
  227. bottom: 0;
  228. background-color: rgba(255, 255, 255, 0.5);
  229. .one {
  230. text-align: center;
  231. padding: 5vw 0;
  232. font-family: "宋体";
  233. font-size: var(--font20Size);
  234. font-weight: bold;
  235. }
  236. /* 白色半透明背景 */
  237. .two {
  238. padding: 2vw 5vw;
  239. .button_1 {
  240. color: var(--mainColor);
  241. background: linear-gradient(90deg, #1E83FF, #1071e6);
  242. }
  243. .button_2 {
  244. margin: 3vw 0 0 0;
  245. color: var(--mainColor);
  246. background: linear-gradient(90deg, #FFCD1E, #FF8A18);
  247. }
  248. }
  249. .thr {
  250. padding: 5vw;
  251. .thr_1 {
  252. display: flex;
  253. align-items: center;
  254. justify-content: flex-end;
  255. font-size: var(--font14Size);
  256. color: var(--f80Color);
  257. text {
  258. margin: 0 0 0 1vw;
  259. }
  260. }
  261. }
  262. .four {
  263. position: absolute;
  264. bottom: 15px;
  265. padding: 5vw;
  266. text-align: center;
  267. font-size: var(--font12Size);
  268. }
  269. }
  270. }
  271. </style>