index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <view class="main">
  3. <view class="wx">
  4. <view class="wx_1">
  5. <text class="iconfont icon-weixin"></text>
  6. </view>
  7. <view class="wx_2">
  8. <button size="mini" @tap="otherLogin()">微信信任登录</button>
  9. </view>
  10. <view class="wx_3">
  11. <checkbox-group @change="changeAgree">
  12. <label>
  13. <checkbox :checked="agree" />
  14. <text @tap.stop="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
  15. </label>
  16. </checkbox-group>
  17. </view>
  18. </view>
  19. <!-- <uni-popup ref="dialogShow" type="center" :mask-click="false" background-color="#ffffff">
  20. <view class="popup">
  21. <view class="title">
  22. <text>绑定手机号</text>
  23. </view>
  24. <view class="wx_1">
  25. <text>确定获取微信绑定手机号吗?</text>
  26. </view>
  27. <view class="wx_2">
  28. <button size="mini" @tap="diaClose">取消</button>
  29. <button size="mini" open-type="getPhoneNumber" @getphonenumber="getUserPhone">确定</button>
  30. </view>
  31. </view>
  32. </uni-popup> -->
  33. </view>
  34. </template>
  35. <script>
  36. export default {
  37. data() {
  38. return {
  39. openid: '',
  40. form: {},
  41. // 用戶协议
  42. agree: true,
  43. // 注册账号信息
  44. user: {},
  45. // 弹框
  46. dialog: {
  47. title: '绑定手机号'
  48. },
  49. };
  50. },
  51. onShow: function() {
  52. const that = this;
  53. // 查询平台信息
  54. that.searchOpenids();
  55. },
  56. methods: {
  57. async searchOpenids() {
  58. const that = this;
  59. try {
  60. const res = uni.getStorageSync('openid');
  61. if (res) that.$set(that, `openid`, res);
  62. } catch (e) {
  63. uni.showToast({
  64. title: err.errmsg,
  65. icon: 'error',
  66. duration: 2000
  67. });
  68. }
  69. },
  70. // 其他登录方式
  71. async otherLogin() {
  72. const that = this;
  73. let agree = that.agree;
  74. let openid = that.openid;
  75. if (agree) {
  76. if (openid) {
  77. const res = await that.$api(`/user/login`, 'GET', {
  78. openid: openid
  79. })
  80. if (res.errcode == '0') {
  81. const token = await that.$token(`/tool/token`, 'GET', {
  82. token: res.data
  83. });
  84. if (token.errcode == '0') {
  85. that.$set(that, `user`, token.data);
  86. // 登录成功
  87. uni.getUserInfo({
  88. provider: 'weixin',
  89. success: function(info) {
  90. // 修改用户信息
  91. that.updatePhone({
  92. nick_name: info.userInfo.nickName,
  93. logo: [{
  94. url: info.userInfo.avatarUrl
  95. }],
  96. })
  97. }
  98. })
  99. }
  100. } else {
  101. uni.showToast({
  102. title: res.errmsg || '信息错误',
  103. icon: 'none'
  104. })
  105. }
  106. } else {
  107. uni.showToast({
  108. title: '系统更新中,请稍后再试!',
  109. icon: 'none'
  110. })
  111. }
  112. } else {
  113. uni.showToast({
  114. title: '请阅读并同意用户协议和隐私政策',
  115. icon: 'none'
  116. })
  117. }
  118. },
  119. // 获取手机号
  120. getUserPhone(e) {
  121. console.log(e);
  122. const that = this;
  123. uni.login({
  124. provider: 'weixin',
  125. success: async function(data) {
  126. let res = await that.$api(`/wechat/api/login/getPhone`, 'GET', {
  127. config: that.$config.wx_projectkey,
  128. code: data.code
  129. })
  130. if (res.errcode == '0') {
  131. // 登录成功
  132. uni.getUserInfo({
  133. provider: 'weixin',
  134. success: function(info) {
  135. const phone = res.data && res.data.phone_info && res.data
  136. .phone_info
  137. .purePhoneNumber || '';
  138. // 修改用户信息
  139. that.updatePhone({
  140. phone,
  141. nick_name: info.userInfo.nickName,
  142. logo: [{
  143. url: info.userInfo.avatarUrl
  144. }],
  145. })
  146. }
  147. })
  148. } else {
  149. uni.showToast({
  150. title: res.errmsg,
  151. icon: 'none'
  152. })
  153. }
  154. },
  155. fail: function(err) {
  156. // 登录授权失败
  157. // err.code是错误码
  158. }
  159. });
  160. },
  161. async updatePhone(form) {
  162. const that = this;
  163. let user = that.user;
  164. let openid = that.openid;
  165. let arr = await that.$api(`/user/${user._id}`, 'POST', {
  166. openid,
  167. ...form
  168. })
  169. if (arr.errcode == '0') {
  170. let res = await that.$api(`/user/${user._id}`, 'GET', {})
  171. uni.setStorage({
  172. key: 'token',
  173. data: res.data,
  174. success: function(res) {
  175. uni.navigateBack({
  176. delta: 1
  177. })
  178. },
  179. fail: function(err) {
  180. console.log(err);
  181. }
  182. })
  183. } else {
  184. uni.showToast({
  185. title: res.errmsg,
  186. icon: 'none'
  187. })
  188. }
  189. },
  190. // 弹框关闭
  191. diaClose() {
  192. const that = this;
  193. that.$refs.dialogShow.close();
  194. },
  195. // 同意隐私协议
  196. changeAgree() {
  197. const that = this;
  198. let agree = true;
  199. if (that.agree) agree = false;
  200. that.$set(that, `agree`, agree);
  201. },
  202. // 查看隐私协议
  203. toAgree() {
  204. const that = this;
  205. uni.navigateTo({
  206. url: `/pagesOther/other/agree`
  207. })
  208. },
  209. },
  210. }
  211. </script>
  212. <style lang="scss" scoped>
  213. .main {
  214. display: flex;
  215. flex-direction: column;
  216. width: 100vw;
  217. height: 100vh;
  218. .wx {
  219. text-align: center;
  220. margin: 25vw 0 0 0;
  221. .wx_1 {
  222. margin: 0 0 5vw 0;
  223. .iconfont {
  224. color: var(--f35BColor);
  225. }
  226. text {
  227. font-size: 50px;
  228. }
  229. }
  230. .wx_2 {
  231. button {
  232. background: var(--f35BColor);
  233. color: var(--fffColor);
  234. font-size: var(--font16Size);
  235. }
  236. }
  237. .wx_3 {
  238. position: absolute;
  239. bottom: 10vw;
  240. width: 100vw;
  241. text-align: center;
  242. font-size: 12px;
  243. }
  244. }
  245. .popup {
  246. width: 86vw;
  247. padding: 2vw;
  248. .title {
  249. text-align: center;
  250. margin: 0 0 2vw 0;
  251. text {
  252. color: var(--fFB1Color);
  253. font-size: var(--font16Size);
  254. }
  255. }
  256. .wx_1 {
  257. text-align: center;
  258. margin: 9vw 0;
  259. text {
  260. font-size: var(--font18Size);
  261. }
  262. }
  263. .wx_2 {
  264. text-align: center;
  265. button {
  266. margin: 0 2vw;
  267. font-size: var(--font16Size);
  268. padding: 0 10vw;
  269. color: var(--fffColor);
  270. background-color: var(--f35BColor);
  271. }
  272. button:nth-child(1) {
  273. background-color: var(--fFB1Color);
  274. }
  275. }
  276. }
  277. }
  278. </style>