login.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <view>
  3. <block v-if="modal == true ? 'show' : ''">
  4. <view class="login-bg"></view>
  5. <view class="login-box">
  6. <view class="login-title">你还没登录</view>
  7. <text>请选择以下任一登录方式</text>
  8. <text>登录后再进行操作</text>
  9. <image src="/static/login.png" mode="widthFix"></image>
  10. <!-- #ifdef MP-WEIXIN -->
  11. <view class="login-btnbox">
  12. <button
  13. class="login-btn"
  14. hover-class="none"
  15. @tap="getUserProfile"
  16. style="background-color: #09ba07; color: #fff"
  17. >
  18. 微信登录
  19. </button>
  20. <button
  21. class="login-btn"
  22. hover-class="none"
  23. @click="goLogin"
  24. style="background-color: #5677fc; color: #fff"
  25. >
  26. 账号登录
  27. </button>
  28. </view>
  29. <!-- #endif -->
  30. <!-- #ifndef MP-WEIXIN -->
  31. <view class="login-btnbox" v-if="showRegBtn">
  32. <view
  33. class="login-btn"
  34. v-on:click="goLogin"
  35. style="
  36. color: #333;
  37. background-color: #5677fc;
  38. color: #fff;
  39. width: 88%;
  40. "
  41. >账号登录</view
  42. >
  43. </view>
  44. <!-- #endif -->
  45. <view class="login-btnbox">
  46. <view
  47. class="login-btn"
  48. v-on:click="hideModal"
  49. style="color: #333; background-color: #f1f1f1; width: 88%"
  50. >暂不登录
  51. </view>
  52. </view>
  53. </view>
  54. </block>
  55. <wx-user-info-modal
  56. v-model="showAuthorizationModal"
  57. @updated="updatedUserInfoEvent"
  58. ></wx-user-info-modal>
  59. <kz-wx-privacy-check ref="kzWxPrivacyCheck"></kz-wx-privacy-check>
  60. </view>
  61. </template>
  62. <script>
  63. import userApi from "@/common/api/user.js";
  64. import WxUserInfoModal from "@/uni_modules/tuniaoui-wx-user-info/components/tuniaoui-wx-user-info/tuniaoui-wx-user-info.vue";
  65. export default {
  66. name: "login",
  67. components: { WxUserInfoModal },
  68. props: {
  69. show: {
  70. default: true,
  71. },
  72. pid: {
  73. default: 0,
  74. },
  75. },
  76. data() {
  77. return {
  78. modal: false,
  79. showAuthorizationModal: false,
  80. afterMethod: null,
  81. // system: null,
  82. showRegBtn: true,
  83. };
  84. },
  85. mounted() {
  86. if (this.show && !uni.getStorageSync("user")) {
  87. //this.modal = true;
  88. }
  89. // let system = uni.getStorageSync("system");
  90. // if (
  91. // system &&
  92. // system.open_user_register != undefined &&
  93. // parseInt(system.open_user_register) === 0
  94. // ) {
  95. // this.showRegBtn = false;
  96. // }
  97. },
  98. methods: {
  99. goLogin() {
  100. this.utils.goto("/pages/user/login-reg");
  101. },
  102. async getUserProfile(e) {
  103. let isPrivacySetting =
  104. await this.$refs.kzWxPrivacyCheck.getPrivacySetting("showPrivacy");
  105. console.log("isPrivacySetting", isPrivacySetting);
  106. if (isPrivacySetting) {
  107. var that = this;
  108. uni.getUserProfile({
  109. lang: "zh_CN",
  110. desc: "用于完善会员资料",
  111. success: (res) => {
  112. uni.login({
  113. provider: "weixin",
  114. success(e) {
  115. that.wxlogin(e.code, res.userInfo);
  116. },
  117. });
  118. },
  119. fail: (err) => {
  120. console.log(err);
  121. },
  122. });
  123. }
  124. },
  125. wxlogin(code, userInfo) {
  126. let that = this;
  127. this.http(
  128. "User/login",
  129. {
  130. code: code,
  131. userInfo: userInfo,
  132. pid: this.pid,
  133. },
  134. "post"
  135. ).then((res) => {
  136. if (res.code == 0) {
  137. uni.showModal({
  138. content: "请求失败:" + res.msg,
  139. showCancel: false,
  140. success: function () {
  141. if (callBack != undefined) {
  142. callBack();
  143. }
  144. },
  145. });
  146. uni.$emit("login_fail", "请求失败:" + res.msg);
  147. } else {
  148. if (res.data.user.status == "hidden") {
  149. uni.showModal({
  150. content: "账号已被禁用,请联系管理员重新开启!",
  151. showCancel: false,
  152. success: function () {
  153. if (callBack != undefined) {
  154. callBack();
  155. }
  156. },
  157. });
  158. uni.$emit("login_fail", "账号已被禁用,请联系管理员重新开启!");
  159. } else {
  160. uni.setStorageSync("token", res.data.token);
  161. uni.setStorageSync("user", res.data.user);
  162. uni.$emit("login_success", res.data);
  163. this.modal = false;
  164. console.log("this.afterMethod", this.afterMethod);
  165. // 登录后执行之前的操作
  166. if (this.afterMethod) {
  167. this.afterMethod();
  168. // 置空
  169. this.afterMethod = null;
  170. }
  171. if (res.data.user.nickname == "微信用户") {
  172. this.showAuthorizationModal = true;
  173. console.log(
  174. "showAuthorizationModal",
  175. this.showAuthorizationModal
  176. );
  177. }
  178. this.$emit("succ", res.data);
  179. }
  180. }
  181. });
  182. },
  183. hideModal(e) {
  184. this.modal = false;
  185. },
  186. // 获取到的用户信息
  187. async updatedUserInfoEvent(info) {
  188. console.log("获取到的用户信息", info);
  189. let newAvatar = "";
  190. let uploadResult = "";
  191. // 上传头像
  192. if (info.avatar && info.avatar.indexOf("tmp") > 0) {
  193. await this.utils.uploadFile(this, info.avatar).then((res) => {
  194. if (res.code == 1) {
  195. newAvatar = res.data.fullurl;
  196. uploadResult = "";
  197. } else {
  198. uploadResult = res.msg;
  199. }
  200. });
  201. }
  202. console.log("uploadResult", uploadResult);
  203. console.log("newAvatar", newAvatar);
  204. if (uploadResult) {
  205. this.utils.alert(uploadResult);
  206. return;
  207. }
  208. let data = {
  209. nickname: info.nickname,
  210. };
  211. if (newAvatar) {
  212. data["avatar"] = newAvatar;
  213. }
  214. if (info.mobile) {
  215. data["mobile"] = info.mobile;
  216. }
  217. console.log("save user data", data);
  218. await userApi.save(this, data).then((res) => {
  219. console.log("user save res", res);
  220. if (res.code == 1) {
  221. uni.setStorageSync("user", this.user);
  222. this.showAuthorizationModal = false;
  223. uni.$emit("login_success", res.data);
  224. this.utils.alert("保存成功");
  225. } else {
  226. this.utils.alert(res.msg);
  227. }
  228. });
  229. },
  230. },
  231. };
  232. </script>
  233. <style>
  234. .login-bg {
  235. width: 100%;
  236. height: 100%;
  237. background-color: rgba(0, 0, 0, 0.6);
  238. position: fixed;
  239. top: 0;
  240. left: 0;
  241. z-index: 990;
  242. }
  243. .login-box {
  244. width: 80%;
  245. height: auto;
  246. background-color: #ffffff;
  247. border-radius: 16rpx;
  248. box-sizing: border-box;
  249. padding: 50rpx 30rpx;
  250. text-align: center;
  251. position: fixed;
  252. top: 50%;
  253. left: 50%;
  254. transform: translate(-50%, -50%);
  255. z-index: 991;
  256. }
  257. .login-title {
  258. font-weight: 500;
  259. color: #000000;
  260. padding-bottom: 10rpx;
  261. font-size: 16px;
  262. }
  263. .login-box > text {
  264. color: #999999;
  265. font-size: 28rpx;
  266. display: block;
  267. }
  268. .login-box > image {
  269. width: 80%;
  270. height: auto;
  271. margin: 20rpx 0 50rpx;
  272. }
  273. .login-btnbox {
  274. height: 100rpx;
  275. display: flex;
  276. justify-content: space-around;
  277. align-items: center;
  278. }
  279. button,
  280. button::after {
  281. margin: 0;
  282. padding: 0;
  283. border: none;
  284. outline: none;
  285. border-radius: 0;
  286. }
  287. .login-btn,
  288. button.login-btn {
  289. width: 200rpx;
  290. height: 80rpx;
  291. line-height: 80rpx;
  292. text-align: center;
  293. border-radius: 8rpx;
  294. color: #ffffff;
  295. font-size: 28rpx;
  296. }
  297. .login-page {
  298. width: 100vw;
  299. height: 100vh;
  300. display: flex;
  301. align-items: center;
  302. justify-content: center;
  303. }
  304. /* 授权按钮 */
  305. .submit-btn {
  306. width: 100%;
  307. background-color: #05c160;
  308. color: #ffffff;
  309. margin-top: 60rpx;
  310. border-radius: 10rpx;
  311. padding: 25rpx;
  312. font-size: 32rpx;
  313. display: flex;
  314. align-items: center;
  315. justify-content: center;
  316. margin: 30rpx;
  317. }
  318. </style>